2017-04-03 73 views
0

我发现很少有关于使用JSON文件使用公用密钥映射和加入指标的CSV文件的d3文档。在使用d3.queue()时加入CSV和TopoJSON()

如果可能,我想利用d3.queue()。到目前为止,我一直在使用d3.map().set,但我发现它用于单个键值对,并且似乎不适用于> 1个值。如果我在这里有人请纠正我。

我正试图利用在this bl.ock中找到的双重for循环。这里的创建者正在使用两个JSON文件,并且我已经很少运气来根据我的需要进行裁剪。链接到他的两个JSON文件在下面,这是值得的。

tv.json

neilsontopo.json

连接键(我好得多说话SQL比JS)使用从JSON和在CSV的county_fipscounties.id

问题是CSV中的county_fips与指标相关的指标变平了吗?上面链接示例中的双for循环使用JSON的层次结构。

d3.queue() 
    .defer(d3.json, "https://d3js.org/us-10m.v1.json") 
    //.defer(d3.csv, "countymetrics_json.csv" , function(d) {metrics.set(d.county_fips, +d.actual_margin);}) 
    .defer(d3.csv, "https://raw.githubusercontent.com/MatthewSnellOKC/mapping/master/merge/countymetrics_json.csv", function(d) {metrics.set(d.county_fips, +d.actual_margin);}) 
    .await(ready); 

function ready (error, us) { 
    if (error) throw error; 
    var counties = us.objects.counties.geometries; 
} 

回答

0

我已经使用d3.queue()d3.map().set转移,并能够使用双for循环加入的CSV JSON。请参阅下面的工作代码。

d3.json("https://d3js.org/us-10m.v1.json", function(error, us) { 
    if (error) throw error; 

    d3.csv("countymetrics_json.csv", function(error, csv) { 
     var counties = us.objects.counties.geometries; 

     csv.forEach(function(d, i) { 
      counties.forEach(function(e, j) { 
       if (d.county_fips == e.id) { 
        e.actual_margin = d.actual_margin; 
        e.state_code = d.state_code; 
        e.state_name = d.state_name; 
        e.county = d.county; 
        e.county_population = d.county_population; 
        e.business_population = d.business_population; 
        e.active_merchants = d.active_merchants; 
        e.actual_margin = d.actual_margin; 
       } 
      }) 
     }) 
    }) 
});