Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The example you based your code upon deeply intertwines sliding the x-axis and adding data. It works fine when you poll something as in the example, not so much with data arriving on a different interval or at random like I suppose it does in your websocket setup (see <a href="http://jsfiddle.net/K8rub/1/" rel="nofollow">http://jsfiddle.net/K8rub/1/</a> for an hopefully accurate reproduction of your code)</p> <p>The first thing to do is to separate the sliding (the <code>tick</code> function) and the data injection: <a href="http://jsfiddle.net/K8rub/2/" rel="nofollow">http://jsfiddle.net/K8rub/2/</a> : each datum has a timestamp which is used to extract the x value, the data is initially empty and <code>tick</code> only slides the graph.</p> <p>Applying these modifications to your code, we get</p> <pre><code>TimeseriesView = Backbone.View.extend({ initialize: function (options) { _.bindAll(this, 'tick'); // ... self.dataSet = options.dataSet; self.dataSet.on('add', this.newPoint, this); self.tick(); }, newPoint: function(model) { this.data.push(model); }, tick: function () { var self = this; self.now = new Date(); var now = self.now; var duration = self.duration; var n = self.n; var x = self.x; var y = self.y; var width = this.width; var height = this.height; x.axis = d3.svg.axis().scale(x).orient("bottom"); // update the domains self.x.domain([now - (n - 2) * duration, now - duration]); self.y.domain([0, d3.max(self.dataSet.pluck('auth_amt'))]); self.line = d3.svg.line() .x(function(d) { return x(d.get('stamp')); }) .y(function(d) { return y(d.get('auth_amt')); }); // redraw the line d3.select(".line") .attr("d", self.line) .attr("transform", null); // slide the x-axis to the left self.axis.transition() .duration(duration) .ease("linear") .call(x.axis); self.x = d3.time.scale() .domain([now - (n-2) * duration, now - duration]) .range([0, width]); var x = self.x; // slide the line left self.path.transition() .duration(duration) .ease("linear") .attr("transform", "translate(" + x(now - (n - 1) * duration) + ")") .each("end", self.tick); } }); </code></pre> <p><a href="http://jsfiddle.net/K8rub/4/" rel="nofollow">http://jsfiddle.net/K8rub/4/</a></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload