D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
mbmcpartland
Full window
Github gist
D3 Interactive Line Chart
<!DOCTYPE html> <meta charset="utf-8"> <style> .line { fill: none; stroke: steelblue; stroke-width: 1.5px; } </style> <svg width="960" height="500"></svg> <script src="//d3js.org/d3.v4.min.js"></script> <script> var parseTime = d3.timeParse("%Y%m%d"); 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]}; }) }; }); var svg = d3.select("svg").call(d3.zoom().on("zoom", zoomed)), 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 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 zoom = d3.zoom() .scaleExtent([1 / 4, 8]) .translateExtent([[-width, -Infinity], [2*width, Infinity]]) .extent([[0, 0], [width, height]]) .on("zoom", zoomed); 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; }); }) ]); var xAxis = d3.axisBottom(x); var yAxis = d3.axisLeft(y); var mX = g.append("g") .attr("class", "axis axis--x") .attr("transform", "translate(0," + height + ")") .call(xAxis); var mY = g.append("g") .attr("class", "axis axis--y") .call(yAxis); z.domain(cities.map(function(c) { return c.id; })); var city = g.selectAll(".city") .data(cities) .enter().append("g") .attr("class", "city"); city.append("path") .attr("class", "line") .attr("d", function(d) { return line(d.values); }) .style("stroke", function(d) { return z(d.id); }) .attr("id", function(d) { return d.id.substring(0,3).toUpperCase() }) .style("opacity", 1); 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; }) .on("click", function() { var ID = d3.select(this).text().substring(0,3).toUpperCase(); if(svg.select("path#" + ID).style("opacity") == 1) { svg.select("path#" + ID).style("opacity", 0); } else { svg.select("path#" + ID).style("opacity", 1); } }) .on("mouseover", function() { var ID = d3.select(this).text().substring(0,3).toUpperCase(); if(svg.select("path#" + ID).style("opacity") != 0) { svg.selectAll("path").each(function(d, i) { if(d != null) { var newID = d.id.substring(0,3).toUpperCase(); if(svg.select("path#" + newID).style("opacity") == 0) { svg.select("path#" + newID).style("opacity", 0); } if(svg.select("path#" + newID).style("opacity") == 1) { svg.select("path#" + newID).style("opacity", 0.1); } } }); svg.select("path#" + ID).style("opacity", 1); } }) .on("mouseout", function() { var ID = d3.select(this).text().substring(0,3).toUpperCase(); svg.selectAll("path").each(function(d, i) { if(d != null) { var ID = d.id.substring(0,3).toUpperCase(); if(svg.select("path#" + ID).style("opacity") == 0.1) { svg.select("path#" + ID).style("opacity", 1); } if(svg.select("path#" + ID).style("opacity") == 0) { svg.select("path#" + ID).style("opacity", 0); } } }); }) function zoomed() { svg.selectAll(".city").attr("transform", d3.event.transform); d3.selectAll('.line').style("stroke-width", 2/d3.event.transform.k); mX.call(xAxis.scale(d3.event.transform.rescaleX(x))); mY.call(yAxis.scale(d3.event.transform.rescaleY(y))); } }); 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