将数据从dc.js dataTable导出到CSV [英] Export Data from dc.js dataTable into CSV
问题描述
我有一个数据表由dc.js和crossfilter.js完成,我想导出该表到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);
推荐答案
这是一个常见的请求,使用 FileSaver.js 的示例。 (可能还有其他很好的方法从浏览器下载,但这是我熟悉的一个。)
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
关键是使用 dimension.top(Infinity)从表维度中提取所有数据
。然后,您可以使用 d3.csv.format
格式化,并使用您首选的方法下载。这里,FileSaver使用 Blob
s作为机制:
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 Blob
s as the mechanism:
var blob = new Blob([d3.csv.format(nameDim.top(Infinity))],
{type: "text/csv;charset=utf-8"});
saveAs(blob, 'data.csv');
这篇关于将数据从dc.js dataTable导出到CSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!