Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save MJefferson/1b1a280a0537e1b0ed40962ca5975554 to your computer and use it in GitHub Desktop.

Select an option

Save MJefferson/1b1a280a0537e1b0ed40962ca5975554 to your computer and use it in GitHub Desktop.
Chart.js Geo Plugin - Choropleth USA
<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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment