D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
ByronHan333
Full window
Github gist
filter brush and zoom line chart
Built with
blockbuilder.org
<!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="https://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 area = d3.area() // .curve(d3.curveStepAfter) // .y0(y(0)) // .y1(function(d) { return y(d.value); }); // var areaPath = g.append("path") // .attr("clip-path", "url(#clip)") // .attr("fill", "steelblue"); var yGroup = g.append("g"); var xGroup = g.append("g") .attr("transform", "translate(0," + height + ")"); var zoom = d3.zoom() .scaleExtent([1 / 4, 8]) .translateExtent([[-width, -Infinity], [2 * width, Infinity]]) .on("zoom", zoomed); var zoomRect = svg.append("rect") .attr("width", width) .attr("height", height) .attr("fill", "none") .attr("pointer-events", "all") .call(zoom); g.append("clipPath") .attr("id", "clip") .append("rect") .attr("width", width) .attr("height", height); var xAxis = d3.axisBottom(x); var yAxis = d3.axisLeft(y); 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]}; }) }; }); console.log(cities); 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 + ")"); g.append("g") .attr("class", "axis axis--y") .call(yAxis) .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") .attr("id", function(d) { return d.id.substring(0, 3).toUpperCase();}); city.append("path") .attr("class", "line") .attr("clip-path", "url(#clip)") .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();}); 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; }); var cityLabels = d3.selectAll(".city").selectAll("text"); cityLabels.on("click", function(outer) { var me = d3.select(this); var on = me.classed("on"); var idSubStr = outer.id.substring(0, 3).toUpperCase();; if (!on) { d3.select("path#" + idSubStr) .transition() .style("opacity", 0) .on("end", function(d) { d3.select(this).style("visibility", "hidden"); }); } else { d3.select("path#" + idSubStr) .style("visibility", "visible") .transition() .style("opacity", 1); } me.classed("on", !on); }); cityLabels.on("mouseover", function(outer) { var me = d3.select(this); var on = me.classed("on"); var idSubStr = outer.id.substring(0, 3).toUpperCase(); if (!on) { d3.selectAll(".city").selectAll("path") .filter(function(d) { return idSubStr != d.id.substring(0, 3).toUpperCase();; }) .transition().style("stroke", "lightgray"); } d3.select("g#" + idSubStr).raise(); }); cityLabels.on("mouseout", function(outer) { var me = d3.select(this); var on = me.classed("on"); var idSubStr = outer.id.substring(0, 3).toUpperCase();; d3.selectAll(".city").selectAll("path") .filter(function(d) { return idSubStr != d.id.substring(0, 3).toUpperCase();; }) .transition().style("stroke", function(d) { return z(d.id); }); }); // var xExtent = d3.extent(data, function(d) { return d.date; }); // zoom.translateExtent([[x(xExtent[0]), -Infinity], [x(xExtent[1]), Infinity]]) // y.domain([0, d3.max(cities, function(c) { return (c.values, function(d){ return d.temperature;}); })]); // yGroup.call(yAxis).select(".domain").remove(); // areaPath.datum(); zoomRect.call(zoom.transform, d3.zoomIdentity); }); 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; } function zoomed() { var xz = d3.event.transform.rescaleX(x); xGroup.call(xAxis.scale(xz)); line.x(function(d) { return xz(d.date);}) g.selectAll(".line") .attr("d", function(d) { return line(d.values); }); // g.selectAll('.line').style("stroke-width", 2/d3.event.transform.k); // line.x() // g.selectAll(".line") // .attr("d", function(d) { return line(d.values); }) // areaPath.attr("d", area.x(function(d) { return xz(d.date); })); } </script>
https://d3js.org/d3.v4.min.js