D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
ShayRomayo
Full window
Github gist
Failure
<!DOCTYPE html> <meta charset="utf-8"> <style> .axis--x path { display: none; } .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 cities; 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 xGroup = g.append("g"); var yGroup = g.append("g"); 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); }); g.append("clipPath") .attr("id", "clip") .append("rect") .attr("width", width) .attr("height", height); var zoom = d3.zoom() .scaleExtent([1, Infinity]) .translateExtent([[0, 0], [width, height]]) .on("zoom", zoomed); var zoomRect = svg.append("rect") .attr("width", width) .attr("height", height) .attr("fill", "none") .attr("pointer-events", "all") .call(zoom); d3.selection.prototype.moveToFront = function() { return this.each(function(){ this.parentNode.parentNode.appendChild(this.parentNode); }); }; d3.tsv("data.tsv", type, function(error, data) { if (error) throw error; cities = data.columns.slice(1).map(function(id) { return { id: id, values: data.map(function(d) { return {date: d.date, temperature: d[id]}; }) }; }); var xExtent = d3.extent(data, function(d) { return d.date; }); zoom.translateExtent([[x(xExtent[0]), -Infinity], [x(xExtent[1]), Infinity]]); 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; })); xGroup.attr("class", "axis axis--x") .attr("transform", "translate(0," + height + ")") .call(d3.axisBottom(x)); yGroup.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"); yGroup.call(d3.axisLeft(y)).select(".domain").remove(); var city = g.selectAll(".city") .data(cities) .attr("clip-path", "url(#clip)") .enter().append("g") .attr("class", "city"); city.append("path") .attr("class", "line") .attr("d", function(d) { return line(d.values); }) .attr("id", function(d) { return d.id.substring(0, 3).toUpperCase(); }) .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; }) .on("click", function(d) { var newOpacity; var thisCity = d.id.substring(0, 3).toUpperCase(); if (d3.select("#" + thisCity).style("opacity") == 1) { newOpacity = 0; } else { newOpacity = 1; } d3.select("#" + thisCity).style("opacity", newOpacity); }) .on("mouseover", function(d){ var thisCity = d.id.substring(0, 3).toUpperCase(); var active = d3.select("#" + thisCity).style("opacity") == 1; d3.select("#" + thisCity).moveToFront() if (!active) { d3.select("#" + thisCity).style("opacity", 0.75); } city.selectAll("path").filter(function(d) { return d.id.substring(0, 3).toUpperCase() != thisCity && d3.select("#" + d.id.substring(0, 3).toUpperCase()).style("opacity") != 0; }).style("stroke", "lightgrey"); }) .on("mouseout", function(d) { var thisCity = d.id.substring(0, 3).toUpperCase(); city.selectAll("path").style("stroke", function(d) { return z(d.id); }) if (d3.select("#" + thisCity).style("opacity") == 0.75) { d3.select("#" + thisCity).style("opacity", 0); } }); zoomRect.call(zoom.transform, d3.zoomIdentity); }); function zoomed() { var xz = d3.event.transform.rescaleX(x); x.domain(xz.domain()); xGroup.call(d3.axisBottom(x).scale(xz)); line.x(function(d) { return x(d.date); }) g.selectAll(".city") .selectAll("path") .attr("d", line); } 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