D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
nehabandal
Full window
Github gist
D3 Interaction Assignment 5
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="//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 + ")") svg.append("defs") .append("clipPath") .attr("id", "clip") .append("rect") .attr("width", width) .attr("height", height); 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); }); // zoom effect var zoom = d3.zoom() .scaleExtent([1 / 4, 8]) .translateExtent([[-width, -Infinity], [2 * width, Infinity]]) .on("zoom", zoomed); // zoom panel var zoomRect = svg.append("rect") .attr("width", width) .attr("height", height) .attr("fill", "none") .attr("pointer-events", "all") .call(zoom); d3.tsv("data.tsv", type, function(error, data) { if (error) throw error; // zoom effect setup var cities = data.columns.slice(1).map(function(id) { return { id: id, values: data.map(function(d) { return {date: d.date, temperature: d[id]}; }) }; }); 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") // add id on each cities (easier in selecting) .attr("id", function(d){return formatLetter(d.id)}) // zxc .attr("class", "city"); // use a easier draw path function // zxc drawPath(city); 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") .style("cursor", "pointer") .text(function(d) { return d.id; }) .on("click", function(d){ var onClick = d3.select(this).classed("onClick"); if(!onClick){ g.select("#"+this.parentNode.id).select("path").remove(); } else { drawPath(g.select("#"+ this.parentNode.id)); } // toglge on/off flag d3.select(this).classed("onClick", !onClick); }) .on("mouseover", function(d){ // if this path is not disabled, bring this line to front, color other line to gray var onClick = d3.select(this).classed("onClick"); if(!onClick){ g.select("#"+ this.parentNode.id).raise(); g.selectAll(".city").filter(function(c){ // return city that is not hover return d.id != c.id; }) .select("path") .style("stroke", "lightgray"); } }) .on("mouseout", function(d){ // resume all cities line color g.selectAll(".city") .select("path") .style("stroke", function(city){ return z(city.id); }) }) // zxc var xExtent = d3.extent(data, function(d) {return d.date; }); zoom.translateExtent([[x(xExtent[0]), -Infinity], [x(xExtent[1]), Infinity]]); // yGroup.call(yAxis).select(".domain").remove(); // areaPath.datum(data); zoomRect.call(zoom.transform, d3.zoomIdentity); }); // zoom function function zoomed() { var xz = d3.event.transform.rescaleX(x); var yz = d3.event.transform.rescaleY(y); // x axis change when zoom g.select(".axis--x") .call(d3.axisBottom(x).scale(xz)); // y g.select(".axis--y") .call(d3.axisLeft(x).scale(yz)); //line line.x(function(d) { return xz(d.date); }) .y(function(d) { return yz(d.temperature); }); lines.attr("d", function(d){return line(d.values);}); //zxc } // easier for drawing path, based on pass in elem function drawPath(elem) { lines = elem.append("path") .attr("class", "line") // zxc .attr("d", function(d) {return line(d.values); }) // .attr("clip-path", "url(#clip)") .style("fill", "none") .style("stroke", function(d) { return z(d.id); }); } // for formatting id values function formatLetter(state){ return state.replace(/[^a-zA-Z]/g, "_"); } 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