Created
March 17, 2018 08:44
-
-
Save adamjanes/c2eea6a36d935cfbe01d807d9b149d0e to your computer and use it in GitHub Desktop.
This file contains 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
// Scales | |
var x = d3.scaleLinear() | |
.range([0, width]); | |
var y = d3.scaleLinear() | |
.range([height, 0]); | |
// Axes | |
var xAxisCall = d3.axisBottom(x) | |
var xAxis = g.append("g") | |
.attr("class", "x-axis") | |
.attr("transform", "translate(" + 0 + "," + height + ")"); | |
var yAxisCall = d3.axisLeft(y) | |
var yAxis = g.append("g") | |
.attr("class", "y-axis"); | |
// Labels | |
xAxis.append("text") | |
.attr("class", "axis-title") | |
.attr("transform", "translate(" + width + ", 0)") | |
.attr("y", -6) | |
.text("Grade Point Average") | |
yAxis.append("text") | |
.attr("class", "axis-title") | |
.attr("transform", "rotate(-90)") | |
.attr("y", 16) | |
.text("Height / Centimeters"); | |
var flag = true; | |
// Run this code every second... | |
d3.interval(function(){ | |
// Flick between our two data arrays | |
data = flag ? data0 : data1; | |
// Update our chart with new data | |
update(data); | |
// Update our flag variable | |
flag = !flag; | |
}, 1000) | |
// Run for the first time | |
update(data0); | |
function update(data){ | |
// Update our scales | |
x.domain([d3.min(data, function(d){ return d.gpa; }) / 1.05, | |
d3.max(data, function(d){ return d.gpa; }) * 1.05]) | |
y.domain([d3.min(data, function(d){ return d.height; }) / 1.05, | |
d3.max(data, function(d){ return d.height; }) * 1.05]) | |
// Update our axes | |
xAxis.call(xAxisCall); | |
yAxis.call(yAxisCall); | |
// Update our circles | |
var circles = g.selectAll("circle") | |
.data(data); | |
circles.exit().remove() | |
circles | |
.attr("cx", function(d){ return x(d.gpa) }) | |
.attr("cy", function(d){ return y(d.height) }) | |
circles.enter() | |
.append("circle") | |
.attr("cx", function(d){ return x(d.gpa) }) | |
.attr("cy", function(d){ return y(d.height) }) | |
.attr("r", 5) | |
.attr("fill", "grey"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment