【问题标题】:Joining a CSV and a TopoJSON together while utilizing d3.queue()在使用 d3.queue() 时将 CSV 和 TopoJSON 连接在一起
【发布时间】:2017-04-03 05:18:10
【问题描述】:

我发现很少有关于使用 JSON 文件映射和连接 CSV 文件使用公共密钥的 d3 文档。

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

我试图利用在this bl.ock 中找到的双for 循环。这里的创建者使用了两个 JSON 文件,但我很难根据自己的需要定制它。下面是他的两个 JSON 文件的链接,值得一看。

tv.json

neilsontopo.json

要使用的连接键(我说 SQL 比 JS 好得多)是 JSON 中的 counties.id 和 CSV 中的 county_fips 值。

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;
}

【问题讨论】:

    标签: javascript json csv d3.js


    【解决方案1】:

    我已经从使用 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;
                    }
                })
            })
        })
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-11
      • 2015-06-01
      • 1970-01-01
      • 2017-09-14
      • 1970-01-01
      • 2014-02-19
      • 2016-08-04
      • 1970-01-01
      相关资源
      最近更新 更多