D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
scotthmurray
Full window
Github gist
Visitors to NZ National Parks
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>NZ National Parks</title> <script type="text/javascript" src="https://d3js.org/d3.v3.js"></script> <link href="natparks.css" rel="stylesheet" type="text/css"/> </head> <body> <h1>National Park visitorship in the land of the long white cloud</h1> <p>They have some superb National Parks over there. You should go sometime.</p> <!-- <div id="legend"></div> --> <div class="clear"></div> <script type="text/javascript"> //Dimensions and padding var w = 700; var h = 600; 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") .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.amount); }); //Create the empty SVG image var svg = d3.select("body") .append("svg") .attr("width", w) .attr("height", h); //Load data d3.csv("NZNationalParks.csv", function(data) { //This was a really good way of making an array of years when I had the data in its original format. Unfortunately, I've now transposed it /*var years = []; data.map(function(d) { years.push(d.calendarYear); });*/ var years = d3.keys(data[0]).filter(function(head) { return head != "NP"; }); //console.log(years); //Create a new, empty array to hold our restructured dataset var dataset = []; //Loop once for each row in data for (var i = 0; i < data.length; i++) { //Create new object with this country's name and empty array dataset[i] = { park: data[i].NP, visitors: [] }; //Loop through all the years for (var j = 0; j < years.length; j++) { // If value is not empty if (data[i][years[j]]) { //Add a new object to the visitors data array //for this country dataset[i].visitors.push({ year: years[j], amount: data[i][years[j]] }); } } } //Uncomment to log the original data to the console //console.log(data); //Uncomment to log the newly restructured dataset to the console //console.log(dataset); //Set scale domains xScale.domain([ d3.min(years, function(d) { return dateFormat.parse(d); }), d3.max(years, function(d) { return dateFormat.parse(d); }) ]); yScale.domain([ d3.max(dataset, function(d) { return d3.max(d.visitors, function(d) { return +d.amount; }); }), 0 ]); //Make a group for each country var groups = svg.selectAll("g") .data(dataset) .enter() .append("g") .attr("class", (function(d) { return d.park; })); //Append a title with the country name (so we get easy tooltips) groups.append("title") .text(function(d) { return d.park; }); //Within each group, create a new line/path, //binding just the visitors data to each one groups.selectAll("path") .data(function(d) { return [ d.visitors ]; }) .enter() .append("path") .attr("d", line); //Label each line! groups.append("text") .attr("class", "label") .attr("x", w - padding[3] - 4) .attr("y", function(d, i) { //Get the last value in the array //(e.g., the right-most point on each line) var lastValue = d.visitors[d.visitors.length - 1].amount; //Position this label vertically, relative to that point //…making exceptions for Paparoa and Tongariro, because //they share the same value if (d.park == "Paparoa") { return yScale(lastValue) + 8; } else if (d.park == "Tongariro") { return yScale(lastValue) - 5; } else { return yScale(lastValue) + 2; } }) .text(function(d) { console.log(d); return d.park; }); //Axes svg.append("g") .attr("class", "x axis") .attr("transform", "translate(0," + (h - padding[2]) + ")") .call(xAxis); svg.append("g") .attr("class", "y axis") .attr("transform", "translate(" + (padding[3]) + ",0)") .call(yAxis); //Legend // var legend = d3.select("#legend").append("svg") // .attr("width", 300) // .attr("height", 120); // legend.selectAll("line") // .data(data) // .enter() // .append("line") // .attr("x1", 0) // .attr("x2", 20) // .attr("y1", function(d, i){return i * 20 + 5;}) // .attr("y2", function(d, i){return i * 20 + 5;}) // .attr("class", function(d){return d.NP;}); // legend.selectAll("text") // .data(data) // .enter() // .append("text") // .attr("x", 25) // .attr("y", function(d, i){return i * 20 + 8;}) // .text(function(d){return d.NP;}); }); </script> <div id="attribution"><p>Source: <a href="https://www.doc.govt.nz/about-us/our-role/managing-conservation/recreation-management/visitor-statistics-and-research/national-parks-visitor-statistics/">NZ Department of Conservation</a>, 2014</p></p></div> </body> </html>
Modified
http://d3js.org/d3.v3.js
to a secure url
https://d3js.org/d3.v3.js