|
<!DOCTYPE html> |
|
<meta charset="utf-8"> |
|
<style> |
|
|
|
svg { |
|
font: 10px avenir; |
|
padding: 10px; |
|
margin-top: 15px; |
|
margin-bottom:15px; |
|
margin-right:50px; |
|
margin-left:50px; |
|
} |
|
|
|
.axis, |
|
.frame { |
|
shape-rendering: crispEdges; |
|
} |
|
|
|
|
|
.axis line { |
|
stroke: #ddd; |
|
} |
|
|
|
.axis path { |
|
display: none; |
|
} |
|
|
|
|
|
.cell text { |
|
font-weight: bold; |
|
text-transform: capitalize; |
|
font-size: 10px; |
|
} |
|
|
|
.frame { |
|
fill: none; |
|
|
|
} |
|
|
|
circle { |
|
fill-opacity: .7; |
|
} |
|
|
|
</style> |
|
<body> |
|
<script src="//d3js.org/d3.v3.min.js"></script> |
|
<script> |
|
|
|
var radius = 2; |
|
var width = 800, |
|
size = 110, |
|
padding = 15; |
|
|
|
|
|
var hidden = [7,14,15,21,22,23,28,29,30,31,35,36,37,38,39,42,43,44,45,46,47]; |
|
var tiers = ["Highly Selective", "Selective", "Non Selective", "Other (Two-year)"]; |
|
|
|
var x = d3.scale.linear() |
|
.range([padding / 2, size - padding / 2]); |
|
|
|
var y = d3.scale.linear() |
|
.range([size - padding / 2, padding / 2]); |
|
|
|
var formatxAxis = d3.format('.0f'); |
|
|
|
var xAxis = d3.svg.axis() |
|
.scale(x) |
|
.orient("bottom") |
|
.ticks(4) |
|
.tickFormat(function (d) { |
|
if ((d / 1000) >= 1) { |
|
d = d / 1000 + "K"; |
|
return d; |
|
} |
|
|
|
else if (d < 1){ |
|
return formatxAxis(d*100)+"%"; |
|
} |
|
|
|
}); |
|
|
|
|
|
|
|
var yAxis = d3.svg.axis() |
|
.scale(y) |
|
.orient("right") |
|
.ticks(4) |
|
.tickFormat(function (d) { |
|
if ((d / 1000) >= 1) { |
|
d = d / 1000 + "K"; |
|
|
|
return d; |
|
} |
|
|
|
else if (d < 1){ |
|
return formatxAxis(d*100)+"%"; |
|
} |
|
|
|
}); |
|
|
|
var color_range = ["#1f77b4", "#ff7f0e" , "#2ca02c", "#d62728"] |
|
|
|
var color = d3.scale.ordinal() |
|
.range(color_range); |
|
|
|
function toNumber(text) { |
|
text = text.trim(); |
|
text = text.replace(/,/, ""); |
|
return +text; |
|
} |
|
|
|
|
|
d3.csv("scatter.csv", |
|
|
|
function(d) { |
|
return { |
|
|
|
childRank:+d.k_rank, |
|
parentQuartile5: +d.par_q5, |
|
parentQuartile4:+d.par_q4, |
|
parentQuartile3: +d.par_q3, |
|
parentQuartile2: +d.par_q2, |
|
parentQuartile1: +d.par_q1, |
|
childMedianInc: +d.k_median, |
|
|
|
|
|
tiername: d.tiername |
|
}; |
|
}, |
|
|
|
|
|
function(error, data) { |
|
if (error) throw error; |
|
/* |
|
* converts strings (with extra spaces and comma thousands |
|
* separators) to a number |
|
*/ |
|
|
|
|
|
if (error) throw error; |
|
|
|
var domainByTrait = {}, |
|
traits = d3.keys(data[0]).filter(function(d) { return d !== "tiername"; }), |
|
|
|
n = traits.length; |
|
|
|
|
|
traits.forEach(function(trait) { |
|
|
|
|
|
domainByTrait[trait] = d3.extent(data, function(d) { return d[trait]; }); |
|
|
|
}); |
|
|
|
|
|
// xAxis.tickSize(size); |
|
// yAxis.tickSize(-size * n); |
|
|
|
var svg = d3.select("body").append("svg") |
|
.attr("width", size * n + padding+120) |
|
.attr("height", size * n + padding+100) |
|
.append("g") |
|
.attr("transform", "translate(" + padding + "," + padding/.5 + ")"); |
|
|
|
|
|
|
|
svg.append("text") |
|
.attr("x", 0) |
|
.attr("y", 120 ) |
|
.attr("text-anchor", "start") |
|
.style("font-size", "40px") |
|
.text("Child Rank Relative to") |
|
// .style('fill', 'darkOrange'); |
|
svg.append("text") |
|
.attr("x", 0) |
|
.attr("y", 120 ) |
|
.attr("dy", "1.2em" ) |
|
.attr("text-anchor", "start") |
|
.style("font-size", "50px") |
|
.text("Parent Income Quartile") |
|
.style('fill', 'darkOrange'); |
|
|
|
svg.append("text") |
|
.attr("x", 0) |
|
.attr("y", 120 ) |
|
.attr("dy", "3.5em" ) |
|
.attr("text-anchor", "start") |
|
.style("font-size", "30px") |
|
.text("for Universities in California") |
|
// .style('fill', 'darkOrange'); |
|
|
|
|
|
|
|
svg.selectAll(".x.axis") |
|
.data(traits) |
|
.enter().append("g") |
|
.attr("class", "x axis") |
|
.attr("transform", function(d, i) { return "translate(" + (n - i - 1) * size + ","+size*n+")"; }) |
|
.each(function(d) { x.domain(domainByTrait[d]); d3.select(this).call(xAxis); }); |
|
|
|
svg.selectAll(".y.axis") |
|
.data(traits) |
|
.enter().append("g") |
|
.attr("class", "axis") |
|
.attr("transform", function(d, i) { return "translate(770," + i * size + ")"; }) |
|
.each(function(d){ |
|
|
|
y.domain(domainByTrait[d]); |
|
|
|
d3.select(this).call(yAxis) |
|
}); |
|
|
|
var cell = svg.selectAll(".cell") |
|
.data(cross(traits, traits)) |
|
.enter().append("g") |
|
.attr("class", function(d,i){ |
|
|
|
// console.log("cell"+i); |
|
return "cell"+i; |
|
}) |
|
|
|
.attr("id", function(d,i){ |
|
|
|
// console.log("cell"+i); |
|
return "cell"+i; |
|
}) |
|
|
|
.attr("transform", function(d) { return "translate(" + (n - d.i - 1) * size + "," + d.j * size + ")"; }) |
|
.each(plot); |
|
|
|
// Titles for the diagonal. |
|
cell.filter(function(d) { return d.i === d.j; }).append("text") |
|
.attr("x", padding-5) |
|
.attr("y", padding) |
|
.attr("dy", ".71em") |
|
.text(function(d) { return d.x; }) |
|
.style("font-weight", "bold"); |
|
|
|
hide(); |
|
|
|
var legend = svg.selectAll(".legend") |
|
.data(color.domain()).enter() |
|
.append("g") |
|
.attr("class","legend") |
|
.attr("transform", "translate(" + 0 + "," + 350+ ")"); |
|
|
|
legend.append("rect") |
|
.attr("x", 0) |
|
.attr("y", function(d, i) { return 20 * i; }) |
|
.attr("width", 15) |
|
.attr("height", 15) |
|
.style("fill", function(d,i) { return color_range[i % color_range.length]}) |
|
|
|
// console.log(color.domain()) |
|
|
|
legend.append("text") |
|
.attr("x", 30) |
|
.attr("dy", "1em") |
|
.attr("y", function(d, i) { return 20 * i; }) |
|
.text(function(d,i) { ; return tiers[i % tiers.length]}) |
|
.attr("font-size", "12px"); |
|
|
|
|
|
legend.append("text") |
|
.attr("x",0) |
|
.attr("dy", "-.2em") |
|
.attr("y",-10) |
|
.text("School Tier") |
|
.attr("font-size", "17px"); |
|
|
|
|
|
function plot(p) { |
|
var cell = d3.select(this); |
|
|
|
|
|
x.domain(domainByTrait[p.x]); |
|
y.domain(domainByTrait[p.y]); |
|
|
|
|
|
|
|
hide(cell); |
|
|
|
cell.append("rect") |
|
.attr("class", "frame") |
|
.attr("x", padding / 2) |
|
.attr("y", padding / 2) |
|
.attr("width", size - padding) |
|
.attr("height", size - padding) |
|
.attr('stroke', function(d){ |
|
|
|
|
|
|
|
|
|
|
|
//grab child rank with all others |
|
if (cell.data()[0]['j'] == 0 & (cell.data()[0]['i'] == 1 || cell.data()[0]['i'] == 2 || cell.data()[0]['i'] == 3 || cell.data()[0]['i'] == 4 || cell.data()[0]['i'] == 5 || cell.data()[0]['i'] == 6)) { |
|
// console.log("yes") |
|
return 'grey'; |
|
} |
|
|
|
//grab child median with all others |
|
if (cell.data()[0]['i'] == 6 & (cell.data()[0]['j'] == 1 || cell.data()[0]['j'] == 2 || cell.data()[0]['j'] == 3 || cell.data()[0]['j'] == 4 || cell.data()[0]['j'] == 5 || cell.data()[0]['j'] == 6 )) { |
|
// console.log("yes") |
|
return 'grey'; |
|
} |
|
|
|
//GET ACROSS DIAGONAL |
|
|
|
//grab child rank with all others |
|
if (cell.data()[0]['j'] == 6 & (cell.data()[0]['i'] == 1 || cell.data()[0]['i'] == 2 || cell.data()[0]['i'] == 3 || cell.data()[0]['i'] == 4 || cell.data()[0]['i'] == 5 || cell.data()[0]['i'] == 0)) { |
|
// console.log("yes") |
|
return 'grey'; |
|
} |
|
|
|
//grab child median with all others |
|
if (cell.data()[0]['i'] == 0 & (cell.data()[0]['j'] == 1 || cell.data()[0]['j'] == 2 || cell.data()[0]['j'] == 3 || cell.data()[0]['j'] == 4 || cell.data()[0]['j'] == 5 || cell.data()[0]['j'] == 0 )) { |
|
return 'grey'; |
|
} |
|
else{ |
|
return '#aaa'; |
|
}}) |
|
|
|
//CHANGE COLOR STROKE |
|
|
|
.attr('stroke-width', function(d){ |
|
if (cell.data()[0]['j'] == 0 & (cell.data()[0]['i'] == 1 || cell.data()[0]['i'] == 2 || cell.data()[0]['i'] == 3 || cell.data()[0]['i'] == 4 || cell.data()[0]['i'] == 5 || cell.data()[0]['i'] == 6)) { |
|
// |
|
return '2.5px'; |
|
} |
|
|
|
//grab child median with all others |
|
if (cell.data()[0]['i'] == 6 & (cell.data()[0]['j'] == 1 || cell.data()[0]['j'] == 2 || cell.data()[0]['j'] == 3 || cell.data()[0]['j'] == 4 || cell.data()[0]['j'] == 5 || cell.data()[0]['j'] == 6 )) { |
|
// |
|
return '2.5px'; |
|
} |
|
|
|
//GET ACROSS DIAGONAL |
|
|
|
//grab child rank with all others |
|
if (cell.data()[0]['j'] == 6 & (cell.data()[0]['i'] == 1 || cell.data()[0]['i'] == 2 || cell.data()[0]['i'] == 3 || cell.data()[0]['i'] == 4 || cell.data()[0]['i'] == 5 || cell.data()[0]['i'] == 0)) { |
|
// |
|
return '2.5px'; |
|
} |
|
|
|
//grab child median with all others |
|
if (cell.data()[0]['i'] == 0 & (cell.data()[0]['j'] == 1 || cell.data()[0]['j'] == 2 || cell.data()[0]['j'] == 3 || cell.data()[0]['j'] == 4 || cell.data()[0]['j'] == 5 || cell.data()[0]['j'] == 0 )) { |
|
return '2.5px'; |
|
} |
|
else{ |
|
return '1px'; |
|
}}) |
|
|
|
|
|
cell.selectAll("circle") |
|
.data(data) |
|
.enter().append("circle") |
|
.attr("cx", function(d) { return x(d[p.x]); }) |
|
.attr("cy", function(d) { return y(d[p.y]); }) |
|
.attr("r", 1.2) |
|
.style("fill", function(d) { return color(d.tiername); }); |
|
|
|
} |
|
}); |
|
|
|
|
|
function cross(a, b) { |
|
var c = []; |
|
var n = a.length; |
|
var m = b.length; |
|
var i |
|
var j; |
|
|
|
for (i = -1; ++i < n;) |
|
for (j = -1; ++j < m;) |
|
c.push({x: a[i], i: i, y: b[j], j: j}); |
|
|
|
return c; |
|
}; |
|
|
|
function hide(cell){ |
|
|
|
var num; |
|
|
|
|
|
|
|
for (num = 0; num < 49 ; num++){ |
|
|
|
|
|
if (hidden.includes(num)){ |
|
|
|
d3.select("#cell"+num) |
|
.remove(); |
|
} |
|
} |
|
|
|
}; |
|
|
|
|
|
|
|
</script> |