A Pen by Martial Jefferson on CodePen.
Created
March 16, 2021 14:18
-
-
Save MJefferson/2646c7f59075f07e2576b9c6b3e677da to your computer and use it in GitHub Desktop.
Choropleth - Chart.js
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; | |
| display: flex; | |
| align-items: center; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment