Created
February 1, 2013 10:46
-
-
Save peterhughesdev/4690601 to your computer and use it in GitHub Desktop.
Gauge proof-of-concept
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
<!DOCTYPE html> | |
<meta charset="utf-8"> | |
<style> | |
.progress-meter .background { | |
fill: #ccc; | |
} | |
.progress-meter .foreground { | |
fill: #000; | |
} | |
.progress-meter text { | |
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; | |
font-size: 24px; | |
font-weight: bold; | |
} | |
</style> | |
<body> | |
<script src="http://d3js.org/d3.v3.min.js"></script> | |
<script> | |
var width = 960, | |
height = 500, | |
twoPi = 2 * Math.PI, | |
progress = 0, | |
v = 0, | |
total = 100, // must be hard-coded if server doesn't report Content-Length | |
formatPercent = d3.format(".0%"); | |
var arc = d3.svg.arc() | |
.startAngle(0) | |
.innerRadius(180) | |
.outerRadius(240); | |
var svg = d3.select("body").append("svg") | |
.attr("width", width) | |
.attr("height", height) | |
.append("g") | |
.attr("transform", "translate(" + width / 2 + "," + height / 2 + ")"); | |
var meter = svg.append("g") | |
.attr("class", "progress-meter"); | |
meter.append("path") | |
.attr("class", "background") | |
.attr("d", arc.endAngle(twoPi)); | |
var foreground = meter.append("path") | |
.attr("class", "foreground"); | |
var text = meter.append("text") | |
.attr("text-anchor", "middle") | |
.attr("dy", ".35em"); | |
setInterval(function() { | |
var n = Math.sin(Math.random() * 180) * 13; | |
v = v+n > total ? v-n : v+n; | |
var i = d3.interpolate(progress, v / total); | |
d3.transition().tween("progress", function() { | |
return function(t) { | |
progress = i(t); | |
foreground.attr("d", arc.endAngle(twoPi * progress)); | |
text.text(formatPercent(progress)); | |
}; | |
}); | |
}, 1000); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment