D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
guilhermesimoes
Full window
Github gist
D3.js: Synchronously animating multiple paths in a multi-series line chart
<!DOCTYPE html> <meta charset="utf-8"> <style> body { font: 12px Helvetica; } .axis line { fill: none; stroke: #000; shape-rendering: crispEdges; } .x.axis path { display: none; } .line { fill: none; stroke: steelblue; stroke-width: 2px; } .overlay { fill: none; stroke: none; } </style> <body> <div class="js-chart-container"> <script src="https://d3js.org/d3.v3.js"></script> <script type="text/javascript"> "use strict"; /*global d3*/ var margin = {top: 10, right: 20, bottom: 40, left: 60}, containerWidth = document.querySelector(".js-chart-container").clientWidth, containerHeight = 500, width = containerWidth - margin.left - margin.right, height = containerHeight - margin.top - margin.bottom; var parseDate = d3.time.format("%Y-%m-%d").parse; var x = d3.time.scale() .range([0, width]); var y = d3.scale.linear() .range([height, 0]); var color = d3.scale.category10(); var xAxis = d3.svg.axis() .scale(x) .ticks(5) .innerTickSize(15) .outerTickSize(0) .orient("bottom"); var yAxis = d3.svg.axis() .scale(y) .tickFormat(function(d) { return d + "%";}) .ticks(5) .innerTickSize(15) .outerTickSize(0) .orient("left"); var line = d3.svg.line() .interpolate("basis") .x(function(d) { return x(d.date); }) .y(function(d) { return y(d.price); }); 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.tsv("airbus_data.tsv", function(error, data) { color.domain(d3.keys(data[0]).filter(function(key) { return key !== "date"; })); data.forEach(function(d) { d.date = parseDate(d.date); }); var companiesData = color.domain().map(function(name) { return { name: name, values: data.map(function(d) { return {date: d.date, price: +d[name]}; }) }; }); x.domain(d3.extent(data, function(d) { return d.date; })); y.domain([ d3.min(companiesData, function(c) { return d3.min(c.values, function(v) { return v.price; }); }), d3.max(companiesData, function(c) { return d3.max(c.values, function(v) { return v.price; }); }) ]); svg.append("g") .attr("class", "x axis") .attr("transform", "translate(0," + height + ")") .call(xAxis); svg.append("g") .attr("class", "y axis") .call(yAxis); svg.append("line") .attr({ "class":"horizontalGrid", "x1" : 0, "x2" : width, "y1" : y(0), "y2" : y(0), "fill" : "none", "shape-rendering" : "crispEdges", "stroke" : "black", "stroke-width" : "1px", "stroke-dasharray": ("3, 3") }); var overlay = svg.append("defs").append("clipPath") .attr("id", "overlay") .append("rect") .attr("x", 0) .attr("y", 0) .attr("width", 0) .attr("height", height); var container = svg.append("g") .attr("class", ".container") .attr("clip-path", "url(#overlay)"); var companies = container.selectAll(".company") .data(companiesData) .enter().append("g") .attr("class", "company"); var path = companies.append("path") .attr("class", "line") .attr("d", function(d) { return line(d.values); }) .style("stroke", function(d) { if (d.name === "Airbus") { return "rgb(000,255,000)"; } else { return "#000"; } }); overlay.transition() .duration(5000) .ease("linear") .attr("width", width); }); </script> </body>
Modified
http://d3js.org/d3.v3.js
to a secure url
https://d3js.org/d3.v3.js