title: Updating D3.js (NVD3.js) Data by Merge
body: <p>I'm working on some realtime graphs built with NVD3.js. I currently refresh each chart with the following:</p>
  
  <pre><code>function reDraw(c) {
      d3.json(c.qs, function(data) {
          d3.select(c.svg)
          .datum(data)
          .transition().duration(500)
          .call(c.chart);
      });
  }
  </code></pre>
  
  <p>c looks like:</p>
  
  <pre><code>function Chart(svg, qs, chart) {
      this.qs = qs;
      this.svg = svg;
      this.ylabel;
      this.chart;
  }
  </code></pre>
  
  <p>This works fairly well, but with each refresh I am fetching the whole time series again. It would be more efficient to only grab recent elements and update each graph. There are examples of doing this by appending elements (This answer <a href="https://stackoverflow.com/questions/12548640/nvd3-line-chart-with-realtime-data">NVD3 line chart with realtime data</a> and <a href="http://mbostock.github.io/d3/tutorial/bar-2.html" rel="nofollow noreferrer">this tutorial</a> for example) , but this isn't ideal for me since some recent elements might be updated that are not the most recent element.</p>
  
  <p>So what I'm looking to do is grab say the most recent minute (by setting query string (<code>.qs</code>) to only get the most recent minute, then take that result and do the following:</p>
  
  <ul>
  <li>Overwrite any existing elements that have the same x value for each series with the most recent data</li>
  <li>Append and elements when there are new x values from the update in each series</li>
  <li>Expire elements past a certain age</li>
  <li>Update the NVD3.js script with the new data. Maybe still use datum with the new merged object?</li>
  </ul>
  
  <p><em>Can anyone suggest an elegant way to perform the above Merge operation?</em> The existing data object looks like the following:</p>
  
  <pre><code>&gt; d3.select(perf.svg).data()[0]
  [
  Object
  key: "TrAvg"
  values: Array[181]
  __proto__: Object
  , 
  Object
  key: "RedisDurationMsAvg"
  values: Array[181]
  __proto__: Object
  , 
  Object
  key: "SqlDurationMsAvg"
  values: Array[181]
  __proto__: Object
  ]
  &gt; d3.select(perf.svg).data()[0][0]['values'][0]
  Object {x: 1373979220000, y: 22, series: 0}
  &gt; d3.select(perf.svg).data()[0][1]['values'][0]
  Object {x: 1373979650000, y: 2, series: 1}
  </code></pre>
  
  <p>The object returned would look something like the following (Except will only be maybe 6 elements or so for each object):</p>
  
  <pre><code>&gt; d3.json(perf.qs, function(data) { foo = data })
  Object {header: function, mimeType: function, response: function, get: function, post: function…}
  &gt; foo
  [
  Object
  , 
  Object
  , 
  Object
  ]
  &gt; foo[0]
  Object {key: "TrAvg", values: Array[181]}
  &gt; foo[0]['values'][0]
  Object {x: 1373980220000, y: 49}
  </code></pre>
  
  <p>In this newer object the series value is missing - maybe that needs to get added or perhaps D3 can do it?</p>