从csv解析数据并在图形javascript中将其可视化

时间:2017-06-27 19:50:51

标签: javascript d3.js dimple.js

我正在尝试解析csv文件,这是我的代码

var directors ={};
var csv = d3.csv("movie.csv",function (data){

for (var i =0; i<data.length;i++){
    var dir= {};
    dir["director_name"] = data[i]["director_name"];
    dir["budget"] = data[i]["director_name"];
    dir["budget"] = +data[i]["budget"];
    dir["gross"] = +data[i]["gross"];
    dir["average"] = (dir.gross+dir.budget)/2;
    directors.push(dir);
}

});     的console.log(导演);

当我打印它时我得到了所有的对象,但是当我试图打印数组长度时,我得到array.length = 0得到特定的索引。 我需要可视化图表中的一些数据

//Chart
var svg = dimple.newSvg("body", 1200, 1200);
var chart = new dimple.chart(svg, d);
chart.setBounds(60, 30, 800, 800);
chart.addCategoryAxis("x", "budget");
chart.addMeasureAxis("y", "gross");
chart.addMeasureAxis("z", "average");
chart.addSeries(dimple.plot.bubble);
chart.addLegend(200, 10, 360, 20, "right");
//chart.setStoryboard("year");

chart.draw();

1 个答案:

答案 0 :(得分:-2)

var directors =[];
var csv = d3.csv("movie.csv",function (data){

    for (var i =0; i<data.length;i++){
        var dir= [];
        dir.name = data[i]["director_name"];
        dir.budget = +data[i]["budget"];
        dir.gross = +data[i]["gross"];
        dir.average = (dir.gross+dir.budget)/2;
        directors.push(dir);
    }

});
console.log(directors[0]);