Export Data from dc.js dataTable into CSV
Asked Answered
K

1

7

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);
Kubis answered 28/5, 2016 at 17:20 Comment(0)
L
9

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');
Labourite answered 1/6, 2016 at 16:6 Comment(8)
Here's an earlier answer using download.jsLabourite
That was very helpful, Thank you very much.Kubis
In V5 of d3 its now d3.csvFormat instead of d3.csv.formatKlatt
Thanks @Peter, sigh, yes, every answer I wrote before 2018 has been invalidated. :/Labourite
I found npmjs.com/package/dc-tableview to be very helpful. Github: github.com/karenpommeroy/dc-tableviewCambrel
Oh cool, thanks @Alok! I didn't know about that. Looks much more complete than my dc.datatables.js, which I probably wouldn't have written if I knew about dc-tableview.Labourite
You are welcome @Gordon! As always you are helpful and humble. Thank you.Cambrel
Any chance the PDF exported will be added soonMetal

© 2022 - 2024 — McMap. All rights reserved.