Basic implementation of a progress bar using D3 that transitions between statuses. A reusable d3 progress bar component can be found here: https://github.com/sarahob/d3ProgressBar
Last active
March 16, 2017 17:20
-
-
Save sarahob/1e291c95c4169ddabb77bbd10b6a7ef7 to your computer and use it in GitHub Desktop.
Progress Bar with D3
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 type="text/css"> | |
.progressSelector{ | |
margin-bottom: 30px; | |
} | |
</style> | |
<body> | |
<select class="progressSelector" onchange="moveProgressBar(value)"> | |
<option value="started" selected>Started</option> | |
<option value="inProgress">In Progress</option> | |
<option value="completed">Completed</option> | |
</select> | |
<div class="progress"></div> | |
<script src='https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js'></script> | |
<script> | |
var svg = d3.select('.progress') | |
.append('svg') | |
.attr('height', 100) | |
.attr('width', 500); | |
var states = ['started', 'inProgress', 'completed'], | |
segmentWidth = 100, | |
currentState = 'started'; | |
var colorScale = d3.scale.ordinal() | |
.domain(states) | |
.range(['yellow', 'orange', 'green']); | |
svg.append('rect') | |
.attr('class', 'bg-rect') | |
.attr('rx', 10) | |
.attr('ry', 10) | |
.attr('fill', 'gray') | |
.attr('height', 15) | |
.attr('width', function(){ | |
return segmentWidth * states.length; | |
}) | |
.attr('x', 0); | |
var progress = svg.append('rect') | |
.attr('class', 'progress-rect') | |
.attr('fill', function(){ | |
return colorScale(currentState); | |
}) | |
.attr('height', 15) | |
.attr('width', 0) | |
.attr('rx', 10) | |
.attr('ry', 10) | |
.attr('x', 0); | |
progress.transition() | |
.duration(1000) | |
.attr('width', function(){ | |
var index = states.indexOf(currentState); | |
return (index + 1) * segmentWidth; | |
}); | |
function moveProgressBar(state){ | |
progress.transition() | |
.duration(1000) | |
.attr('fill', function(){ | |
return colorScale(state); | |
}) | |
.attr('width', function(){ | |
var index = states.indexOf(state); | |
return (index + 1) * segmentWidth; | |
}); | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey @sarahob.Thanks alot for the js
I tried running it on my Jupyter (Ipython) notebook,however,my output shows nothing.Its blank.
Kindly help