D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
dymzd
Full window
Github gist
A5 CS360
<!DOCTYPE html> <meta charset="utf-8"> <style> .axis--x path { display: none; } .line { fill: none; stroke: steelblue; stroke-width: 1.5px; } .legend-box { cursor: pointer; } #mouse-tracker { stroke: #E6E7E8; stroke-width: 1px; } </style> <svg width="960" height="500"></svg> <script src="//d3js.org/d3.v4.min.js"></script> <script> var svg = d3.select("svg"), margin = {top: 20, right: 80, bottom: 30, left: 50}, width = svg.attr("width") - margin.left - margin.right, height = svg.attr("height") - margin.top - margin.bottom, g = svg.append("g").attr("transform", "translate(" + margin.left + "," + margin.top + ")"); var parseTime = d3.timeParse("%Y%m%d"); var x = d3.scaleTime().range([0, width]), y = d3.scaleLinear().range([height, 0]), z = d3.scaleOrdinal(d3.schemeCategory10); var line = d3.line() .curve(d3.curveBasis) .x(function(d) { return x(d.date); }) .y(function(d) { return y(d.temperature); }); var color = d3.scaleOrdinal().range(["#48A36D", "#56AE7C", "#64B98C", "#72C39B", "#80CEAA", "#80CCB3", "#7FC9BD", "#7FC7C6", "#7EC4CF", "#7FBBCF", "#7FB1CF", "#80A8CE", "#809ECE", "#8897CE", "#8F90CD", "#9788CD", "#9E81CC", "#AA81C5", "#B681BE", "#C280B7", "#CE80B0", "#D3779F", "#D76D8F", "#DC647E", "#E05A6D", "#E16167", "#E26962", "#E2705C", "#E37756", "#E38457", "#E39158", "#E29D58", "#E2AA59", "#E0B15B", "#DFB95C", "#DDC05E", "#DBC75F", "#E3CF6D", "#EAD67C", "#F2DE8A"]); d3.tsv("data.tsv", type, function(error, data) { if (error) throw error; var cities = data.columns.slice(1).map(function(id) { return { id: id, values: data.map(function(d) { return { date: d.date, temperature: d[id], }; }), visible: (id === "San Francisco" ? true: false) //Visible is working. Data for San Francisco is null. }; }); x.domain(d3.extent(data, function(d) { return d.date; })); y.domain([ d3.min(cities, function(c) { return d3.min(c.values, function(d) { return d.temperature; }); }), d3.max(cities, function(c) { return d3.max(c.values, function(d) { return d.temperature; }); }) ]); z.domain(cities.map(function(c) { return c.id; })); g.append("g") .attr("class", "axis axis--x") .attr("transform", "translate(0," + height + ")") .call(d3.axisBottom(x)); g.append("g") .attr("class", "axis axis--y") .call(d3.axisLeft(y)) .append("text") .attr("transform", "rotate(-90)") .attr("y", 6) .attr("dy", "0.71em") .attr("fill", "#000") .text("Temperature, ºF"); var city = g.selectAll(".city") .data(cities) .enter().append("g") .attr("class", "city"); city.append("path") .attr("class", "line") .attr("id", function(d) { return "line-" + d.id.replace(" ", "").replace("/", ""); // Give line id of line-(insert issue name, with any spaces replaced with no spaces) }) .attr("d",function(d){ return d.visible ? line(d.values) : null; }) .attr("d", function(d) { return line(d.values); }) .style("stroke", function(d) { return z(d.id); }); city.append("text") .datum(function(d) { return {id: d.id, value: d.values[d.values.length - 1]}; }) .attr("transform", function(d) { return "translate(" + x(d.value.date) + "," + y(d.value.temperature) + ")"; }) .attr("x", 3) .attr("dy", "0.35em") .style("font", "10px sans-serif") .text(function(d) { return d.id; }) .style("fill",function(d){return d.visible ? color(d.id):"#000000"}) .attr("class", "legend-box") .on("click", function(d){ d.visible = !d.visible; console.log(d.id, d.visible); // d3.select("#line-" + d.id.replace(" ", "").replace("/", "")) // return d.visible ? line(d.values) : null; //CAN'T TRIGGER CLICK ? city.select("path") .transition() .attr("d", function(d){ return d.visible ? line(d.values) : null; // If d.visible is true then draw line for this d selection }) }) .on("mouseover", function(d){ d3.select(this) .transition() .attr("fill", function(d) { return color("F1F1F2"); }) d3.select("#line-" + d.id.replace(" ", "").replace("/", "")) .transition() .style("stroke-width", 2.5); d3.selectAll("#line-" + d.id.replace(" ", "").replace("/", "")) .transition() .style("stroke-width", 2.5); }) .on("mouseout", function(d){ d3.select("#line-" + d.id.replace(" ", "").replace("/", "")) .transition() .style("stroke-width", 1.5); }) ; }); function type(d, _, columns) { d.date = parseTime(d.date); for (var i = 1, n = columns.length, c; i < n; ++i) d[c = columns[i]] = +d[c]; return d; } </script>
https://d3js.org/d3.v4.min.js