D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
clhenrick
Full window
Github gist
Barley Punchcard - Static
forked from
syntagmatic
's block:
Barley Punchcard
<!doctype html> <script src="https://d3js.org/d3.v4.min.js"></script> <style type="text/css"> body { font-family: sans-serif; } svg { border:1px solid #d0d0d0; } text { font-size: 16px; fill: #888; } path, line { stroke: #888; } </style> <body> </body> <script> // properties var margin = {top: 20, right: 30, bottom: 30, left: 120}; var width = 800 - margin.left - margin.right; var height = 600 - margin.top - margin.bottom; var yscale = d3.scalePoint(); var xscale = d3.scaleLinear(); var radius = d3.scaleSqrt(); var color = d3.scaleOrdinal(d3.schemeCategory20b); d3.csv('barleyfull.csv', function(err, data) { if (err) { throw error; } data.forEach(function(d) { d.yield = +d.yield; d.year = +d.year; }); var nested = d3.nest() .key(function(d) { return d.site; }) .key(function(d) { return d.gen; }) .entries(data); console.log(nested); var site = nested[4]; yscale .range([0, height]) .domain(site.values.map(function(d) { return d.key; })) .round(true); xscale .range([0, width]) .domain([1927, 1936]); radius .range([0, 15]) .domain([0, d3.max(data, function(d) { return d.yield; }) ]); var yaxis = d3.axisLeft() .scale(yscale); var xaxis = d3.axisBottom() .tickFormat(function(d) { return d; }) .scale(xscale); var chart = d3.select("body") .append("div") .datum(site); chart.append("h2") .text(function(d) { return d.key; }); var svg = 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] + ')'); svg.append("g") .call(yaxis); svg.append("g") .attr("transform", "translate(0," + height + ")") .call(xaxis); var rows = svg.selectAll("g.row") .data(function(d) { return d.values; }) .enter().append("g") .attr("class", function(d) { return d.key + " row"; }) .attr("transform", function(d) { return "translate(0," + yscale(d.key) + ")" }); rows.selectAll("circle") .data(function(d) { return d.values; }) .enter().append("circle") .attr("r", function(d) { return radius(d.yield); }) .attr("cy", 0) .attr("cx", function(d) { return xscale(d.year); }) .attr("fill", function(d) { return color(d.gen); }); }); </script>
https://d3js.org/d3.v4.min.js