D3.js:加入json和csv无法正常工作

时间:2015-10-06 16:19:19

标签: javascript json csv d3.js

我想用json文件中的静态地图和csv文件中的动态数据创建地图。将显示地图,但cvs文件中的动态数据不起作用。我试图遵循mbostock的解释,但到目前为止,我无法让联合工作。这是代码:

d3.json("/sites/default/d3_files/json/toronto-wards.json", function(error,wards) {
   var csvData;
d3.csv("/csv-pledges", function(error, csv) 
{
    csvData = csv;

   alert ("Hello" +csvData); // this displays

joinJsonCsv(csvData);
   alert ("Bye"); // doesn't display 
}); // end d3.csv

function joinJsonCsv(csv) {
// do something with rows
  alert ("joinJsonCsv" +csv); // this displays
   csv.forEach(function(d, i) {
     alert("the value of e.SCODE_NAME is: "+e.SCODE_NAME); // doesn't display
        world.forEach(function(e, j) {
            if (d.ward_no === e.SCODE_NAME) 
            {  
                e.NAME = d.ward_no
            } // end ward test
        }) // end world loop loop
    })// end of csv loop
alert ("exit joinJsonCsv"); // this doesn't display
} // end joinJsonCsv

我错过了什么?

1 个答案:

答案 0 :(得分:1)

您在其范围之外引用了e对象,因此函数崩溃了。我移动了警报,以便定义e,它现在应该可以工作了。     d3.json(" /sites/default/d3_files/json/toronto-wards.json" ;, function(error,wards){        var csvData;     d3.csv(" / csv-pledges",function(error,csv)     {         csvData = csv;

   alert ("Hello" +csvData); // this displays

joinJsonCsv(csvData);
   alert ("Bye"); // doesn't display 
}); // end d3.csv

function joinJsonCsv(csv) {
// do something with rows
  alert ("joinJsonCsv" +csv); // this displays
   csv.forEach(function(d, i) {
        world.forEach(function(e, j) {
            alert("the value of e.SCODE_NAME is: "+e.SCODE_NAME); // try this maybe
            if (d.ward_no === e.SCODE_NAME) 
            {  
                e.NAME = d.ward_no
            } // end ward test
        }) // end world loop loop
    })// end of csv loop
alert ("exit joinJsonCsv"); // this doesn't display
} // end joinJsonCsv