fenec
fenec

Reputation: 5806

any idea why my d3 line graph is always off?

I have been working on graphing realtime data with d3 and i am having some trouble to keep it in the chart.

I am redrawing the graph every minute and rescale the x and y axis

xAxis.scale().domain([ 
    d3.min(chart_data, function (c) { return d3.min(c.points, function (v) { return v.created_at; }); }),
    d3.max(chart_data, function (c) { return d3.max(c.points, function (v) { return v.created_at; }); })
]);

yAxis.scale().domain([
    0,
    d3.max(chart_data, function (c) { return d3.max(c.points, function (v) { return v.rate; }); })
]);

HERE is the JSfiddle link http://jsfiddle.net/GsaGb/4/

Upvotes: 0

Views: 142

Answers (1)

Lars Kotthoff
Lars Kotthoff

Reputation: 109232

You need to redraw the lines after updating the scales and axes, otherwise it'll be off because it's still using the old scales. That is, move

linesGroup.selectAll("path")
    .data(chart_data)
    .transition().duration(1000)
    .attr("d", function(d){ return line(d.points); });

to after you've changed the domains.

Changed jsfiddle here.

Upvotes: 1

Related Questions