D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
jacquessham
Full window
Github gist
Assignment 4 Scatter Plot Matrix for Human Development index
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title> D3: A Scatterplot Matrix of Human Development on Selected Commonwealth Countries</title> <script type="text/javascript" src="https://d3js.org/d3.v4.min.js"></script> <style type="text/css"> .axis,.frame{shape-rendering: crispEdges;} .axis line{stroke: #ddd;} </style> </head> <body> <script type="text/javascript"> /* Reference: https://bl.ocks.org/mbostock/4063663 */ var width = 800; size = 200; padding = 20; var title = "Human Development on Selected Commonwealth Countries in 1990"; var svg = d3.select("body") .append("svg") .attr("width",width) .attr("heigth",100) .append("g") .attr('transform', 'translate(' + padding + ',' + padding + ')'); svg.append("text") .text(title) .attr("x",width/2) .attr("y",0) .style("text-anchor","middle") .style("fill","black") .style("font-size",20); // Put the title var x = d3.scaleLinear().range([padding/2, size - padding/2]); var y = d3.scaleLinear().range([size - padding/2, padding/2]); var xAxis = d3.axisBottom().scale(x).ticks(6); var yAxis = d3.axisLeft().scale(y).ticks(6); var color = d3.scaleOrdinal(d3.schemeCategory10); d3.csv("global_development2.csv", function(error,data){ if(error) throw error; var domainByTrait = {}, traits = d3.keys(data[0]).filter(function(d){return d !== "Country";}), n = traits.length; console.log(n); traits.forEach(function(trait){ domainByTrait[trait] = d3.extent(data, function(d){return d[trait];}); }) xAxis.tickSize(size*n); yAxis.tickSize(-size*n); 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); }); function cross(a, b) { var c = [], n = a.length, m = b.length; for (var i = -1; ++i < n;) for (var j = -1; ++j < m;) c.push({x: a[i], i: i, y: b[j], j: j}); return c; } function plot(p) { var cell = d3.select(this); // x.domain([0,d3.max(data, function(d){return d[p.x];})]); // y.domain([0,d3.max(data, function(d){return d[p.y];})]); x.domain([0,100]); // Question here y.domain([0,100]); 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.Country); }); } 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; }); }); </script> </body> </html>
https://d3js.org/d3.v4.min.js