D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
jalapic
Full window
Github gist
Scatterplot, transition + diagonal line
<!DOCTYPE html> <head> <meta charset="utf-8"> <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script> <title>scatterplot|transition</title> <style type="text/css"> h1{ font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 26px; font-style: bold; } body { background-color: #F9FFFF; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16px; font-style: bold; } svg { background-color: #F9FFFF; } .mycirc { stroke-width: 2; stroke: #411dae; fill: #3adddb; } .mycirc:hover { stroke: #170a3d; fill: #181a8f; } .axis path, .axis line { fill: none; stroke: black; shape-rendering: crispEdges; } .axis text { font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-style: bold; font-size: 11px; } </style> <h1>Stop and Frisk Totals by Race for NYC in 2014 </h1> </head> <body> <p>Original data available at <a href="https://www.nyc.gov/html/nypd/html/analysis_and_planning/stop_question_and_frisk_report.shtml">NYC.gov</a>.</p> <script type="text/javascript"> var w = 700; var h = 500; var padding = [ 20, 20, 50, 80 ]; //Top, right, bottom, left // scale x data linearly var xScale = d3.scale.linear() .range([ padding[3], w - padding[1]]) ; // scale y data ordinally var yScale = d3.scale.linear() .range([ padding[0], h - padding[2] ]); // add x-axis var xAxis = d3.svg.axis() .scale(xScale) .orient("bottom") // .tickFormat(d3.format("d")); //remove comma ; // add y-axis var yAxis = d3.svg.axis() .scale(yScale) .orient("left"); var svg = d3.select("body") .append("svg") .attr("width", w) .attr("height", h); d3.csv("stopfrisksum.txt", function(data) { // inform domain to widthScale - x data xScale.domain([ 0, d3.max(data, function(d) { return +d.Brooklyn; }) ]).nice(); // inform domain to heightScale - y axis yScale.domain([ d3.max(data, function(d) { return +d.Queens; }) ,0 ]) .nice(); var circles = svg.selectAll("circle") .data(data) .enter() .append("circle"); // initial characterization of circles circles.attr("class", "mycirc") .attr("cx", function(d){return xScale(d.Brooklyn);}) .attr("cy", function(d) {return yScale(d.Queens);}) .attr("r", .1) .append("title") .text(function(d) { return d.race + " individuals were stopped and frisked " + d.total + " times in NYC in 2014"; }); // transition entrance of cicles circles.sort(function(a, b) { return d3.ascending(+a.total, +b.total); }) .transition() .delay(function(d, i) { return i * 250; }) .duration(2000) .attr("r", 5); // add x-axis svg.append("g") .attr("class", "x axis") .attr("transform", "translate(0," + (h - padding[2]) + ")") .call(xAxis); // add y-axis svg.append("g") .attr("class", "y axis") .attr("transform", "translate(" + padding[3] + ",0)") .call(yAxis); // add diagonal line svg.append("line") .attr("x1", xScale(0)) .attr("y1", yScale(0)) .attr("x2", xScale(6000)) .attr("y2", yScale(6000)) .attr("stroke-width", 2) .attr("stroke", "red") .attr("stroke-dasharray", "5,5") //style of svg-line ; // text label for the x axis svg.append("text") .attr("x", (w + padding[3]) /2 ) .attr("y", h - (padding[3])/7) .attr("style","font-size:16px; font-weight: bold;") // to bold title .style("text-anchor", "middle") .text("Brooklyn Total"); // Add the text label for the Y axis svg.append("text") .attr("transform", "rotate(-90)") .attr("y", padding[3]/3) .attr("x", (-h+padding[0])/2) .attr("style","font-size:16px; font-weight: bold;") // to bold title .style("text-anchor", "middle") .text("Queens Total"); }); </script> </body> </html>
https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js