D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
kimalbrecht
Full window
Github gist
Zoomable Connected Scatterplot
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><meta charset="utf-8"> <script src="https://d3js.org/d3.v3.js"></script> <style> body{ /*background-color: black;*/ } .axis path{ fill: none; stroke: none; } .axis line { fill: black; stroke: black; shape-rendering: crispEdges; } .axis text { font: 10px sans-serif; fill: black; } .yearButton{ color: black; } </style> </head> <body> <script src="d3.v3.min.js"></script> <div id="visualization"></div> <script> var diameter = 600; var bundaries = 30; var scale = 1; var color = d3.scale.category20c(); var x = d3.scale.log().range([bundaries,diameter-bundaries]); var y = d3.scale.log().range([bundaries,diameter-bundaries]); var xAxis = d3.svg.axis() .scale(x) .orient("bottom") .ticks(6, ",.1s") .tickSize(6, 0); var yAxis = d3.svg.axis() .scale(y) .orient("left") .ticks(6, ",.1s") .tickSize(6, 0); // x.domain([3200,.9]); // y.domain([9000000,1]); var svg = d3.select("#visualization") .append("svg") .attr("width", diameter) .attr("height", diameter) .append("g"); d3.csv("data.csv", function(data){ var years = d3.nest() .key(function(d) { return "y" + (d.year - 2008); }) .rollup(function(values) { return d3.sum(values, function(d) {return +d.real_pageviews; }) }) .map(data); // console.log(years) // console.log(data) x.domain([ d3.max(data, function(d) { return +d.best_rank; }), d3.min(data, function(d) { return +d.best_rank; }) ]); y.domain([ d3.max(data, function(d) { return +d.real_pageviews; }), d3.min(data, function(d) { return +d.real_pageviews; }) ]); svg.append("rect") .attr("x", 0) .attr("y", 0) .attr("width", diameter) .attr("height", diameter) .style("fill", "white") .on("click", zoomOut); svg.append("g") .attr("class", "x axis") .attr("transform", "translate(0," + (diameter - bundaries) + ")") .call(xAxis); svg.append("g") .attr("class", "y axis") .attr("transform", "translate(" + bundaries + ",0)") .call(yAxis); svg.selectAll("circle") .data(data) .enter() .append("circle") .attr("id", function (d) {return d.player_name;}) .attr("class", function (d) {return "y" + d.year;}) .attr("cx", function (d) {return x(d.best_rank);}) .attr("cy", function (d) {return y(d.real_pageviews);}) .attr("r", function (d) {return (d.year-2007)/2;}) .style("stroke", function (d) {return color(d.year-2007);}) .style("fill", "rgba(0,0,0,0.05)") // .attr("opacity", 1) .style("stroke-width", 1) .on("click", zoom); console.log(years) var check = d3.select("body").selectAll(".yearButton") // .data(years) // .enter() // .style("font-size", "20px"); .style("font-size", years.y2008 + "px"); console.log(check) d3.selectAll("circle") .on('mouseover', function(d){ d3.selectAll("circle") .transition() .style("stroke-width", 0); d3.selectAll("#" + this.id) .transition() .style("stroke-width", 2) .style("opacity", "1"); svg.append("text") .attr("class", "tooltip") .attr("id", "tooltip") .attr("x", 100) .attr("y", 100) .text(this.id); }) .on('mouseout', function(d){ d3.select("#tooltip").remove(); d3.selectAll("circle") .transition() .style("stroke-width", 1 / scale + "px"); d3.selectAll("#" + this.id) .transition() .style("stroke-width", 1 / scale + "px"); }); function zoom() { var zoomSpeed = 2000; var selectPerson = this.id; var selectedData = d3.selectAll("#" + this.id).data(); var selectedRankMax = d3.max(selectedData, function(d) { return +d.best_rank; }); var selectedRankMin = (d3.min(selectedData, function(d) { return +d.best_rank; })); var selectedViewsMax = d3.max(selectedData, function(d) { return +d.real_pageviews; }); var selectedViewsMin = (d3.min(selectedData, function(d) { return +d.real_pageviews; })); var years = d3.nest() .key(function(d) { return "y" + d.year; }) .rollup(function(values) { return d3.sum(values, function(d) {return +d.real_pageviews; }) }) .map(selectedData); console.log(years) y.domain([selectedViewsMax,selectedViewsMin]); x.domain([selectedRankMax,selectedRankMin]); d3.selectAll("circle") .transition() .duration(zoomSpeed) .style("stroke-width", 1 / scale + "px") .style("fill", function (d) { if (d.player_name != selectPerson) { return "rgba(0,0,0,0.05)" } else{ color(d.year-2007) }; }) .attr("r", function (d) {return (d.year-2007) / scale;}) .attr("cx", function (d) {return x(d.best_rank);}) .attr("cy", function (d) {return y(d.real_pageviews);}); var wikiLine = d3.svg.line() .x(function(d) { return x(d.best_rank); }) .y(function(d) { return y(d.real_pageviews); }); // .interpolate("cardinal"); d3.select(".wikiLine") .transition() .duration(zoomSpeed) .style("opacity", "0") .remove(); var selectedLine = d3.select("svg") .append("path") .attr("class", "wikiLine") .style("stroke", "black") .style("fill","none") .style("opacity", "0"); selectedLine .transition() .delay(zoomSpeed) .duration(500) .style("opacity", ".5") .attr("d", wikiLine(selectedData)); d3.select(".x.axis") .transition() .duration(zoomSpeed) .call(xAxis); d3.select(".y.axis") .transition() .duration(zoomSpeed) .call(yAxis); } function zoomOut() { d3.select(".wikiLine") .transition() .duration(200) .style("opacity", "0") .remove(); x.domain([ d3.max(data, function(d) { return +d.best_rank; }), d3.min(data, function(d) { return +d.best_rank; }) ]); y.domain([ d3.max(data, function(d) { return +d.real_pageviews; }), d3.min(data, function(d) { return +d.real_pageviews; }) ]); d3.selectAll("circle") .transition() .duration(1000) .style("stroke-width", 1 / scale + "px") .attr("r", function (d) {return (d.year-2007) / 2;}) .attr("cx", function (d) {return x(d.best_rank);}) .attr("cy", function (d) {return y(d.real_pageviews);}); d3.select(".x.axis") .transition() .duration(1000) .call(xAxis); d3.select(".y.axis") .transition() .duration(1000) .call(yAxis); } }); function yearSelection(year) { d3.selectAll("circle") .transition() .style("stroke-width", 0); var hoverYear = ".y" + year; // console.log(hoverYear) d3.selectAll(hoverYear) .transition() .style("stroke-width", .5); }; </script> </body> </html>
Modified
http://d3js.org/d3.v3.js
to a secure url
https://d3js.org/d3.v3.js