2014-07-14 79 views
1

我有一个svg地图,每个路径都有一个class =“county-code - ###”。D3.js使用csv文件中的数据填充svg地图

还有一个csv文件,县名,县代码和县人口。

我有以下启动但不知道如何填充来自csv的日期的正确路径。

d3.text("counties.csv", function (datasetText) { 

     var parsedCSV = d3.csv.parseRows(datasetText); 

     var sampleHTML = d3.select("div") 
      .append("div") 
      .style("") 
      .style("") 

     .selectAll("path") 
      .data(parsedCSV) 
      .enter().append("path") 

    }); 
+0

这不是D3中真正支持的东西。 DOM元素的数据保存在'.__ data__'成员中,我猜在这种情况下,您最好的选择是手动填充(即不使用D3)。 –

回答

0

可以使用datum方法对区域的名称(和ID)添加到数据。例如:

<script> 
    var height = 500; 
    var width = 700; 

    // generate a sample 'map'. This svg does not contain the names of the 
    // regions 
    var data = [{id:1, x:10, y:10}, {id:2, x:300, y:400}, {id:3, x:600, y:100}]; 
    var vis = d3.select("#vis").append("svg") 
    .attr("width", width).attr("height", height); 
    vis.selectAll("rect").data(data).enter().append("rect") 
    .attr("x", function(d) { return d.x;}) 
    .attr("y", function(d) { return d.y;}) 
    .attr("width", 20).attr("height", 20) 
    .attr("class", function(d) { return "county-code-" + d.id;}); 

    // Some sample data containing the region names; in this case hard coded, 
    // but could also be read from a csv file. 
    var data2 = [{id:1, name:"a"}, {id:2, name:"b"}, {id:3, name:"c"}]; 

    // Add the names to the svg 
    vis.selectAll("rect").datum(function(d) { 
    // extract county code from class 
    var id = +d3.select(this).attr("class").match(/county-code-(\d+)/)[1]; 
    d.id = id; 
    // look up id in data2; if found add name to datum 
    for (var i = 0; i < data2.length; i++) { 
     if (data2[i].id == id) { 
     d.name = data2[i].name; 
     break; 
     } 
    } 
    return d; 
    }); 


</script> 
+0

非常好..必须尝试一下。回到你身边,如果我能得到这个工作。 – KornholioBeavis