Skip to content

Instantly share code, notes, and snippets.

@marreguin523
Last active May 15, 2017 02:17
Show Gist options
  • Save marreguin523/425c8856d407b625375dee04253b50ef to your computer and use it in GitHub Desktop.
Save marreguin523/425c8856d407b625375dee04253b50ef to your computer and use it in GitHub Desktop.
New Scatterplot
license: mit
<!DOCTYPE html>
<meta charset="utf-8">
<style>
svg {
font: 10px sans-serif;
padding: 10px;
}
text {
font: 7px sans-serif;
}
.axis,
.frame {
shape-rendering: crispEdges;
}
.axis line {
stroke: #ddd;
}
.axis path {
display: none;
}
.cell text {
font-weight: bold;
text-transform: capitalize;
}
.frame {
fill: none;
stroke: #aaa;
}
circle {
fill-opacity: .7;
}
circle.hidden {
fill: #ccc !important;
}
.extent {
fill: #000;
fill-opacity: .125;
stroke: #fff;
}
.legend {
font-size: 12px;
}
rect {
stroke-width: 2;
}
</style>
<body>
<script src="//d3js.org/d3.v3.min.js"></script>
<script>
var width = 960,
height = 800,
size = 150,
padding = 20;
var x = d3.scale.linear()
.range([padding / 2, size - padding / 2]);
var y = d3.scale.linear()
.range([size - padding / 2, padding / 2]);
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom")
.ticks(3)
.tickFormat(function (d) {
if (d >=1000) {
d = d / 1000 + "K";
}
return d;
});
var yAxis = d3.svg.axis()
.scale(y)
.orient("left")
.ticks(3)
.tickFormat(function (d) {
if (d >= 1000) {
d = d / 1000 + "K";
}
return d;
});
var color = d3.scale.category10();
d3.csv("mrc_table2 (4).csv", function(error, data) {
if (error) throw error;
var domainByTrait = {},
traits = d3.keys(data[0]).filter(function(d) { return d !== "name" && d!== "czname"&& d!=="tier_name"; }),
n = traits.length;
traits.forEach(function(trait) {
domainByTrait[trait] = d3.extent(data, function(d) { return d[trait]; });
});
xAxis.tickSize(size * n);
yAxis.tickSize(-size * n);
var svg = d3.select("body").append("svg")
.attr("width", size * n + padding)
.attr("height", size * n + padding)
.append("g")
.attr("transform", "translate(" + padding + "," + padding / 2 + ")");
svg.selectAll(".x.axis")
.data(traits)
.enter().append("g")
.attr("class", "x axis")
.attr("transform", function(d, i) { return "translate(" + (n - i - 1) * size + ",0)"; })
.each(function(d) { x.domain(domainByTrait[d]); d3.select(this).call(xAxis); });
svg.selectAll(".y.axis")
.data(traits)
.enter().append("g")
.attr("class", "y axis")
.attr("transform", function(d, i) { return "translate(0," + 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", "cell")
.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)
.attr("y", padding)
.attr("dy", ".71em")
.text(function(d) { return d.x; });
function plot(p) {
var cell = d3.select(this);
x.domain(domainByTrait[p.x]);
y.domain(domainByTrait[p.y]);
cell.append("rect")
.attr("class", "frame")
.attr("x", padding / 2)
.attr("y", padding / 2)
.attr("width", size - padding)
.attr("height", size - padding);
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", 4)
.style("fill", function(d) { return color(d.czname); });
}
var legendRectSize = 18;
var legendSpacing = 4;
var SF = 0;
var LA = 0;
var SB = 0;
var SA = 0;
var BA = 0;
var FR = 0;
var legend = svg.selectAll('.legend')
.data(data.sort())
.enter()
.append('g')
.filter(
function(d) {
if(d.czname == "Los Angeles" && LA == 0) {
LA = LA+1; return d.czname;}
if(d.czname == "San Francisco" && SF == 0) {
SF = SF+1; return d.czname;}
if(d.czname == "Santa Barbara" && SB == 0) {
SB = SB+1; return d.czname;}
if(d.czname == "Fresno" && FR == 0) {
FR = FR+1; return d.czname;}
if(d.czname == "Bakersfield" && BA == 0) {
BA = BA+1; return d.czname;}
if(d.czname == "Sacramento" && SA == 0) {
SA = SA+1; return d.czname;}
})
.attr('class', 'legend')
.attr('transform', function(d, i) {
var height = 0;
var horiz = -4 *i* legendRectSize+500;
var vert = height + 760;
return 'translate(' + horiz + ',' + vert + ')';
});
legend.append('rect')
.attr('width', 8)
.attr('height', 8)
.style('fill', function(d) {return color(d.czname)});
legend.append('text')
.attr('x', 10)
.attr('y', 6.5)
.text(function(d) { return d.czname; });
});
function cross(a, b) {
var c = [], n = a.length, m = b.length, i, 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;
}
</script>
name tier_name czname female par_median k_median par_rank k_rank
Academy Of Art University Four-year for-profit San Francisco 0.416753161029681 92300 27400 0.649209833337544 0.5160591456977421
"American Career College of Los Angeles, CA" Two-year for-profit Los Angeles 0.8001053124424927 30400 25700 0.3185618951372836 0.4726860146617291
Argosy University Four-year for-profit Los Angeles 0.92729150843792 84000 29700 0.6218943963732914 0.5155229324961133
Brooks Institute Four-year for-profit Santa Barbara 0.3819508519624274 95300 30500 0.6583324338828464 0.5114270798303362
California Culinary Academy Two-year for-profit San Francisco 0.3078627253507639 66000 27900 0.5842344386753066 0.5106365826872247
Carrington College California Two-year for-profit Sacramento 0.8988514777571207 47900 25000 0.4347629417895073 0.4631066941399956
Crimson Technical College Two-year for-profit Los Angeles 0.108906543192525 44700 37600 0.3633160156486238 0.6147344944715039
Fashion Institute Of Design & Merchandising Four-year for-profit Los Angeles 0.892617263374475 76300 28200 0.5923046826247507 0.5161627988491134
Heald College Two-year for-profit Fresno 0.4946527607774768 52400 33600 0.4619511949377839 0.5402855597995668
International Career Development Center Two-year for-profit Los Angeles 0.775912364343776 29500 16700 0.2551170636761657 0.3817265140367473
"Le Cordon Bleu College Of Culinary Arts of Pasadena, CA" Two-year for-profit Los Angeles 0.3806073365332205 64800 27600 0.5303953059866459 0.5014652544275869
MTI College Two-year for-profit Sacramento 0.6161883107808402 48400 32300 0.4700984500045521 0.4787168268077119
Musicians Institute Four-year for-profit Los Angeles 0.1887864792013786 77400 15500 0.5855237452500736 0.440847484072979
"Platt College of Alhambra, CA" Four-year for-profit Los Angeles 0.3746924792045734 48500 24500 0.4095126723366039 0.484363771423391
San Joaquin Valley College Two-year for-profit Fresno 0.7504147293086743 36000 21500 0.3472364632446089 0.4524976251190155
Santa Barbara Business College Four-year for-profit Bakersfield 0.8116468722616249 39400 17100 0.3786322692279842 0.4278630141970465
United Education Institute Two-year for-profit Los Angeles 0.7405865542025696 29600 19100 0.2862867550532705 0.4155954733213641
"Universal Technical Institute of Rancho Cucamonga, CA" Two-year for-profit Los Angeles 0.0194122445283669 62900 41300 0.5187221638286105 0.6134950375212578
Westwood College - Los Angeles Four-year for-profit Los Angeles 0.3929701485083066 37800 26400 0.3712689422323067 0.4951129463905785
Westwood College - South Bay Four-year for-profit Los Angeles 0.3711052261144525 39500 19000 0.3744360685487348 0.4730588681636837
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment