Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to create a map from an existing database #25

Closed
anand-010 opened this issue Jul 15, 2020 · 4 comments
Closed

How to create a map from an existing database #25

anand-010 opened this issue Jul 15, 2020 · 4 comments
Assignees
Labels
answered The question has been answered but is in a waiting state question Further information is requested

Comments

@anand-010
Copy link

I have a JSON data loaded from the server
data looks like this:
[array]
- country name
- count (value )
How do I display this into the map with other counties (not included in the array) value to be zero and countries present in the array with the count value?

@anand-010 anand-010 added the question Further information is requested label Jul 15, 2020
@sgratzl sgratzl self-assigned this Jul 15, 2020
@sgratzl
Copy link
Owner

sgratzl commented Jul 15, 2020

can you describe where you are stuck, cause as far as I can see, it is

  1. fetching the data from the server
  2. have a topojson file with all the countries
    1 .mixing them both to create the required {feature, value} data structure
  3. feeding that into the library

@anand-010
Copy link
Author

Finally code working can you find a way to work this code in an optimum way.

  // world map
  fetch('https://unpkg.com/world-atlas/countries-110m.json').then((r) => r.json()).then((data) => {
    let canvas = document.getElementById('world_map') as
    HTMLCanvasElement;
    let context = canvas.getContext("2d");
    const countries = ChartGeo.topojson.feature(data, data.objects.countries).features;
    console.log(ChartGeo.topojson);
    
    // TODO additional
    let mycontrylist = [{counry: 'India',count:10},{counry:'South Korea',count:15},{counry:'Vietnam',count:8}];
    let display_list = [];
    countries.forEach(element => {
      let flag:any = {flag:false};
      mycontrylist.forEach((elem)=>{
        var temp = element.properties.name === elem.counry;
        // console.log(temp);
        if (temp) {
          let t2 = countries.find((d) => d.properties.name === elem.counry);
          flag = { feature: t2, flag:true, value:elem.count};
        }
      })
      if (flag.flag) {
        console.log(flag);   
        display_list.push({feature : flag.feature, value: flag.value})
      }
      else{
        display_list.push({feature: element, value: 0 })
      }
    });
    // console.log("contries",countries.map((d) => d.properties.name));

    this.world_map = new Chart(context, {
      type: 'choropleth',
      data: {
        labels: countries.map((d) => d.properties.name),
        datasets: [{
          label: 'Countries',
          // data: countries.map((d) => ({feature: d, value: Math.random()})),
          data: display_list,
        }]
      },
      options: {
        showOutline: true,
        showGraticule: true,
        legend: {
          display: false
        },
        scale: {
          projection: 'equalEarth'
        },
        geo: {
          colorScale: {
            display: true,
          },
        },
      }
    });
  });

image

@sgratzl
Copy link
Owner

sgratzl commented Jul 17, 2020

could be rewritten to

let mycontrylist = [{counry: 'India',count:10},{counry:'South Korea',count:15},{counry:'Vietnam',count:8}];

const lookup = new Map(mycontrylist.map((d) => [d.counry, d.count]));

const display_list = countries.map((element) => {
 return {
     feature: element,
     value: lookup.get(element.properties.name) || 0,
  };
});

@sgratzl sgratzl added the answered The question has been answered but is in a waiting state label Jul 17, 2020
@anand-010
Copy link
Author

Thank you for your help Mr Samuel Gratzl.
It's working.
I am closing this issue with a smiling face.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
answered The question has been answered but is in a waiting state question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants