D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
lloewens
Full window
Github gist
GVC Final Apparel Trade by World Region
<!DOCTYPE html> <meta charset="utf-8"> <style> body { font: 10px sans-serif; } .chart { background: #fff; } p { font: 12px helvetica; } .axis path, .axis line { fill: none; stroke: #000; stroke-width: 2px; shape-rendering: crispEdges; } button { position: absolute; right: 50px; top: 10px; } </style> <body> <script src="https://d3js.org/d3.v2.js"></script> <div id="flow-controls"> <input name="mode" type="radio" value="Exports" id="flow-exports" checked><label for="flow-exports"> Exports</label> <input name="mode" type="radio" value="Imports" id="flow-imports"><label for="flow-imports"> Imports</label> </div> <div class="chart"> </div> <script> var datearray = []; var colorrange = []; var data1 = "ExportsApparelFinal.csv"; var data2 = "ImportsApparelFinal.csv"; var margin = {top: 20, right: 40, bottom: 30, left: 30}; var width = document.body.clientWidth - margin.left - margin.right; var height = 400 - margin.top - margin.bottom; var x = d3.time.scale() .range([0, width]); var y = d3.scale.linear() .range([height-10, 0]); var format = d3.time.format("%Y"); var xAxis = d3.svg.axis() .scale(x) .orient("bottom") .ticks(d3.time.years); var yAxis = d3.svg.axis() .scale(y); var yAxisr = d3.svg.axis() .scale(y); var svg = d3.select(".chart").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 + ")"); var tooltip = d3.select("body") .append("div") .attr("class", "remove") .style("position", "absolute") .style("z-index", "20") .style("visibility", "hidden") .style("top", "30px") .style("left", "85px"); var vertical = d3.select(".chart") .append("div") .attr("class", "remove") .style("position", "absolute") .style("z-index", "19") .style("width", "1px") .style("height", "380px") .style("top", "10px") .style("bottom", "30px") .style("left", "0px") .style("background", "#fff"); svg.append("text") .attr("class", "y label") .attr("text-anchor", "end") .attr("y", 4) .attr("dy", ".75em") .attr("transform", "rotate(-90)") .text("Bil.$"); svg.append("text") .attr("x", (width / 2)) .attr("y", 0 - (margin.top/5)) .attr("text-anchor", "middle") .style("font-size", "16px") .text("Final Apparel"); d3.select("#flow-exports") .on("click", function(d,i) { console.log(data1); chart(data1, "blue") }) d3.select("#flow-imports") .on("click", function(d,i) { console.log(data2); chart(data2, "orange") }) initchart(data1,"blue"); function initchart(csvpath, color) { if (color == "blue") { colorrange = ["#034E7B", "#0570B0", "#3690C0", "#74A9CF", "#A6BDDB", "#D0D1E6", "#F1EEF6"]; } else if (color == "pink") { colorrange = ["#980043", "#DD1C77", "#DF65B0", "#C994C7", "#D4B9DA", "#F1EEF6"]; } else if (color == "orange") { colorrange = ["#990000", "#D7301F", "#EF6548", "#FC8D59", "#FDBB84", "#FDD49E", "#FEF0D9"]; } strokecolor = colorrange[0]; var z = d3.scale.ordinal() .range(colorrange); var stack = d3.layout.stack() .offset("zero") .values(function(d) { return d.values; }) .x(function(d) { return d.year; }) .y(function(d) { return d.tradevalue; }); var nest = d3.nest() .key(function(d) { return d.region; }); var area = d3.svg.area() .interpolate("cardinal") .x(function(d) { return x(d.year); }) .y0(function(d) { return y(d.y0); }) .y1(function(d) { return y(d.y0 + d.y); }); d3.csv(csvpath, function(data) { data.forEach(function(d) { d.year = format.parse(d.year.toString()); d.tradevalue = +d.tradevalue; }); var layers = stack(nest.entries(data)); x.domain(d3.extent(data, function(d) { return d.year; })); y.domain([0, d3.max(data, function(d) { return d.y0 + d.y; })]); svg.selectAll(".layer") .data(layers) .enter().append("path") .attr("class", "layer") .attr("d", function(d) { return area(d.values); }) .style("fill", function(d, i) { return z(i); }); svg.append("g") .attr("class", "x axis") .attr("transform", "translate(0," + height + ")") .call(xAxis); svg.append("g") .attr("class", "y axis") .attr("transform", "translate(" + width + ", 0)") .call(yAxisr.orient("right")); svg.append("g") .attr("class", "axis") .call(yAxis.orient("left")); svg.selectAll(".layer") .attr("opacity", 1) .on("mouseover", function(d, i) { svg.selectAll(".layer").transition() .duration(250) .attr("opacity", function(d, j) { return j != i ? 0.6 : 1; })}) .on("mousemove", function(d, i) { mousex = d3.mouse(this); mousex = mousex[0]; var invertedx = x.invert(mousex); invertedx = invertedx.getFullYear(); var selected = (d.values); for (var k = 0; k < selected.length; k++) { datearray[k] = selected[k].year datearray[k] = datearray[k].getFullYear(); } mousedate = datearray.indexOf(invertedx); pro = d.values[mousedate].tradevalue; d3.select(this) .classed("hover", true) .attr("stroke", strokecolor) .attr("stroke-width", "0.5px"), tooltip.html( "<p>" + d.key + "<br>" + "Bil.$" + pro + "</p>" ).style("visibility", "visible"); }) .on("mouseout", function(d, i) { svg.selectAll(".layer") .transition() .duration(250) .attr("opacity", "1"); d3.select(this) .classed("hover", false) .attr("stroke-width", "0px"), tooltip.html( "<p>" + d.key + "<br>" + "Bil.$" + pro + "</p>" ).style("visibility", "hidden"); }) d3.select(".chart") .on("mousemove", function(){ mousex = d3.mouse(this); mousex = mousex[0] + 5; vertical.style("left", mousex + "px" )}) .on("mouseover", function(){ mousex = d3.mouse(this); mousex = mousex[0] + 5; vertical.style("left", mousex + "px")}); }); } function chart(csvpath, color) { if (color == "blue") { colorrange = ["#034E7B", "#0570B0", "#3690C0", "#74A9CF", "#A6BDDB", "#D0D1E6", "#F1EEF6"]; } else if (color == "pink") { colorrange = ["#980043", "#DD1C77", "#DF65B0", "#C994C7", "#D4B9DA", "#F1EEF6"]; } else if (color == "orange") { colorrange = ["#990000", "#D7301F", "#EF6548", "#FC8D59", "#FDBB84", "#FDD49E", "#FEF0D9"]; } strokecolor = colorrange[0]; var z = d3.scale.ordinal() .range(colorrange); var stack = d3.layout.stack() .offset("zero") .values(function(d) { return d.values; }) .x(function(d) { return d.year; }) .y(function(d) { return d.tradevalue; }); var nest = d3.nest() .key(function(d) { return d.region; }); var area = d3.svg.area() .interpolate("cardinal") .x(function(d) { return x(d.year); }) .y0(function(d) { return y(d.y0); }) .y1(function(d) { return y(d.y0 + d.y); }); d3.csv(csvpath, function(data) { data.forEach(function(d) { d.year = format.parse(d.year.toString()); d.tradevalue = +d.tradevalue; }); var layers = stack(nest.entries(data)); d3.selectAll("path") .data(layers) .transition() .duration(2500) .attr("d", function(d) { return area(d.values); }) .style("fill", function(d, i) { return z(i); }); svg.selectAll(".layer") .attr("opacity", 1) .on("mouseover", function(d, i) { svg.selectAll(".layer").transition() .duration(250) .attr("opacity", function(d, j) { return j != i ? 0.6 : 1; })}) .on("mousemove", function(d, i) { mousex = d3.mouse(this); mousex = mousex[0]; var invertedx = x.invert(mousex); invertedx = invertedx.getFullYear(); var selected = (d.values); for (var k = 0; k < selected.length; k++) { datearray[k] = selected[k].year datearray[k] = datearray[k].getFullYear(); } mousedate = datearray.indexOf(invertedx); pro = d.values[mousedate].tradevalue; d3.select(this) .classed("hover", true) .attr("stroke", strokecolor) .attr("stroke-width", "0.5px"), tooltip.html( "<p>" + d.key + "<br>" + "Bil.$" + pro + "</p>" ).style("visibility", "visible"); }) .on("mouseout", function(d, i) { svg.selectAll(".layer") .transition() .duration(250) .attr("opacity", "1"); d3.select(this) .classed("hover", false) .attr("stroke-width", "0px"), tooltip.html( "<p>" + d.key + "<br>" + "Bil.$" + pro + "</p>" ).style("visibility", "hidden"); }) d3.select(".chart") .on("mousemove", function(){ mousex = d3.mouse(this); mousex = mousex[0] + 5; vertical.style("left", mousex + "px" )}) .on("mouseover", function(){ mousex = d3.mouse(this); mousex = mousex[0] + 5; vertical.style("left", mousex + "px")}); }); } </script>
Modified
http://d3js.org/d3.v2.js
to a secure url
https://d3js.org/d3.v2.js