Last active
September 19, 2016 23:06
-
-
Save deweydell/3765a90eb36443b3191c45bb823e7f50 to your computer and use it in GitHub Desktop.
Animating four groups of data
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!DOCTYPE html> | |
| <html> | |
| <body> | |
| <div class="buttons"></div> | |
| <script src="https://d3js.org/d3.v4.min.js"></script> | |
| <style> | |
| body { | |
| background-color: #c0ffee; | |
| } | |
| .axis { | |
| font: 14px sans-serif; | |
| } | |
| .circleGroups text { | |
| font: 10px sans-serif; | |
| } | |
| </style> | |
| <script> | |
| //Sets up SVG chart | |
| var margin = {top: 50, right: 50, bottom: 50, left: 50}; | |
| var width = 800 - margin.left - margin.right, | |
| height = 800 - margin.top - margin.bottom; | |
| var svg = d3.select("body").append("svg") | |
| .attr("width", width + margin.left + margin.right) | |
| .attr("height", height + margin.top + margin.bottom) | |
| .append("g") | |
| .attr("transform", "translate(" + margin.left + "," + margin.top + ")"); | |
| var colorScale = d3.scaleSequential(d3.interpolateRainbow) | |
| .domain([0, 20]) | |
| var xScale = d3.scaleLinear() | |
| .domain([0,15]).nice() | |
| .range([0, width]) | |
| var yScale = d3.scaleLinear() | |
| .domain([0, 15]).nice() | |
| .range([height, 0]) | |
| var xAxis = d3.axisBottom(xScale); | |
| var yAxis = d3.axisLeft(yScale); | |
| svg.append("g") | |
| .attr("class", "x axis") | |
| .attr("transform", "translate(0," + height + ")") | |
| .call(xAxis) | |
| svg.append("g") | |
| .attr("class", "y axis") | |
| .call(yAxis) | |
| // Reads and Filters Data | |
| function dataSwap(datasetGroup) { | |
| thisDataGroup = data.filter(function(d) { return d.group == datasetGroup }) | |
| thisDataGroup.sort(function(a, b) { return a.x - b.x }); | |
| svg.selectAll(".circleGroups") | |
| .data(thisDataGroup) | |
| .transition() | |
| .delay(function(d, i) { return i*40; }) | |
| .attr("transform",function(d) { return "translate(" + xScale(d.x) + "," + yScale(d.y) + ")" }) | |
| svg.selectAll(".circleGroups") | |
| .select("circle") | |
| .transition() | |
| .delay(function(d, i) { return i*40; }) | |
| .style("fill", function(d, i) { return colorScale(i); }) | |
| d3.select(".title") | |
| .text("Group " + datasetGroup) | |
| } | |
| d3.tsv("quartet.tsv", function(error, data) { | |
| if (error) return console.warn(error); | |
| window.data = data; | |
| var groups = d3.set(data.map(function(d) { return d.group })).values(); | |
| d3.select(".buttons") | |
| .selectAll("button") | |
| .data(groups) | |
| .enter().append("button") | |
| .text(function(d) { return "Group " + d; }) | |
| .on("click", function(d) { | |
| dataSwap(d); | |
| }) | |
| groupI = data.filter(function(d) { return d.group == "I" }) | |
| groupI.sort(function(a, b) { return a.x - b.x }); | |
| // Draws the data on the chart | |
| var circleGroups = svg.selectAll(".circleGroups") | |
| .data(groupI) | |
| .enter().append("g") | |
| .attr("class", "circleGroups") | |
| .attr("transform", function(d) { return "translate(" + xScale(d.x) + "," + yScale(d.y) + ")"}) | |
| .on("mouseenter", function(d) { | |
| d3.select(this) | |
| .append("text") | |
| .attr("dx", 10) | |
| .attr("dy", 4) | |
| .text("[" + d.x + ", " + d.y + "]") | |
| d3.selectAll("circle") | |
| .style("fill-opacity", .5); | |
| d3.select(this) | |
| .select("circle") | |
| .transition() | |
| .ease(d3.easeElastic) | |
| .duration(2000) | |
| .attr("r", 9) | |
| .style("fill-opacity", 1); | |
| }) | |
| .on("mouseleave", function(d){ | |
| d3.select(this) | |
| .select("text") | |
| .remove(); | |
| d3.select(this) | |
| .select("circle") | |
| .transition() | |
| .ease(d3.easeElastic) | |
| .duration(2000) | |
| .attr("r", 6) | |
| d3.selectAll("circle") | |
| .style("fill-opacity", 1); | |
| }) | |
| circleGroups.append("circle") | |
| .attr("r", 6) | |
| .style("fill", function(d, i) { return colorScale(i); }) | |
| }) | |
| </script> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| group | x | y | |
|---|---|---|---|
| I | 10 | 8.04 | |
| I | 8 | 6.95 | |
| I | 13 | 7.58 | |
| I | 9 | 8.81 | |
| I | 11 | 8.33 | |
| I | 14 | 9.96 | |
| I | 6 | 7.24 | |
| I | 4 | 4.26 | |
| I | 12 | 10.84 | |
| I | 7 | 4.82 | |
| I | 5 | 5.68 | |
| II | 10 | 9.14 | |
| II | 8 | 8.14 | |
| II | 13 | 8.74 | |
| II | 9 | 8.77 | |
| II | 11 | 9.26 | |
| II | 14 | 8.1 | |
| II | 6 | 6.13 | |
| II | 4 | 3.1 | |
| II | 12 | 9.13 | |
| II | 7 | 7.26 | |
| II | 5 | 4.74 | |
| III | 10 | 7.46 | |
| III | 8 | 6.77 | |
| III | 13 | 12.74 | |
| III | 9 | 7.11 | |
| III | 11 | 7.81 | |
| III | 14 | 8.84 | |
| III | 6 | 6.08 | |
| III | 4 | 5.39 | |
| III | 12 | 8.15 | |
| III | 7 | 6.42 | |
| III | 5 | 5.73 | |
| IV | 8 | 6.58 | |
| IV | 8 | 5.76 | |
| IV | 8 | 7.71 | |
| IV | 8 | 8.84 | |
| IV | 8 | 8.47 | |
| IV | 8 | 7.04 | |
| IV | 8 | 5.25 | |
| IV | 19 | 12.5 | |
| IV | 8 | 5.56 | |
| IV | 8 | 7.91 | |
| IV | 8 | 6.89 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment