NVD3 line chart with realtime data
Asked Answered
L

3

10

I have a really simple line chart written using NVD3.js. I've written a simple redraw based on timer, pulled from examples I've seen, but I get the error

Uncaught TypeError: Cannot read property 'y' of undefined

The JS is

    var data = [{
        "key": "Long",
        "values": getData()
    }];
    var chart;

    nv.addGraph(function () {
          chart = nv.models.cumulativeLineChart()
                      .x(function (d) { return d[0] })
                      .y(function (d) { return d[1] / 100 })
                      .color(d3.scale.category10().range());

        chart.xAxis
            .tickFormat(function (d) {
                return d3.time.format('%x')(new Date(d))
            });

        chart.yAxis
            .tickFormat(d3.format(',.1%'));

        d3.select('#chart svg')
            .datum(data)
            .transition().duration(500)
            .call(chart);

        nv.utils.windowResize(chart.update);

        return chart;
    });


    function redraw() {
        d3.select('#chart svg')
            .datum(data)
          .transition().duration(500)
            .call(chart);
    }

    function getData() {
        var arr = [];
        var theDate = new Date(2012, 01, 01, 0, 0, 0, 0);
        for (var x = 0; x < 30; x++) {
            arr.push([theDate.getTime(), Math.random() * 10]);
            theDate.setDate(theDate.getDate() + 1);
        }
        return arr;
    }

    setInterval(function () {
        var long = data[0].values;
        var next = new Date(long[long.length - 1][0]);
        next.setMonth(next.getMonth() + 1)
        long.shift();
        long.push([next.getTime(), Math.random() * 100]);
        redraw();
    }, 1500);
Lutero answered 22/9, 2012 at 23:57 Comment(2)
Can you add a jsfiddle that demonstrates the issue?Stentorian
@JaimalChohan how did you fix this?Copyreader
G
11

Second Answer (after comment)

I looked at source for cumulativeLineChart. You can see the display.y property get created during chart creation. It relies on a private method: "indexify". If some derivative of that method was made public, then perhaps you could do something like chart.reindexify() before redrawing.

As a temporary workaround, you could recreate the chart from scratch on every update. If you remove the transition, that seems to work okay. Example jsfiddle: http://jsfiddle.net/kaliatech/PGyKF/.

First Answer

I believe there is bug in cumulativeLineChart. It appears that the cumulativeLineChart adds a "display.y" property dynamically to data values in the series. However, it does not regenerate this property when new values are added to the series for a redraw. I don't know of anyway to make it do this, although I'm new to NVD3.

Do you really need a CumulativeLineChart, or would a normal line chart be sufficient? If so, I had to make the following changes to your code:

  • Change from cumulativeLineChart to lineChart
  • Change from using 2 dimension arrays of data, to using objects of data (with x,y properties)
    • (I'm not familiar enough with NVD3 to say what data formats is expects. The 2D array obviously works for initial loads, but I think it fails to work for subsequent redraws. This is likely related to the same issue you are having with cumulativeLineChart. I thought changing to objects would fix cumulativeLineChart as well, but it didn't seem to.)

I also changed the following, although not as important:

  • Modified your getData function to create a new instance of Date to avoid unexpected consequences of sharing a reference as the date gets incremented.

  • Modified the update interval function to generate new data in increments of days (not months) with y values in the same range as the getData function.

Here's a working jsfiddle with those changes:

Gallivant answered 2/10, 2012 at 15:12 Comment(3)
No, it needs to be a cumulative line chart, I simplified my jsfiddle to only show 1 line to make the problem easier to understandLutero
ah. Well, I really think it's a bug of sorts in NVD3. I was able to make cumulativeLineChart function without throwing errors by manually adding a display:{y:???} property to the data point. JSfiddle: jsfiddle.net/kaliatech/khp9d/30 . Of course, the result is not correct. I think only recourse is to examine source and figure out how to get that property generated/updated on redraw.Gallivant
I actually prefer the comment above as a solution, as the proposed one in your question results in redrawing the entire graph. Apart from a easily resolved interpolation issue, jsfiddle.net/kaliatech/khp9d/30 is what I was looking forLutero
G
1

I found what I think is a better solution. The problem occurs because the cumulative chart sets the y function during processing. Whenever your want to refresh the chart, first set it back to a default which returns the correct original y. In your redraw function do this before updating:

chart.y(function (d) { return d.y; });

Even better would be if the cumulative chart could do this for itself (store the original access function before setting the new one, and put it back before re-indexing). If I get a chance, I'll try to push a fix.

Generally answered 18/11, 2013 at 12:51 Comment(0)
M
0

I ran into the same issue. I changed the y() function on the lines from

.y(function(d) { return d.display.y })

to

.y(function(d) { return d.display ? d.display.y : d.y })

This gets rid of the error. Obviously it won't be displaying the (non-existent) indexed value in the error case, but in my experience, the chart gets updated again with display defined, and it looks correct.

Meryl answered 29/7, 2013 at 15:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.