Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
like image 501
Ms.J Avatar asked May 28 '16 17:05

Ms.J


People also ask

How to export jQuery DataTables to CSV DataTable using buttons?

I am using jQuery datatables buttons extension to export jQuery Datatable to CSV datatable feature.The buttons extension provides, Step 1: We have to include all jQuery datatables library files into head section of index.html file. Step 2: Created HTML layout for jQuery datatables listing in index.html file.

How do I export data from a project to a CSV?

Build and run the project Now build and run the project. Click on the button to export data. The output file will be test.csv. When you open the CSV file in Notepad, you will see this: By default, this file opens in Excel. Double-click to open this file in Excel. This is how the file looks like.

How to use a CSV file in Excel?

Another use of a CSV file is to directly open the file in Excel and then the data will be auto-filled into Excel cells. The following is a snapshot of a sample CSV file: Here is the process of creating a DataTable and exporting its data to a .csv file. a. Create a DataTable We added a class containing a method that returns a DataTable.

What data export plug-ins are available for DataTables?

The Buttons extension for DataTables provides three plug-ins that provide overlapping functionality for data export: Buttons provides button types that will alias HTML5 buttons. These are: copy, csv , excel, pdf.


1 Answers

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');
like image 138
Gordon Avatar answered Oct 02 '22 19:10

Gordon