项目作者: sgratzl

项目描述 :
Chart.js Choropleth and Bubble Maps
高级语言: TypeScript
项目地址: git://github.com/sgratzl/chartjs-chart-geo.git
创建时间: 2019-09-26T02:48:19Z
项目社区:https://github.com/sgratzl/chartjs-chart-geo

开源协议:MIT License

下载


Chart.js Geo

NPM Package Github Actions

Chart.js module for charting maps with legends. Adding new chart types: choropleth and bubbleMap.

Choropleth

Open in CodePen

Earth Choropleth

Open in CodePen

Bubble Map

Open in CodePen

works great with https://github.com/chartjs/chartjs-plugin-datalabels

Check out also my other chart.js plugins:

Install

  1. npm install --save chart.js chartjs-chart-geo

Usage

see https://www.sgratzl.com/chartjs-chart-geo/ website

CodePens

Options

The option can be set globally or per dataset

see https://github.com/sgratzl/chartjs-chart-geo/blob/main/src/controllers/GeoController.ts#L221

Choropleth

A Choropleth (chart type: choropleth) is used to render maps with the area filled according to some numerical value.

Choropleth

Open in CodePen

Earth Choropleth

Open in CodePen

Data Structure

A data point has to have a .feature property containing the feature to render and a .value property containing the value for the coloring.

TopoJson is packaged with this plugin to convert data, it is exposed as ChartGeo.topojson in the global context. However, this plugin doesn’t include any topojson files itself. Some useful resources I found so far:

  1. const us = await fetch('https://cdn.jsdelivr.net/npm/us-atlas/states-10m.json').then((r) => r.json());
  2. // whole US for the outline
  3. const nation = ChartGeo.topojson.feature(us, us.objects.nation).features[0];
  4. // individual states
  5. const states = ChartGeo.topojson.feature(us, us.objects.states).features;
  6. const alaska = states.find((d) => d.properties.name === 'Alaska');
  7. const california = states.find((d) => d.properties.name === 'California');
  8. ...
  9. const config = {
  10. data: {
  11. labels: ['Alaska', 'California'],
  12. datasets: [{
  13. label: 'States',
  14. outline: nation, // ... outline to compute bounds
  15. showOutline: true,
  16. data: [
  17. {
  18. value: 0.4,
  19. feature: alaska // ... the feature to render
  20. },
  21. {
  22. value: 0.3,
  23. feature: california
  24. }
  25. ]
  26. }]
  27. },
  28. options: {
  29. scales: {
  30. projection: {
  31. projection: 'albersUsa' // ... projection method
  32. }
  33. }
  34. }
  35. };

Styling

The styling of the new element GeoFeature is based on Bar Element with some additional options for the outline and graticule.

see https://github.com/sgratzl/chartjs-chart-geo/blob/main/src/elements/GeoFeature.ts#L41

Legend and Color Scale

The coloring of the nodes will be done with a special color scale. The scale itself is based on a linear scale.

see

Bubble Map

A Bubble Map (chart type: bubbleMap) aka Proportional Symbol is used to render maps with dots that are scaled according to some numerical value. It is based on a regular bubble chart where the positioning is done using latitude and longitude with an additional sizeScale to create a legend for the different radi.

Bubble Map

Open in CodePen

Data Structure

see Bubble Chart. Alternatively to x and y, the following structure can be used:

  1. interface IBubbleMapPoint {
  2. longitude: number;
  3. latitude: number;
  4. value: number;
  5. }

Note: instead of using the r attribute as in a regular bubble chart, the value attribute is used, which is picked up by the sizeScale to convert it to an actual pixel radius value.

Styling

A regular point is used and thus supports the Point Element styling options. In addition, the outline* and graticule* are supported.

Legend

Similar to the choropleth chart a new sizeScale is used to map the values to symbol radius size. The scale itself is based on a linear scale.

see

Scales

A new scale projection is registered and used by default by Choropleth and BubbleMap. The available methods are the one from https://github.com/d3/d3-geo#projections. Just remove the geo prefix. Alternatively, the projection method instance can be directly given.

see https://github.com/sgratzl/chartjs-chart-geo/blob/main/src/scales/ProjectionScale.ts#L76

ESM and Tree Shaking

The ESM build of the library supports tree shaking thus having no side effects. As a consequence the chart.js library won’t be automatically manipulated nor new controllers automatically registered. One has to manually import and register them.

Variant A:

  1. import { Chart } from 'chart.js';
  2. import { ChoroplethController, GeoFeature, ColorScale, ProjectionScale } from 'chartjs-chart-geo';
  3. // register controller in chart.js and ensure the defaults are set
  4. Chart.register(ChoroplethController, GeoFeature, ColorScale, ProjectionScale);
  5. const chart = new Chart(document.getElementById('canvas').getContext('2d'), {
  6. type: 'choropleth',
  7. data: {
  8. // ...
  9. },
  10. });

Variant B:

  1. import { ChoroplethChart } from 'chartjs-chart-geo';
  2. const chart = new ChoroplethChart(document.getElementById('canvas').getContext('2d'), {
  3. data: {
  4. //...
  5. },
  6. });

Development Environment

  1. npm i -g yarn
  2. yarn install
  3. yarn sdks vscode

Common commands

  1. yarn compile
  2. yarn test
  3. yarn lint
  4. yarn fix
  5. yarn build
  6. yarn docs