d3.csv('/data.csv',
// How to format each row. Since the CSV file has a header, `row` will be
// an object with keys derived from the header.
function(row) {
return {name : row.name, age : +row.age, color : row.color};
},
// Callback to run once all data's loaded and ready.
function(data) {
// Log the data to the JavaScript console
console.log(data);
// Compute some interesting results
var averageAge = data.reduce(function(prev, curr) {
return prev + curr.age;
}, 0) / data.length;
// Also, display it
var ulSelection = d3.select('body').append('ul');
var valuesSelection =
ulSelection.selectAll('li').data(data).enter().append('li').text(
function(d) { return d.age; });
var totalSelection =
ulSelection.append('li').text('Average: ' + averageAge);
});
var pivot = function(data){
var result = [];
for (var i = 0; i < data.length; i++){
for (var j=0; j < data[i].length; j++){
if (i === 0){
result[j] = [];
}
result[j][i] = data[i][j];
}
}
return result;
};
var getData = function() {
var csvString = $(".myText").val();
var csvLines = csvString.split(/\n?$/m);
var dataTable = [];
for (var i = 0; i < csvLines.length; i++){
var values;
eval("values = [" + csvLines[i] + "]");
dataTable[i] = values;
}
return pivot(dataTable);
};
Recline 特别拥有一个 Dataset 对象,其结构有点类似于 Panda 数据帧。然后,它允许您连接您的数据与“视图”,如数据网格,图形,地图等。视图通常是现有最好的可视化库(如 D3、 Flot、 SlickGrid 等)的薄包装器。
这里有一个例子:
// Load some data
var dataset = recline.Model.Dataset({
records: [
{ value: 1, date: '2012-08-07' },
{ value: 5, b: '2013-09-07' }
]
// Load CSV data instead
// (And Recline has support for many more data source types)
// url: 'my-local-csv-file.csv',
// backend: 'csv'
});
// get an element from your HTML for the viewer
var $el = $('#data-viewer');
var allInOneDataViewer = new recline.View.MultiView({
model: dataset,
el: $el
});
// Your new Data Viewer will be live!