A Pen by Martial Jefferson on CodePen.
Created
March 16, 2021 07:24
-
-
Save MJefferson/1b1a280a0537e1b0ed40962ca5975554 to your computer and use it in GitHub Desktop.
Chart.js Geo Plugin - Choropleth USA
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <canvas id="canvas"></canvas> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| fetch('https://unpkg.com/us-atlas/states-10m.json').then((r) => r.json()).then((us) => { | |
| const nation = ChartGeo.topojson.feature(us, us.objects.nation).features[0]; | |
| const states = ChartGeo.topojson.feature(us, us.objects.states).features; | |
| console.debug(states.map(s => s.properties)) | |
| const stateData = states.map((d) => ({feature: d, value: Math.random() * 10})); | |
| console.dir(stateData); | |
| const chart = new Chart(document.getElementById("canvas").getContext("2d"), { | |
| type: 'choropleth', | |
| data: { | |
| labels: states.map((d) => d.properties.name), | |
| datasets: [{ | |
| label: 'States', | |
| outline: nation, | |
| data: stateData, | |
| }] | |
| }, | |
| options: { | |
| legend: { | |
| display: true | |
| }, | |
| scale: { | |
| projection: 'albersUsa' | |
| }, | |
| geo: { | |
| colorScale: { | |
| display: true, | |
| position: 'bottom', | |
| quantize: 5, | |
| legend: { | |
| position: 'bottom-right', | |
| }, | |
| }, | |
| }, | |
| } | |
| }); | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <script src="https://unpkg.com/chart.js"></script> | |
| <script src="https://unpkg.com/chartjs-chart-geo"></script> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| body { | |
| width: 100vw; | |
| height: 100vh; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment