D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
mbostock
Full window
Github gist
Diamonds
<!DOCTYPE html> <meta charset="utf-8"> <style> canvas, svg { position: absolute; } .grid .tick line { stroke: #fff; } .grid--x .domain { fill: #e7e7e7; stroke: none; } .grid--y .domain, .axis .domain { display: none; } </style> <svg width="960" height="960"></svg> <canvas width="960" height="960"></canvas> <script src="//d3js.org/d3.v4.0.0-alpha.49.min.js"></script> <script> var canvas = d3.select("canvas").node(), context = canvas.getContext("2d"); var margin = {top: 20, right: 20, bottom: 30, left: 40}, width = canvas.width - margin.left - margin.right, height = canvas.height - margin.top - margin.bottom; var svg = d3.select("svg").append("g") .attr("transform", "translate(" + margin.left + "," + margin.top + ")"); var x = d3.scaleLinear() .rangeRound([0, width - 2]); var y = d3.scaleLinear() .rangeRound([height - 2, 0]); context.translate(margin.left, margin.top); context.globalCompositeOperation = "multiply"; context.fillStyle = "rgba(60,180,240,0.6)"; d3.tsv("diamonds.tsv", type, function(error, diamonds) { if (error) throw error; x.domain(d3.extent(diamonds, function(d) { return d.carat; })); y.domain(d3.extent(diamonds, function(d) { return d.price; })); svg.append("g") .attr("class", "grid grid--x") .call(d3.axisLeft(y) .tickSize(-width) .tickFormat("")); svg.append("g") .attr("class", "grid grid--y") .attr("transform", "translate(0," + height + ")") .call(d3.axisBottom(x) .tickSize(-height) .tickFormat("")); svg.append("g") .attr("class", "axis axis--y") .call(d3.axisLeft(y) .ticks(10, "s")) .append("text") .attr("x", 10) .attr("y", 10) .attr("dy", ".71em") .attr("fill", "#000") .attr("font-weight", "bold") .attr("text-anchor", "start") .text("Price (US$)"); svg.append("g") .attr("class", "axis axis--x") .attr("transform", "translate(0," + height + ")") .call(d3.axisBottom(x)) .append("text") .attr("x", width - 10) .attr("y", -10) .attr("dy", "-.35em") .attr("fill", "#000") .attr("font-weight", "bold") .attr("text-anchor", "end") .text("Mass (carats)"); d3.shuffle(diamonds); var t = d3.timer(function() { for (var i = 0, n = 500, d; i < n; ++i) { if (!(d = diamonds.pop())) return t.stop(); context.fillRect(x(d.carat), y(d.price), Math.max(2, x(d.carat + 0.01) - x(d.carat)), 2); } }); }); function type(d) { d.carat = +d.carat; d.price = +d.price; return d; } </script>
https://d3js.org/d3.v4.0.0-alpha.49.min.js