sources: https://cepa.stanford.edu/seda/data-archive
d3.js source:
http://bl.ocks.org/weiglemc/6185069
Tooltip example - http://www.d3noob.org/2013/01/adding-tooltips-to-d3js-graph.html
sources: https://cepa.stanford.edu/seda/data-archive
d3.js source:
http://bl.ocks.org/weiglemc/6185069
Tooltip example - http://www.d3noob.org/2013/01/adding-tooltips-to-d3js-graph.html
<!DOCTYPE html> | |
<html> | |
<meta charset="utf-8"> | |
<style> | |
body { | |
font: 11px sans-serif; | |
} | |
.axis path, | |
.axis line { | |
fill: none; | |
stroke: #000; | |
shape-rendering: crispEdges; | |
} | |
.dot { | |
stroke: #000; | |
} | |
.tooltip { | |
position: absolute; | |
width: 200px; | |
height: 28px; | |
pointer-events: none; | |
} | |
</style> | |
<body> | |
<div id="correl-1-title">Median Income | |
<div id="correl-1-chart"></div> | |
</div> | |
<script src="http://d3js.org/d3.v3.min.js"></script> | |
<script> | |
var margin = {top: 20, right: 20, bottom: 30, left: 40}, | |
width = 1000 - margin.left - margin.right, | |
height = 500 - margin.top - margin.bottom; | |
var xValue = function(d) { return d.income50perc;}, // data -> value | |
xScale = d3.scale.linear().range([0, width]), // value -> display | |
xMap = function(d) { return xScale(xValue(d));}, // data -> display | |
xAxis = d3.svg.axis().scale(xScale).orient("bottom"); | |
var yValue = function(d) { return d["averagetest"];}, // data -> value | |
yScale = d3.scale.linear().range([height, 0]), // value -> display | |
yMap = function(d) { return yScale(yValue(d));}, // data -> display | |
yAxis = d3.svg.axis().scale(yScale).orient("left"); | |
var svg = d3.select("#correl-1-chart").append("svg") | |
.attr("width", width + margin.left + margin.right) | |
.attr("height", height + margin.top + margin.bottom) | |
.append("g") | |
.attr("transform", "translate(" + margin.left + "," + margin.top + ")"); | |
var tooltip = d3.select("body").append("div") | |
.attr("class", "tooltip") | |
.style("opacity", 0); | |
d3.csv("https://raw.githubusercontent.com/nadinesk/nadinesk.github.io/master/d3Data/schooldata2.csv", function(error, data) { | |
data.forEach(function(d) { | |
d.income50perc = +d.income50perc; | |
d["averagetest"] = +d["averagetest"]; | |
}); | |
xScale.domain([d3.min(data, xValue)-1, d3.max(data, xValue)+1]); | |
yScale.domain([d3.min(data, yValue)-0.2, d3.max(data, yValue)+0.2]); | |
svg.append("g") | |
.attr("class", "x axis") | |
.attr("transform", "translate(0," + height + ")") | |
.call(xAxis) | |
.append("text") | |
.attr("class", "label") | |
.attr("x", width) | |
.attr("y", -6) | |
.style("text-anchor", "end") | |
.text("median income"); | |
svg.append("g") | |
.attr("class", "y axis") | |
.call(yAxis) | |
.append("text") | |
.attr("class", "label") | |
.attr("transform", "rotate(-90)") | |
.attr("y", 6) | |
.attr("dy", ".71em") | |
.style("text-anchor", "end") | |
.text("grades ahead/below"); | |
svg.selectAll(".dot") | |
.data(data) | |
.enter().append("circle") | |
.attr("class", "dot") | |
.attr("r", function(d) { if (d.totalenrollment >= 400000) | |
{ | |
return 25 | |
} | |
else if (d.totalenrollment < 400000 && d.totalenrollment >= 100000) | |
{ | |
return 20 | |
} | |
else if (d.totalenrollment < 100000 && d.totalenrollment >= 50000) | |
{ | |
return 15 | |
} | |
else if (d.totalenrollment < 50000 && d.totalenrollment >= 10000) | |
{ | |
return 10 | |
} | |
else if (d.totalenrollment < 10000 && d.totalenrollment >= 1000) | |
{ | |
return 8 | |
} | |
else | |
return 4 | |
}) | |
.attr("cx", xMap) | |
.attr("cy", yMap) | |
.style("fill", function(d) { | |
if (d.totalenrollment >= 400000) | |
{ | |
return "rgba(255, 116, 140,1.0)" | |
} | |
else if (d.totalenrollment < 400000 && d.totalenrollment >= 100000) | |
{ | |
return "rgba(255, 116, 140,0.8)" | |
} | |
else if (d.totalenrollment < 100000 && d.totalenrollment >= 50000) | |
{ | |
return "rgba(255, 116, 140,0.7)" | |
} | |
else if (d.totalenrollment < 50000 && d.totalenrollment >= 10000) | |
{ | |
return "rgba(255, 116, 140,0.6)" | |
} | |
else if (d.totalenrollment < 10000 && d.totalenrollment >= 1000) | |
{ | |
return "rgba(255, 116, 140,0.5)" | |
} | |
else | |
return "rgba(255, 116, 140,0.1)" | |
}) | |
.style("stroke", "white") | |
.on("mouseover", function(d) { | |
tooltip.transition() | |
.duration(200) | |
.style("opacity", .9); | |
tooltip.html(d["educationagencyname"] + "<br/> (" + xValue(d) | |
+ ", " + yValue(d) + ")") | |
.style("left", (d3.event.pageX + 5) + "px") | |
.style("top", (d3.event.pageY - 28) + "px"); | |
}) | |
.on("mouseout", function(d) { | |
tooltip.transition() | |
.duration(500) | |
.style("opacity", 0); | |
}); | |
}); | |
</script> | |
</body> | |
</html> |