Created
April 17, 2015 10:09
-
-
Save mbaba/463be48790cbc655a13b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<script type="text/javascript" src="http://d3js.org/d3.v3.js"></script> | |
<link rel="stylesheet" type="text/css" href="main.css"> | |
<title>Matrix</title> | |
</head> | |
<body> | |
<script type="text/javascript"> | |
// variables | |
var w = 1500; | |
var h = 800; | |
var padding = [85, 105, 85, 105]; | |
var labels = ["Time pressure", "Discrimination", "Communication Problem", "Cooperation Problem", "Lack of Control", "Job Insecurity", "Difficult Customers", "Supervisor-Employee Problem", "Irregular Hours", "Human Resources Policy"] | |
var widthScaleCountry = d3.scale.ordinal() | |
.rangeRoundBands([0, w - padding[1] - padding[3] ], 0.1); | |
var heightScaleLabel = d3.scale.ordinal() | |
.rangeRoundBands([ padding[0], h - padding[2] ], 0.1); | |
var rScale = d3.scale.sqrt() | |
.domain([1, 81]) | |
.range([3, 20]); | |
var xAxisCountryBottom = d3.svg.axis() | |
.scale(widthScaleCountry) | |
.orient("bottom"); | |
var xAxisCountryTop = d3.svg.axis() | |
.scale(widthScaleCountry) | |
.orient("top"); | |
var yAxisLabelLeft = d3.svg.axis() | |
.scale(heightScaleLabel) | |
.orient("left"); | |
var yAxisLabelRight = d3.svg.axis() | |
.scale(heightScaleLabel) | |
.orient("right"); | |
d3.select("body") | |
.append("div") | |
.attr("id", "wrapper"); | |
d3.select("div", "#wrapper") | |
.append("div") | |
.attr("id", "container"); | |
var matrix = d3.select("#container") | |
.append("svg") | |
.attr("width", w) | |
.attr("height", h); | |
var tooltip = d3.select("body") | |
.append("div") | |
.attr("class", "tooltip"); | |
d3.csv("psychosocial_risk_major_concerns.csv", function(data) { | |
console.log(data); | |
widthScaleCountry.domain(data.map(function(d) { return d.country; } )); | |
heightScaleLabel.domain(labels); | |
// time_pressure | |
var circles = matrix.selectAll(".time-pressure") | |
.data(data) | |
.enter() | |
.append("circle"); | |
circles.attr("cx", function(d, i) { | |
return widthScaleCountry(d.country) + padding[3] + 20; | |
}) | |
.attr("cy", 125) | |
.attr("r", function(d) { | |
return rScale(+d.time_pressure);}) | |
.attr("class", "time-pressure"); | |
circles.on("mouseover", function(d) { | |
d3.select(this) | |
.classed("hover", true) | |
.attr("class", "hover-matrix") | |
tooltip = d3.select(".tooltip"); | |
tooltip.html('<span class="country">' + d.country + '</span>' + '<br />' + 'Time Pressure - ' + d.time_pressure + '%') | |
.style("top", d3.event.pageY - 12 + "px") | |
.style("opacity", 0.9) | |
}) | |
circles.on("mouseout", function() { | |
d3.select(this) | |
.classed("hover", false) | |
.attr("class", "time-pressure") | |
tooltip.style("opacity", 0) | |
}) | |
circles.on("mousemove", function (d) { | |
var tool_width = d3.select(".tooltip").node().getBoundingClientRect().width; | |
if(d3.event.pageX > (w - tool_width)) { | |
tooltip.style("left", d3.event.pageX - tool_width - 12 + "px") | |
} else { | |
tooltip.style("left", d3.event.pageX + 12 + "px") | |
} | |
}) | |
// discrimination | |
var circles = matrix.selectAll(".discrimination") | |
.data(data) | |
.enter() | |
.append("circle"); | |
circles.attr("cx", function(d, i) { | |
return widthScaleCountry(d.country) + padding[3] + 20; | |
}) | |
.attr("cy", 195) | |
.attr("r", function(d) { | |
return rScale(+d.discrimination);}) | |
.attr("class", "discrimination"); | |
circles.on("mouseover", function(d) { | |
d3.select(this) | |
.classed("hover", true) | |
.attr("class", "hover-matrix") | |
tooltip = d3.select(".tooltip"); | |
tooltip.html('<span class="country">' + d.country + '</span>' + '<br />' + 'Discrimination - ' + d.discrimination + '%') | |
.style("top", d3.event.pageY - 12 + "px") | |
.style("opacity", 0.9) | |
}) | |
circles.on("mouseout", function() { | |
d3.select(this) | |
.classed("hover", false) | |
.attr("class", "discrimination") | |
tooltip.style("opacity", 0) | |
}) | |
circles.on("mousemove", function (d) { | |
var tool_width = d3.select(".tooltip").node().getBoundingClientRect().width; | |
if(d3.event.pageX > (w - tool_width)) { | |
tooltip.style("left", d3.event.pageX - tool_width - 12 + "px") | |
} else { | |
tooltip.style("left", d3.event.pageX + 12 + "px") | |
} | |
}) | |
// communication_problem | |
var circles = matrix.selectAll(".communication") | |
.data(data) | |
.enter() | |
.append("circle"); | |
circles.attr("cx", function(d, i) { | |
return widthScaleCountry(d.country) + padding[3] + 20; | |
}) | |
.attr("cy", 260) | |
.attr("r", function(d) { | |
return rScale(+d.communication_problem);}) | |
.attr("class", "communication"); | |
circles.on("mouseover", function(d) { | |
d3.select(this) | |
.classed("hover", true) | |
.attr("class", "hover-matrix") | |
tooltip = d3.select(".tooltip"); | |
tooltip.html('<span class="country">' + d.country + '</span>' + '<br />' + 'Communication Problem - ' + d.communication_problem + '%') | |
.style("top", d3.event.pageY - 12 + "px") | |
.style("opacity", 0.9) | |
}) | |
circles.on("mouseout", function() { | |
d3.select(this) | |
.classed("hover", false) | |
.attr("class", "communication") | |
tooltip.style("opacity", 0) | |
}) | |
circles.on("mousemove", function (d) { | |
var tool_width = d3.select(".tooltip").node().getBoundingClientRect().width; | |
if(d3.event.pageX > (w - tool_width)) { | |
tooltip.style("left", d3.event.pageX - tool_width - 12 + "px") | |
} else { | |
tooltip.style("left", d3.event.pageX + 12 + "px") | |
} | |
}) | |
// cooperation_problem | |
var circles = matrix.selectAll(".cooperation") | |
.data(data) | |
.enter() | |
.append("circle"); | |
circles.attr("cx", function(d, i) { | |
return widthScaleCountry(d.country) + padding[3] + 20; | |
}) | |
.attr("cy", 325) | |
.attr("r", function(d) { | |
return rScale(+d.cooperation_problem);}) | |
.attr("class", "cooperation"); | |
circles.on("mouseover", function(d) { | |
d3.select(this) | |
.classed("hover", true) | |
.attr("class", "hover-matrix") | |
tooltip = d3.select(".tooltip"); | |
tooltip.html('<span class="country">' + d.country + '</span>' + '<br />' + 'Cooperation Problem - ' + d.cooperation_problem + '%') | |
.style("top", d3.event.pageY - 12 + "px") | |
.style("opacity", 0.9) | |
}) | |
circles.on("mouseout", function() { | |
d3.select(this) | |
.classed("hover", false) | |
.attr("class", "cooperation") | |
tooltip.style("opacity", 0) | |
}) | |
circles.on("mousemove", function (d) { | |
var tool_width = d3.select(".tooltip").node().getBoundingClientRect().width; | |
if(d3.event.pageX > (w - tool_width)) { | |
tooltip.style("left", d3.event.pageX - tool_width - 12 + "px") | |
} else { | |
tooltip.style("left", d3.event.pageX + 12 + "px") | |
} | |
}) | |
// lack_control | |
var circles = matrix.selectAll(".lack-control") | |
.data(data) | |
.enter() | |
.append("circle"); | |
circles.attr("cx", function(d, i) { | |
return widthScaleCountry(d.country) + padding[3] + 20; | |
}) | |
.attr("cy", 377) | |
.attr("r", function(d) { | |
return rScale(+d.lack_control);}) | |
.attr("class", "lack-control"); | |
circles.on("mouseover", function(d) { | |
d3.select(this) | |
.classed("hover", true) | |
.attr("class", "hover-matrix") | |
tooltip = d3.select(".tooltip"); | |
tooltip.html('<span class="country">' + d.country + '</span>' + '<br />' + 'Lack of Control - ' + d.lack_control + '%') | |
.style("top", d3.event.pageY - 12 + "px") | |
.style("opacity", 0.9) | |
}) | |
circles.on("mouseout", function() { | |
d3.select(this) | |
.classed("hover", false) | |
.attr("class", "lack-control") | |
tooltip.style("opacity", 0) | |
}) | |
circles.on("mousemove", function (d) { | |
var tool_width = d3.select(".tooltip").node().getBoundingClientRect().width; | |
if(d3.event.pageX > (w - tool_width)) { | |
tooltip.style("left", d3.event.pageX - tool_width - 12 + "px") | |
} else { | |
tooltip.style("left", d3.event.pageX + 12 + "px") | |
} | |
}) | |
// job_insecurity | |
var circles = matrix.selectAll(".job-insecurity") | |
.data(data) | |
.enter() | |
.append("circle"); | |
circles.attr("cx", function(d, i) { | |
return widthScaleCountry(d.country) + padding[3] + 20; | |
}) | |
.attr("cy", 435) | |
.attr("r", function(d) { | |
return rScale(+d.job_insecurity);}) | |
.attr("class", "job-insecurity"); | |
circles.on("mouseover", function(d) { | |
d3.select(this) | |
.classed("hover", true) | |
.attr("class", "hover-matrix") | |
tooltip = d3.select(".tooltip"); | |
tooltip.html('<span class="country">' + d.country + '</span>' + '<br />' + 'Job Insecurity - ' + d.job_insecurity + '%') | |
.style("top", d3.event.pageY - 12 + "px") | |
.style("opacity", 0.9) | |
}) | |
circles.on("mouseout", function() { | |
d3.select(this) | |
.classed("hover", false) | |
.attr("class", "job-insecurity") | |
tooltip.style("opacity", 0) | |
}) | |
circles.on("mousemove", function (d) { | |
var tool_width = d3.select(".tooltip").node().getBoundingClientRect().width; | |
if(d3.event.pageX > (w - tool_width)) { | |
tooltip.style("left", d3.event.pageX - tool_width - 12 + "px") | |
} else { | |
tooltip.style("left", d3.event.pageX + 12 + "px") | |
} | |
}) | |
// difficult-customers | |
var circles = matrix.selectAll(".difficult-customers") | |
.data(data) | |
.enter() | |
.append("circle"); | |
circles.attr("cx", function(d, i) { | |
return widthScaleCountry(d.country) + padding[3] + 20; | |
}) | |
.attr("cy", 500) | |
.attr("r", function(d) { | |
return rScale(+d.difficult_customers);}) | |
.attr("class", "difficult-customers"); | |
circles.on("mouseover", function(d) { | |
d3.select(this) | |
.classed("hover", true) | |
.attr("class", "hover-matrix") | |
tooltip = d3.select(".tooltip"); | |
tooltip.html('<span class="country">' + d.country + '</span>' + '<br />' + 'Difficult Customers - ' + d.difficult_customers + '%') | |
.style("top", d3.event.pageY - 12 + "px") | |
.style("opacity", 0.9) | |
}) | |
circles.on("mouseout", function() { | |
d3.select(this) | |
.classed("hover", false) | |
.attr("class", "difficult-customers") | |
tooltip.style("opacity", 0) | |
}) | |
circles.on("mousemove", function (d) { | |
var tool_width = d3.select(".tooltip").node().getBoundingClientRect().width; | |
if(d3.event.pageX > (w - tool_width)) { | |
tooltip.style("left", d3.event.pageX - tool_width - 12 + "px") | |
} else { | |
tooltip.style("left", d3.event.pageX + 12 + "px") | |
} | |
}) | |
// supervisor_employee_problem | |
var circles = matrix.selectAll(".supervisor-employee") | |
.data(data) | |
.enter() | |
.append("circle"); | |
circles.attr("cx", function(d, i) { | |
return widthScaleCountry(d.country) + padding[3] + 20; | |
}) | |
.attr("cy", 570) | |
.attr("r", function(d) { | |
return rScale(+d.supervisor_employee_problem);}) | |
.attr("class", "supervisor-employee"); | |
circles.on("mouseover", function(d) { | |
d3.select(this) | |
.classed("hover", true) | |
.attr("class", "hover-matrix") | |
tooltip = d3.select(".tooltip"); | |
tooltip.html('<span class="country">' + d.country + '</span>' + '<br />' + 'Supervisor Employee Problem - ' + d.supervisor_employee_problem + '%') | |
.style("top", d3.event.pageY - 12 + "px") | |
.style("opacity", 0.9) | |
}) | |
circles.on("mouseout", function() { | |
d3.select(this) | |
.classed("hover", false) | |
.attr("class", "supervisor-employee") | |
tooltip.style("opacity", 0) | |
}) | |
circles.on("mousemove", function (d) { | |
var tool_width = d3.select(".tooltip").node().getBoundingClientRect().width; | |
if(d3.event.pageX > (w - tool_width)) { | |
tooltip.style("left", d3.event.pageX - tool_width - 12 + "px") | |
} else { | |
tooltip.style("left", d3.event.pageX + 12 + "px") | |
} | |
}) | |
// human_resources_policy | |
var circles = matrix.selectAll(".human-resources-policy") | |
.data(data) | |
.enter() | |
.append("circle"); | |
circles.attr("cx", function(d, i) { | |
return widthScaleCountry(d.country) + padding[3] + 20; | |
}) | |
.attr("cy", 690) | |
.attr("r", function(d) { | |
return rScale(+d.human_resources_policy);}) | |
.attr("class", "human-resources-policy"); | |
circles.on("mouseover", function(d) { | |
d3.select(this) | |
.classed("hover", true) | |
.attr("class", "hover-matrix") | |
tooltip = d3.select(".tooltip"); | |
tooltip.html('<span class="country">' + d.country + '</span>' + '<br />' + 'Human Resources Policy - ' + d.human_resources_policy + '%') | |
.style("top", d3.event.pageY - 12 + "px") | |
.style("opacity", 0.9) | |
}) | |
circles.on("mouseout", function() { | |
d3.select(this) | |
.classed("hover", false) | |
.attr("class", "human-resources-policy") | |
tooltip.style("opacity", 0) | |
}) | |
circles.on("mousemove", function (d) { | |
var tool_width = d3.select(".tooltip").node().getBoundingClientRect().width; | |
if(d3.event.pageX > (w - tool_width)) { | |
tooltip.style("left", d3.event.pageX - tool_width - 12 + "px") | |
} else { | |
tooltip.style("left", d3.event.pageX + 12 + "px") | |
} | |
}) | |
// irregular_hours | |
var circles = matrix.selectAll(".irregular-hours") | |
.data(data) | |
.enter() | |
.append("circle"); | |
circles.attr("cx", function(d, i) { | |
return widthScaleCountry(d.country) + padding[3] + 20; | |
}) | |
.attr("cy", 625) | |
.attr("r", function(d) { | |
return rScale(+d.irregular_hours);}) | |
.attr("class", "irregular-hours"); | |
circles.on("mouseover", function(d) { | |
d3.select(this) | |
.classed("hover", true) | |
.attr("class", "hover-matrix") | |
tooltip = d3.select(".tooltip"); | |
tooltip.html('<span class="country">' + d.country + '</span>' + '<br />' + 'Irregular Hours - ' + d.irregular_hours + '%') | |
.style("top", d3.event.pageY - 12 + "px") | |
.style("opacity", 0.9) | |
}) | |
circles.on("mouseout", function() { | |
d3.select(this) | |
.classed("hover", false) | |
.attr("class", "irregular-hours") | |
tooltip.style("opacity", 0) | |
}) | |
circles.on("mousemove", function (d) { | |
var tool_width = d3.select(".tooltip").node().getBoundingClientRect().width; | |
if(d3.event.pageX > (w - tool_width)) { | |
tooltip.style("left", d3.event.pageX - tool_width - 12 + "px") | |
} else { | |
tooltip.style("left", d3.event.pageX + 12 + "px") | |
} | |
}) | |
// axis | |
matrix.append("g") | |
.attr("class", "x matrix-axis") | |
.attr("transform", "translate("+ padding[3] + "," + (padding[2]) + ")") | |
.call(xAxisCountryTop) | |
.selectAll("text") | |
.style("text-anchor", "end") | |
.attr("dy", "10") | |
.attr("transform", function(d) { | |
return "rotate(90)" | |
}); | |
matrix.append("g") | |
.attr("class", "x matrix-axis") | |
.attr("transform", "translate("+ padding[3] + "," + (h - padding[2]) + ")") | |
.call(xAxisCountryBottom) | |
.selectAll("text") | |
.style("text-anchor", "start") | |
.attr("dy", "-5") | |
.attr("transform", function(d) { | |
return "rotate(90)" | |
}); | |
matrix.append("g") | |
.attr("class", "y matrix-axis") | |
.attr("transform", "translate(" + (padding[3]) + ",0)") | |
.call(yAxisLabelLeft) | |
.selectAll(".tick text") | |
.call(wrap, heightScaleLabel.rangeBand()); | |
matrix.append("g") | |
.attr("class", "y matrix-axis") | |
.attr("transform", "translate(" + (w - padding[1]) + ",0)") | |
.call(yAxisLabelRight) | |
.selectAll(".tick text") | |
.call(wrap, heightScaleLabel.rangeBand()); | |
// line brakes | |
function wrap(text, width) { | |
text.each(function() { | |
var text = d3.select(this), | |
words = text.text().split(/\s+/).reverse(), | |
word, | |
line = [], | |
lineNumber = 0, | |
lineHeight = 1.1, // ems | |
y = text.attr("y"), | |
dy = parseFloat(text.attr("dy")), | |
tspan = text.text(null).append("tspan").attr("x", 0).attr("y", y).attr("dy", dy + "em"); | |
while (word = words.pop()) { | |
line.push(word); | |
tspan.text(line.join(" ")); | |
if (tspan.node().getComputedTextLength() > width) { | |
line.pop(); | |
tspan.text(line.join(" ")); | |
line = [word]; | |
tspan = text.append("tspan").attr("x", 0).attr("y", y).attr("dy", ++lineNumber * lineHeight + dy + "em").text(word); | |
} | |
} | |
}); | |
} | |
// line breaks | |
}); | |
</script> | |
</body> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
body { | |
background-color: #d2d0cc; | |
font-family: Helvetica, Arial, sans-serif; | |
font-size: 16px; | |
} | |
h1 { | |
font-size: 28px; | |
font-weight: bold; | |
font-variant: small-caps; | |
text-align: center; | |
line-height: 14px; | |
color: dimgrey; | |
} | |
a { | |
text-decoration: none; | |
} | |
a:link, a:visited { | |
color: dimgrey; | |
border-bottom: 2px dotted dimgrey; | |
} | |
a:hover { | |
color: #ffc04c; | |
border-bottom: 2px dotted #ffc04c; | |
} | |
.chart-title { | |
font-size: 20px; | |
font-weight: bold; | |
font-variant: small-caps; | |
letter-spacing: 2px; | |
padding-top: 5px; | |
padding-bottom: 5px; | |
text-align: center; | |
color: dimgrey; | |
} | |
.text { | |
padding: 1px 1%; | |
margin-left: 1%; | |
margin-right: 1%; | |
margin-bottom: 25px; | |
background-color: #ededeb; | |
text-align: left; | |
} | |
.axis path, .axis line { | |
fill: none; | |
stroke: dimgrey; | |
shape-rendering: crispEdges; | |
} | |
.matrix-axis path, .matrix-axis line { | |
visibility: hidden; | |
} | |
.axis text, .matrix-axis text, .label { | |
font-family: Helvetica, Arial, sans-serif; | |
font-size: 11px; | |
fill: dimgrey; | |
} | |
.label { | |
font-family: Helvetica, Arial, sans-serif; | |
font-size: 12px; | |
font-weight: bold; | |
letter-spacing: 2px; | |
fill: dimgrey; | |
} | |
#wrapper { | |
width: 85%; | |
height: 100%; | |
margin: 0 auto; | |
position: relative; | |
} | |
#container { | |
padding: 1px 1%; | |
margin-left: 1%; | |
margin-right: 1%; | |
margin-bottom: 25px; | |
background-color: #ededeb; | |
text-align: center; | |
} | |
#col-left1, #col-left2, #col-left3, #col-left4, #col-left5, #col-right1, #col-right2, #col-right3, #col-right4, #col-right5 { | |
margin-bottom: 25px; | |
padding-left: 1%; | |
padding-bottom: 1%; | |
width: 47%; | |
background-color: #ededeb; | |
} | |
#col-left1, #col-left2, #col-left3, #col-left4, #col-left5 { | |
float: left; | |
margin-left: 1%; | |
} | |
#col-right1, #col-right2, #col-right3, #col-right4, #col-right5 { | |
float: right; | |
margin-right: 1% | |
} | |
svg { | |
fill: #ff7b00; | |
} | |
/*Circles*/ | |
.time-pressure { | |
fill: #a6cee3; | |
} | |
.discrimination { | |
fill: #1f78b4; | |
} | |
.communication { | |
fill: #b2df8a; | |
} | |
.cooperation { | |
fill: #33a02c; | |
} | |
.lack-control { | |
fill: #fb9a99; | |
} | |
.job-insecurity { | |
fill: #e31a1c; | |
} | |
.difficult-customers { | |
fill: #fdbf6f; | |
} | |
.supervisor-employee { | |
fill: #b15928; | |
} | |
.irregular-hours { | |
fill: #cab2d6; | |
} | |
.human-resources-policy { | |
fill: #6a3d9a; | |
} | |
/*ToolTip*/ | |
.tooltip { | |
padding: 3px; | |
background-color: #d2d0cc; | |
border-radius: 4px; | |
position: absolute; | |
font-size: 11px; | |
line-height: 14px; | |
opacity: 0; | |
} | |
.country { | |
font-weight: bold; | |
font-size: 11px; | |
} | |
.hover { | |
cursor: crosshair; | |
} | |
.hover-matrix { | |
fill: #ff7b00; | |
cursor: crosshair; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment