dc.js d3+crossfilter.top将过滤后的数据导出为CSV

dc.js d3+crossfilter.top to export filtered data to CSV

本文关键字:数据 CSV d3+crossfilter js top 过滤 dc      更新时间:2023-09-26

我仔细研究了代码示例,并能够调用crossfilter的.top(Infinity)函数来输出过滤后的更新数据记录。我甚至可以用.top(n)调用d3.csv.format,以便获得可以下载的字符串化CSV输出。但我有几个问题。

  1. 我在我的图表绘制方法中调用top和csv.format函数,如下所述:

    d3.csv("/data/acuityData.csv", function (data) {
     // format our data
     var dtgFormat = d3.time.format("%Y-%m");
     var dtgFormat2 = d3.time.format("%a %e %b %H:%M");
    data.forEach(function(d) {
    d.dtg1=d.dayOfWeek;
    d.dtg= dtgFormat.parse(d.year+"-"+d.month);
    d.dtg2=d.year;
    d.mag= d.acuityInitial;
    d.depth= d.waitTime;
    d.dispositions= d.disposition;
    });
    
     // Run the data through crossfilter and load our 'facts'
    var facts = crossfilter(data);
    var all = facts.groupAll();
     // for wait time
     var depthValue = facts.dimension(function (d) {
       return d.depth;
     });
     var depthValueGroup = depthValue.group();  
     depthChart.width(930)
        .height(150)
        .margins({top: 10, right: 10, bottom: 20, left: 40})
        .dimension(depthValue)
        .group(depthValueGroup)
        .transitionDuration(500)
        .centerBar(true)    
        .gap(1)  
        .x(d3.scale.linear().domain([0, 500]))
        .elasticY(false)
        .xAxis().tickFormat(function(v) {
          console.log(d3.csv.format(depthValue.top(500)));
        return v;});
    dc.renderAll();
    });
    

为了节省空间,我删除了一些图表。我最重要的问题是关于调用这个导出数据调用。现在,我只是console.log处理输出,它在浏览器控制台中显示为一组"漂亮"的CSV数据。我无法解决的问题是如何添加一个能够调用此方法的Export All链接,因为在d3.csv函数闭包的范围之外无法访问此代码区域我该如何解决这个问题

  1. CSV输出中返回的数据有重复的行,因为它包括原始CSV文件中的列以及data.forEach(function(d){}块中可见的生成列的句柄。CSV输出中的标题显示如下:

acuityInitial,dayOfWeek,disposition,hour,month,waitTime,year,dtg1,dtg,dtg2,mag,depth,dispositions

不知道如何解决这个问题。任何帮助都将不胜感激。

我只使用jquery onclick处理程序就解决了这个问题。我想我只是需要在这个问题上睡一觉。还使用download.js触发使用javascript 的文件下载

     depthChart.width(930)
        .height(150)
        .margins({top: 10, right: 10, bottom: 20, left: 40})
        .dimension(depthValue)
        .group(depthValueGroup)
        .transitionDuration(500)
        .centerBar(true)    
        .gap(1)  
        .x(d3.scale.linear().domain([0, 500]))
        .elasticY(false)
        .xAxis().tickFormat(function(v) {
         //function triggered onClick by element from DOM
             $("#exportData").unbind().click(function() {
                  download(d3.csv.format(depthValue.top(500))."exportedData.csv")
              });
        return v;});
    dc.renderAll();
    });