D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
kforeman
Full window
Github gist
Labeled scatter
<!DOCTYPE html> <html> <head> <!-- header/title stuff --> <meta http-equiv='Content-Type' content='text/html;charset=utf-8'> <title>d3 test</title> <!-- load in d3 --> <script src="https://d3js.org/d3.v3.min.js" charset="utf-8"></script> <!-- load in labeler plugin --> <script src="labeler.js" charset="utf-8"></script> <!-- styling --> <style> body { font: 10px sans-serif; } .axis path, .axis line { fill: none; stroke: #000; shape-rendering: crispEdges; } .dot { stroke: #000; opacity: 0.7; } </style> </head> <body> <div id='chart'></div> <script type="text/javascript"> var margin = {top: 20, right: 20, bottom: 40, left: 40}, width = 560 - margin.left - margin.right, height = 560 - margin.top - margin.bottom; var x = d3.scale.linear() .range([0, width]); var y = d3.scale.linear() .range([height, 0]); var size = d3.scale.sqrt() .range([10,30]); var color = d3.scale.category10(); var xAxis = d3.svg.axis() .scale(x) .tickFormat(d3.format(".0%")) .orient("bottom"); var yAxis = d3.svg.axis() .scale(y) .tickFormat(d3.format(".0%")) .orient("left"); var svg = d3.select("body").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 + ")"); d3.csv('data.csv', function(data) { data.forEach(function(d) { d.rate1990 = +d.rate1990; d.rate2015 = +d.rate2015; d.rate2040 = +d.rate2040; d.change1 = (Math.log(d.rate2015) - Math.log(d.rate1990)) / (2015-1990); d.change2 = (Math.log(d.rate2040) - Math.log(d.rate2015)) / (2040-2015); d.group = d.cause.substr(0,1); }); var xe = d3.extent(data, function(d) { return d.change1; }), ye = d3.extent(data, function(d) { return d.change2; }), mn = d3.min([xe[0],ye[0]]), mx = d3.max([xe[1],ye[1]]); mn *= (mn > 0 ? .9 : 1.11); mx *= (mn < 0 ? .9 : 1.11); x.domain([mn,mx]).nice(); y.domain([mn,mx]).nice(); size.domain(d3.extent(data, function(d) { return d.rate2015; })); svg.append('line') .attr('x1', x.range()[0]) .attr('y1', y.range()[0]) .attr('x2', x.range()[1]) .attr('y2', y.range()[1]) .style('stroke', 'black'); svg.append("g") .attr("class", "x axis") .attr("transform", "translate(0," + height + ")") .call(xAxis) .append("text") .attr("class", "axis-label") .attr("x", (margin.left + (width - margin.left - margin.right)/2)) .attr("y", margin.bottom) .style("text-anchor", "middle") .text("Rate of change 1990-2015"); svg.append("g") .attr("class", "y axis") .call(yAxis) .append("text") .attr("class", "axis-label") .attr("transform", "translate("+(-1*margin.left)+"," +(margin.top + (height - margin.top - margin.bottom)/2) +")rotate(-90)") .attr("dy", ".71em") .style("text-anchor", "middle") .text("Rate of change 2015-2040") svg.selectAll(".dot") .data(data) .enter().append("circle") .attr("class", "dot") .attr("r", function(d) { return size(d.rate2015); }) .attr("cx", function(d) { return x(d.change1); }) .attr("cy", function(d) { return y(d.change2); }) .style("fill", function(d) { return color(d.group); }); var legend = svg.selectAll(".legend") .data(color.domain()) .enter().append("g") .attr("class", "legend") .attr("transform", function(d, i) { return "translate(0," + i * 20 + ")"; }); legend.append("rect") .attr("x", 6) .attr("width", 18) .attr("height", 18) .style("fill", color); legend.append("text") .attr("x", 32) .attr("y", 9) .attr("dy", ".35em") .text(function(d) { return d; }); var label_array = data.map(function(d) { return { 'x': x(d.change1), 'y': y(d.change2), 'name': d.cause }; }); var anchor_array = data.map(function(d) { return { 'x': x(d.change1), 'y': y(d.change2), 'r': size(d.rate2015)*1.05 }; }); // Draw labels labels = svg.selectAll(".label") .data(label_array) .enter() .append("text") .attr("class", "label") .attr('text-anchor', 'start') .text(function(d) { return d.name; }) .attr("x", function(d) { return (d.x); }) .attr("y", function(d) { return (d.y); }) .attr("fill", "black"); var index = 0; labels.each(function() { label_array[index].width = this.getBBox().width; label_array[index].height = this.getBBox().height; index += 1; }); // Setup labels var sim_ann = d3.labeler() .label(label_array) .anchor(anchor_array) .width(width) .height(height) .start(1000); labels .transition() .duration(300) .attr("x", function(d) { return (d.x); }) .attr("y", function(d) { return (d.y); }); }); </script> </body> </html>
https://d3js.org/d3.v3.min.js