D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
uafrazier
Full window
Github gist
D3: Multi-Line Chart
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>US Passports 1996-2014</title> <script type="text/javascript" src="https://d3js.org/d3.v3.js"></script> <style type="text/css"> body { background:#24263F; font-family: 'Open Sans',Helvetica,arial,sans-serif; } h1 { font-weight: 600; text-transform: uppercase; } header { color: #F4D73C; } hr { border: 2px solid #F4D73C; margin-top: 20px; margin-left: 10px; margin-right: 10px; } a { color: #F4D73C; text-decoration: underline; } svg { background: #24263F; float:left; } .axis path, .axis line { fill: none; stroke: #F4D73C; shape-rendering: crispEdges; } .axis text { font-family: sans-serif; font-size: 11px; fill: #F4D73C; } img { float:left; height: 100px; width: 100px; margin-right: 10px; margin-left: 10px; } .explanatory { color: #F4D73C; font-size: 14px; padding: 10px 25px 0 0; margin: 25px 25px 225px 88px; } .legend { text-transform: uppercase; } </style> </head> <body> <header> <img src="https://joshuafrazier.info/images/seal.png"> <h1>United States Passports</h1> <p>Applications and Issuances: 1996-2014 (Fiscal Years).<br /> Source: <a href="https://travel.state.gov/content/passports/english/passports/statistics.html">Bureau of Consular Affairs</a></p> </header> <hr> <script type="text/javascript"> //Dimensions and padding var w = 1000; var h = 500; var padding = [ 20, 10, 50, 100 ]; //Top, right, bottom, left //Set up date formatting and years var dateFormat = d3.time.format("%Y"); //Set up scales var xScale = d3.time.scale() .range([ padding[3], w - padding[1] - padding[3] ]); var yScale = d3.scale.linear() .range([ padding[0], h - padding[2] ]); //Configure axis generators var xAxis = d3.svg.axis() .scale(xScale) .orient("bottom") .ticks(20) .tickFormat(function(d) { return dateFormat(d); }); var yAxis = d3.svg.axis() .scale(yScale) .orient("left"); //Configure line generator var line = d3.svg.line() .x(function(d) { return xScale(dateFormat.parse(d.year)); }) .y(function(d) { return yScale(d.numbers); }); //Create the empty SVG image var svg = d3.select("body") .append("svg") .attr("width", w) .attr("height", h); //Load passport issued data d3.csv("passports_issued.csv", function(issuedData) { //Load passport application data d3.csv("passport_applications.csv", function(appliedData) { //Create a new array that contains both the //applied for and issued data, merged into one var mergedData = issuedData.concat(appliedData); //console.log(mergedData); //Use the newly merged data to determine the //min and max values, for setting scale domains xScale.domain([ d3.min(mergedData, function(d) { return dateFormat.parse(d.year); }), d3.max(mergedData, function(d) { return dateFormat.parse(d.year); }) ]); yScale.domain([ d3.max(mergedData, function(d) { return +d.numbers; }), 0 ]); //Lines // // Note data is wrapped in another array, so all its // values are bound to a single element (the line!) // svg.data([ issuedData ]) .append("path") .attr("class", "line issued") .attr("d", line) .attr("fill", "none") .attr("stroke", "#ff6666") .attr("stroke-width", 2); svg.data([ appliedData ]) .append("path") .attr("class", "line applied") .attr("d", line) .attr("fill", "none") .attr("stroke", "#FFFFFF") .attr("stroke-width", 2); //Axes svg.append("g") .attr("class", "x axis") .attr("transform", "translate(0," + (h - padding[2] + 10) + ")") .call(xAxis); svg.append("g") .attr("class", "y axis") .attr("transform", "translate(" + (padding[3] - 10) + ",0)") .call(yAxis); }); //End passports applied for data load function }); //End passports issued data load function //legend svg.append("text") .attr("x",w-225) .attr("y", h-125) .attr("class", "legend") .style("fill", "#FFFFFF") .text("Applications"); svg.append("text") .attr("x",w-225) .attr("y", h-100) .attr("class", "legend") .style("fill", "#ff6666") .text("Issuances"); //explanatory var para = d3.select("body") .append("p") .attr("class", "explanatory") .html("1997-2007: the number of passports applied for and issued more than tripled.<br/><br/>2001: after the World Trade Center attacks the gap between applications and issuances gets larger, possibly indicating stricter legal requirements or more stringent adjudication. <br/><br/>2007: the <a href='https://en.wikipedia.org/wiki/Western_Hemisphere_Travel_Initiative'> Western Hemisphere Travel Initiative</a> was implemented requiring travelers to show a valid passport or other approved document when traveling to the US from areas within the Western Hemisphere.<br/><br/>2008: passport cards were introduced. One application can be used for a passport book and a passport card thus allowing for the total number of issuances (books and cards) to exceed the number of applications."); </script> </body> </html>
Modified
http://d3js.org/d3.v3.js
to a secure url
https://d3js.org/d3.v3.js