D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
jmsarabia
Full window
Github gist
Assignment 5 - Interactivity D3
<!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").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 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); }); //ADDED: Method for brushing d3.selection.prototype.moveToFront = function(name) { return this.each(function(){ this.parentNode.parentNode.appendChild(this.parentNode); }); }; var scaledX, xAxis, areaPath, area, box; 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]}; }) }; }); 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; })); //ADDED: variables/methods for zoom function var zoom = d3.zoom() .scaleExtent([1, Infinity]) .translateExtent([[0, 0], [width, height]]) .extent([[0,0], [width, height]]) .on("zoom", zoomed); xAxis = d3.axisBottom(x); scaledX = g.append("g") .attr("class", "axis axis--x") .attr("transform", "translate(0," + height + ")") .call(xAxis); //ADDED: was to be used for clipping, but no changes even when called in the paths? box = svg.append("rect") .attr("class", "box") .attr("x", 10) .attr("y", 10) .attr("width", width-1) .attr("height", height-1) .attr("fill", "none") .attr("opacity", 0) .attr("pointer-events", "all") .call(zoom) .style("stroke-width", 0); //ADDED: for clipping, which doesn't work g.append("clipPath") .attr("id", "clipper") .append("rect") .attr("x", 1) .attr("y", 1) .attr("width", width ) .attr("height", height); // 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("d", function(d) { return line(d.values); }) //ADDED: id per notes .attr("id", function(d){ return d.id.substring(0,3).toUpperCase(); }) .attr("opacity", 1) .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; }) //ADDED: hovering functions .on("mouseover", function(){ var cityID = d3.select(this).text().substring(0,3).toUpperCase(); var opac = svg.select("path#"+cityID).style("opacity"); if(opac == 0) { //don't do anything } else { d3.select(this).moveToFront(cityID); //change all lines to gray except for this one d3.select(".path").style("stroke", 2); var path = svg.selectAll("path"); svg.selectAll("path").style("stroke", "lightgray"); svg.select("path#"+cityID).style("stroke", function(d) { return z(d.id); }); svg.select("path#"+cityID).style("opacity", 1); } }) .on("mouseout", function(){ var cityID = d3.select(this).text().substring(0,3).toUpperCase(); var opac = svg.select("path#"+cityID).style("opacity"); if(opac == 0) { //don't do anything } else { svg.select("path#"+cityID).style("stroke-width", 1.5); var circleUnderMouse = this; svg.selectAll("path").style("stroke", function(d) { //check that the path is not null (2 empty arrays) if(!(d==null)) {return z(d.id); } }) } }) .on("click", function(){ var cityID = d3.select(this).text().substring(0,3).toUpperCase(); var opac = svg.select("path#"+cityID).style("opacity"); if(opac == 0) { svg.select("path#"+cityID).style("opacity", 1); } else { svg.select("path#"+cityID).style("opacity", 0); } svg.selectAll("path").style("stroke", function(d) { //check that the path is not null if(!(d==null)) { return z(d.id); } }) }); }); 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() { svg.selectAll(".city").attr("transform", d3.event.transform); d3.selectAll(".line").style("stroke-width", 2/d3.event.transform.k); scaledX.call(xAxis.scale(d3.event.transform.rescaleX(x))); } </script>
https://d3js.org/d3.v4.min.js