D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
35degrees
Full window
Github gist
Multiple movie legs
Built with
blockbuilder.org
<!DOCTYPE html> <head> <meta charset="utf-8"> <link href='https://fonts.googleapis.com/css?family=Cabin:300,400' rel='stylesheet' type='text/css'> <script src="https://d3js.org/d3.v4.min.js"></script> <style> .line { fill: none; stroke: Orange; stroke-width: 2px; } .axis { font-family: Cabin; font-size: 12px; } text.shadow { stroke: white; stroke-width: 4px; opacity: 0.9 } .grid line { stroke: lightgrey; stroke-opacity: 0.6; shape-rendering: crispEdges } .grid path { stroke-width: 0; } </style> </head> <body> <script> var margin = {top: 30, right: 50, bottom: 350, left: 70}, width = 960 - margin.left - margin.right, height = 750 - margin.top - margin.bottom; // parse the date / time var parseTime = d3.timeParse("%d-%b-%y"); // set the ranges var x = d3.scaleTime().range([0, width]); var y = d3.scaleLinear().range([height, 0]); // define the line var valueline = d3.line() .x(function(d) { return x(d.date); }) .y(function(d) { return y(d.titanic); }); var valueline2 = d3.line() .x(function(d) { return x(d.date); }) .y(function(d) { return y(d.avatar); }); var valueline3 = d3.line() .x(function(d) { return x(d.date); }) .y(function(d) { return y(d.frozen); }); var valueline4 = d3.line() .x(function(d) { return x(d.date); }) .y(function(d) { return y(d.forceawakens); }); // append the svg obgect to the body of the page // appends a 'group' element to 'svg' // moves the 'group' element to the top left margin 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 + ")"); function make_x_gridlines() { return d3.axisBottom(x) .ticks(10) } function make_y_gridlines() { return d3.axisLeft(y) .ticks(5) } // Get the data d3.tsv("oldtitava.tsv", function(error, data) { if (error) throw error; // format the data data.forEach(function(d) { d.date = parseTime(d.date); d.titanic = +d.titanic; d.avatar = +d.avatar; d.frozen = +d.frozen; d.forceawakens = +d.forceawakens; }); // Scale the range of the data x.domain(d3.extent(data, function(d) { return d.date; })); y.domain([0, d3.max(data, function(d) { return Math.max(d.titanic, d.avatar, d.frozen, d.forceawakens); })]); svg.append("g") .attr("class","grid") .attr("transform","translate(0," + height + ")") .style("stroke-dasharray",("3,3")) .call(make_x_gridlines() .tickSize(-height) .tickFormat("") ) svg.append("g") .attr("class","grid") .style("stroke-dasharray",("3,3")) .call(make_y_gridlines() .tickSize(-width) .tickFormat("") ) // Add the valueline path. svg.append("path") .data([data]) .attr("class", "line") .attr("d", valueline); svg.append("path") .data([data]) .attr("class", "line") .style("stroke","IndianRed") .attr("d", valueline2); svg.append("path") .data([data]) .attr("class", "line") .style("stroke","SteelBlue") .attr("d", valueline3); svg.append("path") .data([data]) .attr("class", "line") .style("stroke","LimeGreen") .attr("d", valueline4); // Add the X Axis svg.append("g") .attr("class","axis") .attr("transform", "translate(0," + height + ")") .call(d3.axisBottom(x).ticks(12).tickFormat(d3.timeFormat("%Y-%d-%b"))) .selectAll("text") .style("text-anchor","end") .attr("dx","-.8em") .attr("dy",".15em") .attr("transform","rotate(-45)"); svg.append("text") .attr("transform","translate(" + (width/2) + "," + (height + margin.top + 50) + ")") .style("text-anchor", "left") .style("font","14px Cabin") .style("font-weight","bolder") .text("Week"); // Add the Y Axis svg.append("g") .attr("class","axis") .call(d3.axisLeft(y)); svg.append("text") .attr("transform","rotate(-90)") .attr("y",0-margin.left) .attr("x",0-height/2) .attr("dy","2em") .attr("text-anchor","middle") .style("font","14px Cabin") .text("Weekly percent of total domestic gross (%)"); svg.append("text") .attr("transform","translate(" + (width/2) + "," + (0 - margin.top/2) + ")") .attr("text-anchor","middle") .style("text-decoration","underline") .text("Titanic weekly gross as pct of total") svg.append("text") .attr("transform", "translate(" + (10) + "," + (y(data[0].titanic)-5) + ")") .attr("dy",".35em") .attr('text-anchor',"start") .attr("fill","Orange") .style("font-family","Cabin") .attr("class","shadow") .text("Titanic"); svg.append("text") .attr("transform", "translate(" + (10) + "," + (y(data[0].titanic)-5) + ")") .attr("dy",".35em") .attr('text-anchor',"start") .attr("fill","Orange") .style("font-family","Cabin") .text("Titanic"); svg.append("text") .attr("transform", "translate(" + (10) + "," + (y(data[0].avatar)-23) + ")") .attr("dy",".35em") .attr('text-anchor',"start") .attr("fill","IndianRed") .style("font-family","Cabin") .text("Avatar"); svg.append("text") .attr("transform", "translate(" + (10) + "," + y(data[0].frozen) + ")") .attr("dy",".25em") .attr('text-anchor',"start") .attr("fill","SteelBlue") .style("font-family","Cabin") .text("Frozen"); svg.append("text") .attr("transform", "translate(" + (10) + "," + (y(data[0].forceawakens)+7) + ")") .attr("dy",".25em") .attr('text-anchor',"start") .attr("fill","LimeGreen") .style("font-family","Cabin") .text("The Force Awakens"); }); </script> </body>
https://d3js.org/d3.v4.min.js