D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
bricedev
Full window
Github gist
Wars
Data from
emeeks' blocks
.
<!DOCTYPE html> <meta charset="utf-8"> <style> body { font: 10px sans-serif; } .axis path, .axis line { fill: none; stroke: #000; shape-rendering: crispEdges; } .y.axis line { stroke: #777; stroke-dasharray: 2,2; } </style> <body> <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script> <script> var margin = {top: 20, right: 20, bottom: 20, left: 0}, width = 960 - margin.left - margin.right, height = 500 - margin.top - margin.bottom; var parseDate = d3.time.format("%d/%m/%Y").parse; var x = d3.time.scale() .range([0, width - 50]); var y = d3.scale.sqrt() .range([height, 0]); var xAxis = d3.svg.axis() .scale(x) .tickSize(6, 0) .orient("bottom"); var yAxis = d3.svg.axis() .scale(y) .tickSize(30, 0) .orient("left"); var color = d3.scale.ordinal() .range(["#bc80bd","#8dd3c7","#fdb462","#fb8072","#80b1d3"]); var diag = d3.svg.diagonal() .source(function(d) { return {x: x(d.start),y: y(0)}; }) .target(function(d) { return {x: x(d.end),y: y(d.duration)}; }); 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(error, data) { if (error) throw error; data.forEach(function(d) { d.start = parseDate(d.start); d.end = parseDate(d.end); d.duration = (d.end - d.start)/(1000*24*60*60*31*12); }); x.domain([d3.min(data, function(d) { return d.start; }),d3.max(data, function(d) { return d.end; })]).nice(); y.domain([0,d3.max(data, function(d) { return d.duration; })]); var war = svg.selectAll(".war") .data(data) .enter().append("g") .attr("class","war"); war.append("path") .attr("class","curves") .attr("d", diag) .attr("fill","none") .attr("stroke-width",2) .attr("stroke",function(d) { return color(d.sphere); }); war.append("circle") .attr("class","circle") .attr("r", function(d) { return 5; }) .attr("cx",function(d) { return x(d.end)}) .attr("cy",function(d) { return y(d.duration)}) .attr("fill",function(d) { return color(d.sphere);}) .attr("stroke","white") .attr("stroke-width",1.5); svg.append("g") .attr("class", "y axis") .attr("transform", "translate(" + width + ",0)") .call(yAxis) .append("text") .attr("x", 0) .attr("y", 10) .attr("transform", "rotate(-90)") .style("text-anchor", "end") .style("font-weight","bold") .text("Duration (years)"); svg.append("g") .attr("class", "x axis") .attr("transform", "translate(0," + height + ")") .call(xAxis); }); </script>
https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js