-
-
Save wimdows/1510223 to your computer and use it in GitHub Desktop.
transition not working
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> | |
<html> | |
<head> | |
<script type="text/javascript" src="https://raw.github.com/mbostock/d3/master//d3.js"></script> | |
<script type="text/javascript" src="https://raw.github.com/mbostock/d3/master/d3.time.js"></script> | |
<style type="text/css"> | |
body { | |
font: 10px sans-serif; | |
margin: 0; | |
} | |
path.line { | |
fill: none; | |
stroke: #666; | |
stroke-width: 1.5px; | |
} | |
path.area { | |
fill: #e7e7e7; | |
} | |
.axis { | |
shape-rendering: crispEdges; | |
} | |
.x.axis line { | |
stroke: #fff; | |
} | |
.x.axis .minor { | |
stroke-opacity: .5; | |
} | |
.x.axis path { | |
display: none; | |
} | |
.y.axis line, .y.axis path { | |
fill: none; | |
stroke: #000; | |
} | |
</style> | |
</head> | |
<body> | |
<script type="text/javascript"> | |
var m = [20, 40, 20, 20], | |
w = 960 - m[1] - m[3], | |
h = 240 - m[0] - m[2], | |
parse = d3.time.format("%b %Y").parse; | |
// Scales. Note the inverted domain for the y-scale: bigger is up! | |
var x = d3.time.scale().range([0, w]), | |
y = d3.scale.linear().range([h, 0]); | |
// Axes. | |
var xAxis = d3.svg.axis().scale(x).tickSize(-h).tickSubdivide(true), | |
yAxis = d3.svg.axis().scale(y).ticks(4).orient("right"); | |
// A line generator, for the dark stroke. | |
var line = d3.svg.line() | |
.interpolate("monotone") | |
.x(function(d) { return x(d.date); }) | |
.y(function(d) { return y(d.price); }); | |
values = [] | |
var d1 = new Date ("12/1/2011"); | |
count = 0; | |
for (i=0; i<=50; i++) { | |
d2 = new Date(d1); | |
values.push({ | |
date: d2.setMinutes(d1.getMinutes() + (i*1)), | |
price: Math.random() * 20 | |
}); | |
count++; | |
} | |
// Compute the minimum and maximum date, and the maximum price. | |
x.domain([values[0].date, values[values.length - 1].date]); | |
y.domain([0, d3.max(values, function(d) { return d.price; })]); | |
// Add an SVG element with the desired dimensions and margin. | |
svg = d3.select("body").append("svg") | |
.attr("width", w + m[1] + m[3]) | |
.attr("height", h + m[0] + m[2]) | |
.append("g") | |
.attr("transform", "translate(" + m[3] + "," + m[0] + ")"); | |
// Add the x-axis. | |
svg.append("g") | |
.attr("class", "x axis") | |
.attr("transform", "translate(0," + h + ")") | |
.call(xAxis); | |
// Add the y-axis. | |
svg.append("g") | |
.attr("class", "y axis") | |
.attr("transform", "translate(" + w + ",0)") | |
.call(yAxis); | |
// Add the line path. | |
svg.append("path") | |
.attr("class", "line") | |
// .attr("clip-path", "url(#clip)") | |
.attr("d", line(values)); | |
function redraw() { | |
svg.selectAll("path") | |
.data([values]) | |
.attr("transform", "translate(" + x(1) + ")") | |
.attr("d", line) | |
.transition() | |
.ease("linear") | |
.duration(1000) | |
.attr("transform", "translate(" + x(0) + ")"); | |
} | |
setInterval(function() { | |
var v = values.shift(); | |
console.log(v); | |
values.push({ | |
date: d2.setMinutes(d1.getMinutes() + (count*1)), | |
price: Math.random() * 20 | |
}); | |
redraw(); | |
count++; | |
}, 1000); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment