如何使用 d3 将 .csv 文件加载到 crossfilter 中?

How to load a .csv file into crossfilter with d3?

我正在尝试将 .csv 文件加载到 crossfilter 中,以便进一步将其与 dc.js 和 d3 一起使用。但是,如果 ndx = crossfilter(data_) 不在 d3.csv(... 内,则不起作用。是否可以在 global/outside 变量(在本例中为 ndx)中使用 d3 加载数据?

 var ndx;

 private method(){

        var data_;

        d3.csv("samples.csv", function(data){

            var format = d3.timeParse("%m-%y"); 

            data.forEach(function(d: any) {
                d.date = format(d.date);
            });

            data_ = d3.csvParse(data);
        });

        ndx = crossfilter(data_);
}

如何将其加载到 crossfilter 中? 我是否必须在 d3.csv(.. 调用中使用 crossfilter?


解法: 我将我的 .csv 变成了 .json 并加载了它 'synchronously'。下面观察。

 var ndx;

 private method(){

        var data_ = (function() {
            var json: any = null;
            $.ajax({
                'async': false,
                'global': false,
                'url': "samples.json",
                'dataType': "json",
                'success': function (data:any) {
                    json = data;
                }
            });
            return json;
        })(); 

        ndx = crossfilter(data_);
}

观察:

'async': false

发生这种情况是因为一旦返回数据,回调函数就会异步执行。这意味着如果您将图表代码放在回调之外,您将获得您定义的空数组,因为尚未返回任何数据。