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

Fix the npm example #207

Open
zackees opened this issue Nov 15, 2023 · 0 comments
Open

Fix the npm example #207

zackees opened this issue Nov 15, 2023 · 0 comments
Labels
bug Something isn't working documentation Improvements or additions to documentation

Comments

@zackees
Copy link

zackees commented Nov 15, 2023

The npm example in the readme is incomplete and doesn't work. Here's one that does.

<canvas id="canvas"></canvas>
import Chart from 'chart.js/auto';
import { ChoroplethController, GeoFeature, ColorScale, ProjectionScale, topojson } from 'chartjs-chart-geo';

// register controller in chart.js and ensure the defaults are set
Chart.register(ChoroplethController, GeoFeature, ColorScale, ProjectionScale);

const CANVAS_ID = "canvas"
const DATA_SOURCE = "https://unpkg.com/us-atlas/states-10m.json";


async function initDynamicContent() {
  try {
    const response = await fetch(DATA_SOURCE);
    const us = await response.json();

    const nation = topojson.feature(us, us.objects.nation).features[0];
    const states = topojson.feature(us, us.objects.states).features;

    const data = {
      type: 'choropleth',
      data: {
        labels: states.map((d) => d.properties.name),
        datasets: [{
          label: 'States',
          outline: nation,
          data: states.map((d) => ({feature: d, value: Math.random() * 10})),
        }]
      },
      options: {
        plugins: {
          legend: {
            display: false
          },
        },
        scales: {
          projection: {
            axis: 'x',
            projection: 'albersUsa'
          },
          color: {
            axis: 'x',
            quantize: 5,
            legend: {
              position: 'bottom-right',
              align: 'bottom'
            },
          }
        },
      }
    }

    const chart = new Chart(document.getElementById(CANVAS_ID).getContext("2d"), data);
  } catch (error) {
    console.error("Error fetching and processing data:", error);
  }
}

export async function initGeoChart () {
  await initDynamicContent();
}
@zackees zackees added the bug Something isn't working label Nov 15, 2023
@sgratzl sgratzl added the documentation Improvements or additions to documentation label Nov 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

2 participants