Связывание одной переменной между CSV и JSON в D3 - PullRequest
1 голос
/ 05 марта 2019

В настоящее время я хочу создать карту мира, используя d3.

Я использую https://unpkg.com/world-atlas@1.1.4/world/50m.json, чтобы загрузить данные, чтобы нарисовать карту мира, где одна из переменных, id такая жес моими dataset2010.csv, Country_code.

как я могу связать эти 2 файла через json id и csv Country_code.

Ниже приводится мой main.js

(function (d3,topojson) {
  'use strict';

  const svg = d3.select('svg');

  const projection = d3.geoNaturalEarth1();
  const pathGenerator = d3.geoPath().projection(projection);

  const g = svg.append('g');

  g.append('path')
      .attr('class', 'sphere')
      .attr('d', pathGenerator({type: 'Sphere'}));

  svg.call(d3.zoom().on('zoom', () => {
    g.attr('transform', d3.event.transform);
  }));

  Promise.all([
    d3.json('https://unpkg.com/world-atlas@1.1.4/world/50m.json'),
    d3.csv('dataset2010.csv')
  ]).then(([topoJSONdata, csvData]) => {


    // here I could not link them
    const countryName = {};
    csvData.forEach(d => {
      countryName[d.Country_code] = d.name;
      console.log(d.Country_code);
      console.log(d.name);
    });



    const countries = topojson.feature(topoJSONdata, topoJSONdata.objects.countries);
    g.selectAll('path').data(countries.features)
      .enter().append('path')
        .attr('class', 'country')
        .attr('d', pathGenerator)
      .append('title')
        .text(d => countryName[d.id]); //the d.id is from the json file.

  });

}(d3,topojson));
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...