Skip to content

Instantly share code, notes, and snippets.

@mrtriangle
Last active August 29, 2015 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrtriangle/11222333 to your computer and use it in GitHub Desktop.
Save mrtriangle/11222333 to your computer and use it in GitHub Desktop.
Bar Chart with Transition
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Transition</title>
<style>
body {
font-family: "helvetica";
}
button {
margin: 0 7px 0 0;
background-color: #f5f5f5;
border: 1px solid #dedede;
border-top: 1px solid #eee;
border-left: 1px solid #eee;
font-size: 12px;
line-height: 130%;
text-decoration: none;
font-weight: bold;
color: #565656;
cursor: pointer;
}
.single-bar {
min-height: 1px;
min-width: 30px;
background-color: #4682b4;
margin-right: 1px;
font-size: 10px;
color: #f0f8ff;
text-align: center;
width: 15px;
display: inline-block;
}
.baseline {
height: 1px;
background-color: black;
}
.axis path, .axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.axis text {
font: 10px sans-serif;
}
.axis .grid-line{
stroke: black;
shape-rendering: crispEdges;
stroke-opacity: .2;
}
.line{
fill: none;
stroke: #000;
stroke-width: 2;
}
.dot {
fill: #fff;
stroke: #000;
}
.area {
stroke: none;
fill: steelblue;
fill-opacity: .2;
}
.pie text{
fill: white;
font-weight: bold;
}
.bubble{
fill-opacity: .3;
}
.bar{
stroke: none;
fill: steelblue;
}
text {
font-size: 11px;
pointer-events: none;
}
text.parent {
fill: steelblue;
}
circle {
fill: #ccc;
stroke: #999;
pointer-events: all;
}
circle.parent {
fill: steelblue;
fill-opacity: .1;
stroke: steelblue;
}
circle.parent:hover {
stroke-width: .5px;
}
circle.child {
pointer-events: none;
}
</style>
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
</head>
<body>
<script type="text/javascript">
var id = 0,
data = [],
duration = 500,
chartHeight = 200,
chartWidth = 900;
for(var i = 0; i < 20; i++){
push(data);
}
function render(data) {
var selection = d3.select('body')
.selectAll('div.single-bar')
.data(data, function(d){return d.id});
selection.enter()
.append('div')
.attr('class', 'single-bar')
.style('position', 'fixed')
.style('top', chartHeight + 'px')
.style('left', function(d, i){
return barLeft(i+1) + 'px';
})
.style('height', '0px')
.append('span');
selection
.transition().duration(duration)
.style('top', function(d) {
return chartHeight - barHeight(d) + 'px';
})
.style('left', function(d, i) {
return barLeft(i) + 'px';
})
.style('height', function(d) {
return barHeight(d) + 'px';
})
.select('span')
.text(function (d) {return d.value;});
selection.exit()
.transition().duration(duration)
.style('left', function(d, i){
return barLeft(-1) + 'px';
})
.remove();
}
function push(data){
data.push({
id: ++id,
value: Math.round(Math.random() * chartHeight)
});
}
function barLeft(i) {
return i * (44);
}
function barHeight(d) {
return d.value;
}
setInterval(function () {
data.shift();
push(data);
render(data);
}, 2000);
render(data);
d3.select('body')
.append('div')
.attr('class', 'baseline')
.style('position', 'fixed')
.style('top', chartHeight + 'px')
.style('left', '0px')
.style('width', chartWidth + 'px');
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment