Created
March 22, 2016 17:52
-
-
Save jrbalsano/5de7fd6b94be178f8727 to your computer and use it in GitHub Desktop.
D3 Scatterplot Rendering - 2
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> | |
<head> | |
<script src="http://d3js.org/d3.v3.min.js"></script> | |
<script> | |
var height = 500; | |
var width = 960; | |
var timer, startTime; | |
var startTime; | |
function showTimeSince(startTime) { | |
var currentTime = new Date().getTime(); | |
var runtime = currentTime - startTime; | |
document.getElementById('timeRendering').innerHTML = runtime + 'ms'; | |
console.log(runtime + 'ms'); | |
} | |
function startTimer() { | |
stopTimer(); | |
startTime = new Date().getTime(); | |
timer = setInterval(function() { | |
showTimeSince(startTime); | |
}, 10); | |
showTimeSince(startTime); | |
} | |
function stopTimer() { | |
if (timer) { | |
clearInterval(timer); | |
} | |
showTimeSince(startTime); | |
} | |
function drawCircles(svg, data) { | |
startTimer(); | |
setTimeout(function() { | |
var circles = svg.selectAll('circle').data(data); | |
circles.enter().append('circle') | |
.attr('r', 3); | |
circles.exit().remove(); | |
circles | |
.attr('cx', function(d) { return d.x }) | |
.attr('cy', function(d) { return d.y }); | |
stopTimer(); | |
}, 0); | |
} | |
function renderChart() { | |
var numPoints = parseInt(document.getElementsByName('numPoints')[0].value, 10); | |
if (isNaN(numPoints)) { | |
return; | |
} | |
startTimer(); | |
var data = []; | |
for (var i = 0; i < numPoints; i++) { | |
data.push({ | |
x: Math.random() * width, | |
y: Math.random() * height | |
}); | |
} | |
var svg = d3.selectAll('svg').data([0]); | |
svg.enter().append('svg') | |
.attr("height", height) | |
.attr("width", width); | |
drawCircles(svg, data); | |
} | |
</script> | |
</head> | |
<body> | |
<form action=""> | |
<input name="numPoints" type="text" value="10000"> | |
<button type="button" id="render" onClick="renderChart()">Render</button> | |
</form> | |
Time Rendering: <span id="timeRendering"></span> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment