D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
emagee
Full window
Github gist
US chocolate imports (and why must this title text be so bloody big?)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <title>SVG, CSV, ETC</title> <style> body { background-color: burlywood; font-family: helvetica, sans-serif; } svg { background-color: ivory; margin: 12px 0 4px 12px; border-radius: 25px; } .bar { fill: sienna; } h1 { margin: 24px 0 0 32px; font-weight: normal; font-size: 22px; color: sienna; } .intro { padding: 0; margin: 10px 0 0px 1px; font-size: 14px; color: black; } .labels { fill: black; font-size: 12px; text-anchor: end } .values { fill: sienna; font-size: 12px; } .source { width: 630px; margin: 0px 0 0 32px; font-weight: normal; font-style: italic; font-size: 12px; color: black; text-align: right; } </style> <script type="text/javascript" src="https://d3js.org/d3.v3.min.js" charset="utf-8"></script> </head> <body> <script> /* The addCommas function is from https://www.mredkj.com/javascript/nfbasic.html. It's used to format the dollar amounts in the titles/tooltips. */ function addCommas(nStr) { nStr += ''; x = nStr.split('.'); x1 = x[0]; x2 = x.length > 1 ? '.' + x[1] : ''; var rgx = /(\d+)(\d{3})/; while (rgx.test(x1)) { x1 = x1.replace(rgx, '$1' + ',' + '$2'); } return x1 + x2; } // sizing up display area var margin = {top: 30, right: 0, bottom: 0, left: 100} var width = 780 - margin.left - margin.right; var height = 258 - margin.top - margin.bottom; // adding title and subtitle // did not want to nest subtitle in the title, but too lazy to fix it d3.select("body") .append("h1") .text("US chocolate imports, 2013, in millions of dollars") .append("p") .attr("class", "intro") .text("Import values of chocolate entering US ports and their origin of shipment"); var svg = d3.select("body") .append("svg") .attr("width", width) .attr("height", height); // the main event . . . importing and charting the data d3.csv("cocoa2_3-final.csv", function(data) { data.sort(function(a,b) { // thank you for the hint about forcing the strings! return d3.ascending(+a["2013"], +b["2013"]); }); var bars = svg.selectAll("rect") .data(data) .enter() .append("rect"); bars.attr("x", 110) .attr("y", function(d, i) { return i * 26 + 13; }) .attr ("width", function (d,i) { return d[2013] / 2; }) .attr("height", 20) .attr("class", "bar") .append("title") .text(function (d) { var dollarAmount = d[2013] * 1000000; dollarAmount = addCommas(dollarAmount); return "$" + dollarAmount + " of chocolate was imported from " + d["Source"] + "."; }); // adding labels to the left of the bars svg.selectAll("text") .data(data) .enter() .append("text") .text(function(d){ return d.Source; }) .attr("class", "title") .attr("x", 100) .attr("y", function(d, i){ return i * 26 + 28; }) .attr({ "fill": "black", "font-family": "sans-serif", "font-size": "12px", "text-anchor": "end" }); // adding values to the right of the bars var values = svg.selectAll(".values") .data(data) .enter() .append("text"); values.attr("x", function(d) { return d[2013]/2 + 115; }) .attr("y", function(d, i) { return i * 26 + 28; }) .attr("class","values") .text(function(d) { return d[2013]; }); }); // source line under chart // the commented-out lines were part of an aborted attempt // to try to add a link to the SVG text d3.select("body") .append("p") .attr("class", "source") //.append("a") //.attr("xmlns", "https://www.w3.org/2000/svg") //.attr(":xlink", "https://www.w3.org/1999/xlink") //.attr("version","1.1") .text("Source: USDA, www.fas.usda.gov/gats"); </script> </body> </html>
Modified
http://d3js.org/d3.v3.min.js
to a secure url
https://d3js.org/d3.v3.min.js