D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
micahstubbs
Full window
Github gist
http://jsfiddle.net/gx8zLwma/
<!DOCTYPE html> <html> <meta charset="utf-8"> <body> <div id="chart"> <svg width = "960px" height = "500px" style="border: 0.5px solid #ccc;"> </svg> </div> <script src="https://d3js.org/d3.v3.min.js"></script> <script> var data = [ {month: 1, high:56.39, low:45.61, precipitations:4.39}, {month: 2, high:59.83, low:48, precipitations:3.87}, {month: 3, high:61.65, low:48.84, precipitations:2.79}, {month: 4, high:62.97, low:49.8, precipitations:1.37}, {month: 5, high:64, low:51.26, precipitations:0.56}, {month: 6, high:66.13, low:52.77, precipitations:0.15}, {month: 7, high:65.71, low:53.68, precipitations:0.02}, {month: 8, high:66.58, low:54.45, precipitations:0.06}, {month: 9, high:69.87, low:55.57, precipitations:0.23}, {month: 10, high:69.26, low:54.35, precipitations:1.02}, {month: 11, high:63.7, low:50.97, precipitations:2.59}, {month: 12, high:57.35, low:46.9, precipitations:4.06} ]; var margin = {top: 20, right: 20, bottom: 30, left: 40}, width = 960 - margin.left - margin.right, height = 500 - margin.top - margin.bottom; var x = d3.scale.linear() .range([0, width]); var y = d3.scale.linear() .range([height, 0]); var color = d3.scale.category10(); var xAxis = d3.svg.axis() .scale(x) .orient("bottom"); var yAxis = d3.svg.axis() .scale(y) .orient("left"); var svg = d3.select("svg") .attr("width", width + margin.left + margin.right) .attr("height", height + margin.top + margin.bottom) .append("g") .attr("transform", "translate(" + margin.left + "," + margin.top + ")"); data.forEach(function(d) { d.high = +d.high; d.precipitations = +d.precipitations; }); x.domain([d3.min(data, function(d) { return d.low; }), d3.max(data, function(d) { return d.high; })] ).nice(); y.domain( [d3.min(data, function(d) { return d.precipitations; }), d3.max(data, function(d) { return d.precipitations; })] ).nice(); svg.append("g") .attr("class", "x axis") .attr("transform", "translate(0," + height + ")") .call(xAxis) .append("text") .attr("class", "label") .attr("x", width) .attr("y", -6) .style("text-anchor", "end") .text("high"); svg.append("g") .attr("class", "y axis") .call(yAxis) .append("text") .attr("class", "label") .attr("transform", "rotate(-90)") .attr("y", 6) .attr("dy", ".3em") .style("text-anchor", "end") .text("precipitations (in)") svg.select(".x path") .attr("fill", "none"); svg.select(".y path") .attr("fill", "none"); svg.selectAll(".dot") .data(data) .enter().append("circle") .attr("class", "dot") .attr("r", 5) .attr("cx", function(d) { return x(d.high); }) .attr("cy", function(d) { return y(d.precipitations); }) .style("fill", "orange"); svg.selectAll(".box") .data(data) .enter().append("rect") .attr("class", "box") .attr("height", 2) .attr("width", 2) .attr("x", function(d) { return x(d.low); }) .attr("y", function(d) { return y(d.precipitations); }) .style("fill", "blue"); 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", width - 18) .attr("width", 18) .attr("height", 18) .style("fill", color); legend.append("text") .attr("x", width - 24) .attr("y", 9) .attr("dy", ".35em") .style("text-anchor", "end") .text(function(d) { return d; }); </script> </body> </html>
https://d3js.org/d3.v3.min.js