D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
Pragyagarg
Full window
Github gist
Scatterplot Matrix
Built with
blockbuilder.org
<!DOCTYPE html> <meta charset="utf-8"> <style> svg { font: 10px sans-serif; padding: 10px; } .axis line { stroke: #ddd; } .axis path { display: none; } .cell text { font-weight: bold; } .frame { fill: none; stroke: #aaa; } circle { fill-opacity: .9; } </style> <body> <script src="https://d3js.org/d3.v4.min.js"></script> <script> var width = 960, size = 250, padding = 30; var x = d3.scaleLinear() .range([padding / 2, size - padding / 2]); var y = d3.scaleLinear() .range([size - padding / 2, padding / 2]); var gxScale = d3.scaleBand() .range([padding / 2, size - padding / 2]); var gyScale = d3.scaleBand() .range([size - padding / 2, padding / 2]) color = d3.scaleOrdinal(d3.schemeCategory10); d3.csv("billionaires.csv", function(error, data) { if (error) throw error; var domainByTrait = {}, traits = d3.keys(data[0]).filter(function(d) { return d == "gender" || d == "age" || d == "worth in billions"; }), n = traits.length; traits.forEach(function(trait) { if(trait == "gender"){ domainByTrait[trait] = data.map(function(d) { return d['gender']; }) }else { domainByTrait[trait] = d3.extent(data, function(d) { return +d[trait]; }); } }); 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 + ")"); // X axes 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) { if (d == "gender"){ gxScale.domain(domainByTrait[d]); d3.select(this).call(d3.axisTop(gxScale)); } else { x.domain(domainByTrait[d]); d3.select(this).call(d3.axisTop(x).ticks(5)); } }); // Y axes 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) { if (d == "gender"){ gyScale.domain(domainByTrait[d]); d3.select(this).call(d3.axisLeft(gyScale)); } else { y.domain(domainByTrait[d]); d3.select(this).call(d3.axisLeft(y).ticks(5)); } }); 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) // Diagonal Titles cell.filter(function(d) { return d.i === d.j; }).append("text") .attr("x", padding) .attr("y", padding) .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) { if(p.x == "gender"){ return gxScale(d[p.x]); }else { return x(d[p.x]); } }) .attr("cy", function(d) { if(p.y == "gender"){ return gyScale(d[p.y]); }else { return y(d[p.y]); } }) .attr("r", 2) .style("fill", function(d) { return color(d.gender); }); } 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 </body>
https://d3js.org/d3.v4.min.js