Thursday, 12 September 2013

How can I add more time-series data after drawing a D3 line chart?

How can I add more time-series data after drawing a D3 line chart?

I have a D3 line chart that loads and displays a lot of live streaming
data. In order to make the UI feel faster, I am progressively rendering
the chart as data comes in in chunks. So, for example, I may be showing a
chart that spans 15 minutes, but load the latest 5 minutes, then load the
data from 10 minutes ago, then 15 minutes ago. In that time, I'll
re-render the chart 3 times.
My problem is that when I re-render the chart, a line is drawn across the
entire chart.
This is best explained through an example:
var end_ts = new Date().getTime();
var range = 60000;
var start_ts = end_ts - range;
var n = 30,
random = d3.random.normal(0, .2),
data = d3.range(n).map(function(i) {
return { time:new Date(end_ts - (n*1000) + i*1000), value:random() };
});
var margin = {top: 20, right: 20, bottom: 20, left: 40},
width = 500 - margin.left - margin.right,
height = 300 - margin.top - margin.bottom;
var x = d3.time.scale()
.domain([new Date(start_ts), new Date(end_ts)])
.range([0, width]);
var y = d3.scale.linear()
.domain([-1, 1])
.range([height, 0]);
var line = d3.svg.line()
.x(function(d, i) { return x(d.time); })
.y(function(d, i) { return y(d.value); });
var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
svg.append("defs").append("clipPath")
.attr("id", "clip")
.append("rect")
.attr("width", width)
.attr("height", height);
var xAxis = d3.svg.axis().scale(x).orient("bottom")
var xAxisGroup = svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + y(0) + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.call(d3.svg.axis().scale(y).orient("left"));
var path = svg.append("g")
.attr("clip-path", "url(#clip)")
.append("path")
.datum(data)
.attr("class", "line")
.attr("d", line);
// Render the first time
path.attr("d", line)
// Add some more data
for (var i=0; i < 10; i++)
data.push({time: new Date(start_ts+5000+i*1000), value: random()});
// Render the second time
path.attr("d", line)
See this on jsfiddle: http://jsfiddle.net/DANuz/
Anyone know how to add data to a chart and make this work? Note that it is
important that I am adding the newest data first, then older data later.
That's the behavior that I need.
Thanks.

No comments:

Post a Comment