Skip to content

Instantly share code, notes, and snippets.

@MJefferson
Created March 16, 2021 14:18
Show Gist options
  • Select an option

  • Save MJefferson/2646c7f59075f07e2576b9c6b3e677da to your computer and use it in GitHub Desktop.

Select an option

Save MJefferson/2646c7f59075f07e2576b9c6b3e677da to your computer and use it in GitHub Desktop.
Choropleth - Chart.js
<canvas id="canvas"></canvas>
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"
}
}
}
}
}
);
});
<script src="https://unpkg.com/chart.js"></script>
<script src="https://unpkg.com/chartjs-chart-geo"></script>
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