0

Using the lineChart.html from the NVD3 examples, I've replaced the code as follows:

//data = sinAndCos();
data = getDataIntensity();

function getDataIntensity() {
    mySet = {};
    mySet.values = [];
    mySet.key = "Intensity";
    mySet.color = "#ff7f0e";
    mySet.strokeWidth = 4;

    d3.csv('20170906perfmix.csv', function(dat) {
        dat.forEach(function(d,i) {
            mySet.values.push({x: i, y: +d.Intensity});
        });
    });
    return [mySet];
}

I get the No Data Available message. I added a button to call chart.update() and then the data displays correctly. I understand this is probably due to the fact things are running asynchronously and the return of [mySet] occurs before all the data points are pushed onto the 'values' array.

How do I get this function to wait for all the data values to complete before returning?

Update: Now I'm confused about what is happening... if I console.log(mySet) prior to the return from getDataIntensity() it appears that all the values are there when using Chrome inspector. After calling chart.update() I do see that a member 'seriesIndex' has been set to 0 in the data object. Setting this value in mySet prior to the return has no effect. I get the No Data message on the first load in the browser.

Update2: fixed code by calling chart.update() within the callback

d3.csv('20170906perfmix.csv', function(dat) {
    dat.forEach(function(d,i) {
        mySet.values.push({x: i, y: +d.Intensity});
    });
    chart.update(); // fixes issue
});
return [mySet];
Robert
  • 159
  • 7
  • Two observations: 1. console.log is dynamic, it logs a **live** reference to the object, not a reference to the moment you logged it. 2. `d3.csv` is asynchronous, so, put all the code that depends on `dat` inside the callback. – Gerardo Furtado Sep 09 '17 at 23:29
  • 1
    Thank you @gerardo-furtado, updated code with chart.update() in the callback and all is well. – Robert Sep 10 '17 at 06:16

0 Answers0