Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
274 views
in Technique[技术] by (71.8m points)

javascript - Export Data from dc.js dataTable into CSV

I have a data table done by dc.js and crossfilter.js, and i want to export that table to CSV file..

dataTable.width(960).height(800)
.dimension(by_id)
.group(function(d) { return ""
 })
.size(data.length)                          
.columns([
  function(d) { return d.id; },
  function(d) { return d.name; },
  function(d) { return d.gender; },
  function(d) { return parseFloat(d.GPA).toFixed(2); },
  function(d) { return parseFloat(d.Major_GPA).toFixed(2); },
  function(d) { return parseFloat(d.Math_GPA).toFixed(2); },
  function(d) { return parseFloat(d.English_GPA).toFixed(2); },
  function(d) { return parseFloat(d.Science_GPA).toFixed(2); },
  function(d) { return parseFloat(d.Humanities_GPA).toFixed(2); }
  ])
.sortBy(function(d){ return d.id; })
.order(d3.ascending);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is a common request, so I've added an example using FileSaver.js. (There are probably other good ways to to a download from the browser, but this is the one I'm familiar with.)

http://dc-js.github.io/dc.js/examples/download-table.html

The key is to fetch all the data from the table's dimension using dimension.top(Infinity). Then you can format it using d3.csv.format and download it using your preferred method. Here, FileSaver uses Blobs as the mechanism:

    var blob = new Blob([d3.csv.format(nameDim.top(Infinity))],
                        {type: "text/csv;charset=utf-8"});
    saveAs(blob, 'data.csv');

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...