sgratzl / chartjs-chart-geo

Chart.js Choropleth and Bubble Maps
https://www.sgratzl.com/chartjs-chart-geo/
MIT License
347 stars 36 forks source link

How to use bubble map with longitude and latitude #202

Closed Dan-Veinberg closed 11 months ago

Dan-Veinberg commented 1 year ago

Can someone give me an example on how to use bubblemap with longitude and latitude values for the whole world? Is there a way to do it without use typescript? Also can you answer the following questions:

  1. I also keep an error that the map needs to be destroyed before being created. I am using React.js.
  2. How to update map with new data?

Thank you so much. I am have been trying to create a Bubble Map world map with longitude and latitude for several weeks now without success. My Code So far:

useEffect(()=>{

` fetch('https://unpkg.com/world-atlas@2.0.2/countries-50m.json')
    .then((r) => {
      console.log(r.json().then((datapoint)=>{
        const countries = ChartGeo.topojson.feature(datapoint,datapoint.objects.countries).features
        console.log(countries)
    const chart = new Chart(document.getElementById("canvas").getContext("2d"),{
      type: 'bubbleMap',
      data: {
        labels: countries.map((d) => d.properties.name),
        datasets: [
          {
            label: 'States',
            outline: countries,
            data: countries.map((d) => ({
              value: 10,
              feature: d
            })),
          },
        ],
      },
      options: {
        showOutline: true,
        showGraticule: false,
        plugins: {
          legend: {
            display: false,
          },
        },
        scales: {
          xy: {
            projection: 'equalEarth'
          },
        }
      },
    });

  }))

  })

`},[])``

sgratzl commented 1 year ago

have you looked at https://www.sgratzl.com/chartjs-chart-geo/examples/bubbleMap.html with https://github.com/sgratzl/chartjs-chart-geo/blob/main/docs/examples/data/us-capitals.json

Dan-Veinberg commented 1 year ago

Thank you for answering. I have not look at those examples yet. How do I just do bubbles for longitude and latitude instead of states? It seems like the examples do states only.

sgratzl commented 1 year ago

what do you mean? the referenced example uses the referenced json which contains long/lat for the US capitals and that's where the bubbles are shown.

Dan-Veinberg commented 1 year ago

Okay I see now. I will try it out. Thank you for your help.