D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
mountainsophist
Full window
Github gist
Exercise 6 Fixed
<!DOCTYPE html> <meta charset="utf-8"> <style> /* set the CSS */ body { font: 12px Arial;} path { stroke: steelblue; stroke-width: 2; fill: none; } .axis path, .axis line { fill: none; stroke: grey; stroke-width: 1; shape-rendering: crispEdges; } </style> <body> <h1>Changing Woodshop Production Goals</h1> <p>How I want to shift my woodworking away from <strong style="color: red;">Furniture</strong> and onto <strong style="color: steelblue;">Boxes</strong> and <strong style="color: brown;">Other</strong> items over the coming years</p> <!-- load the d3.js library --> <script src="https://d3js.org/d3.v3.min.js"></script> <script> // Set the dimensions of the canvas / graph var margin = {top: 30, right: 60, bottom: 30, left: 50}, width = 600 - margin.left - margin.right, height = 270 - margin.top - margin.bottom; // Parse the date / time var parseDate = d3.time.format("%Y").parse; var yearFormat = d3.time.format("%Y"); // Set the ranges var x = d3.time.scale().range([0, width]); var y = d3.scale.linear().range([height, 0]); // Define the axes var xAxis = d3.svg.axis().scale(x) .orient("bottom").ticks(8); var yAxis = d3.svg.axis().scale(y) .orient("left").ticks(5); // Define the line var valueline = d3.svg.line() .x(function(d) { return x(d.Year); }) .y(function(d) { return y(d.Boxes); }); var valueline2 = d3.svg.line() .x(function(d) { return x(d.Year); }) .y(function(d) { return y(d.Furniture); }); var valueline3 = d3.svg.line() .x(function(d) { return x(d.Year); }) .y(function(d) { return y(d.Other); }); // Adds the svg canvas 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 + ")"); // Get the data d3.csv("my_goals.csv", function(error, data) { data.forEach(function(d) { d.Year = parseDate(d.Year); d.Boxes = +d.Boxes; d.Furniture = +d.Furniture; }); // Scale the range of the data x.domain(d3.extent(data, function(d) { return d.Year; })); y.domain([0, d3.max(data, function(d) { return Math.max(d.Boxes, d.Furniture); })]); // Add the valueline path. svg.append("path") .attr("class", "line") .attr("d", valueline(data)); svg.append("text") .attr("transform", "translate("+(width+5)+","+y(data[data.length - 1].Boxes)+")") .attr("dy", ".35em") .attr("text-anchor", "start") .style("fill", "steelblue") .text("Boxes"); //Add the valueline2 path svg.append("path") .style("stroke", "red") .attr("d", valueline2(data)); svg.append("text") .attr("transform", "translate("+(width+5)+","+y(data[data.length - 1].Furniture)+")") .attr("dy", ".35em") .attr("text-anchor", "start") .style("fill", "red") .text("Furniture"); //Add the valueline3 path svg.append("path") .style("stroke", "brown") .attr("d", valueline3(data)); svg.append("text") .attr("transform", "translate("+(width+5)+","+y(data[data.length - 1].Other)+")") .attr("dy", ".35em") .attr("text-anchor", "start") .style("fill", "brown") .text("Other"); svg.selectAll("dot") .data(data) .enter().append("circle") .attr("r", 3.5) .attr("cx", function(d) { return x(d.Year); }) .attr("cy", function(d) { return y(d.Boxes); }) .attr("fill", "steelblue") .append("title") .text(function(d) { return yearFormat(d.Year) + " Boxes: " + d.Boxes + " pieces"; }); svg.selectAll("dot") .data(data) .enter().append("circle") .attr("r", 3.5) .attr("cx", function(d) { return x(d.Year); }) .attr("cy", function(d) { return y(d.Furniture); }) .attr("fill", "red") .append("title") .text(function(d) { return yearFormat(d.Year) + " Furniture: " + d.Furniture + " pieces"; }); svg.selectAll("dot") .data(data) .enter().append("circle") .attr("r", 3.5) .attr("cx", function(d) { return x(d.Year); }) .attr("cy", function(d) { return y(d.Other); }) .attr("fill", "brown") .append("title") .text(function(d) { return yearFormat(d.Year) + " Other: " + d.Other + " pieces"; }); // Add the X Axis svg.append("g") .attr("class", "x axis") .attr("transform", "translate(0," + height + ")") .call(xAxis); // Add the Y Axis svg.append("g") .attr("class", "y axis") .call(yAxis); }); </script> </body>
Modified
http://d3js.org/d3.v3.min.js
to a secure url
https://d3js.org/d3.v3.min.js