score:1

Accepted answer

The issue here is that d3.csv is asynchronous, so data is filled at a different time than accessed.

If you want to run d3.csv once, get the data and save them elsewhere, you can try something like this or this or this

In general:

// define your module here    
var Module_BarDataDaily = function(data) {
   this.dataX = data; //this.dataX will store the data
   this.process = function(){// do whatever you need afterwards
            console.log(this.dataX);
    }
 };

var process = function(myModule){
   // or use another function to do what you need etc
   console.log(myModule.dataX);
}

// load your csv once and save the data
d3.csv("path/to/your.csv", function(error, data) {
    // after some proper error handling...
    var myModule = new Module_BarDataDaily(data);
    // so then you can do anything after that and 
    // your myModule will have data without calling d3.csv again
    myModule.process();
    //or
    process(myModule);
});

Hope this helps! Good luck!

score:0

I've used the following based on mkaran's answer:

 var Module_BarDataDaily = function(data) {
     this.dataX = data;
 };


 d3.csv("data/BarChart_data.csv", function(error, rows) {
     var myModule = new Module_BarDataDaily(rows);

     var chart = barChart();
     chart.render(myModule.dataX);

     d3.selectAll('input[name="meas"]').on("change", function change() {
         chart.currentMeasure(this.value)
         chart.render(myModule.dataX);
     });

 });

Related Query