Map Chart Dynamic Coloring
Example on using MapChart
with real data set for coloring map regions dynamically.
The data set is downloaded with fetch
when the application is started.
It contains population for each country around European area in the year 2018.
;[
{ 'Country Code': 'ALB', Value: 2866376 },
{ 'Country Code': 'AUT', Value: 8840521 },
{ 'Country Code': 'BLR', Value: 9483499 },
]
The application translates this format to one expected by MapChart
:
const regionValuesData = populationData.map((item) => ({
ISO_A3: item['Country Code'],
value: item['Value'],
}))
;[
{ ISO_A3: 'ALB', value: 2866376 },
{ ISO_A3: 'AUT', value: 8840521 },
{ ISO_A3: 'BLR', value: 9483499 },
]
Finally, the data is supplied to MapChart
using the invalidateRegionValues
method:
mapChart.invalidateRegionValues(regionValuesData)
MapChart
can utilize the region values data in two ways:
- Dynamic region coloring based on color look-up table.
- Auto Cursor formatting.
Dynamic region coloring
Dynamic coloring is enabled by setting the regions fill style to a PalettedFill
:
mapChart.setFillStyle(
new PalettedFill({
lut: new LUT({
steps: [
{
value: 1 * 1000 * 1000,
color: ColorRGBA(0, 255, 0),
label: '1 Million',
},
{ value: 10 * 1000 * 1000, color: ColorRGBA(127, 127, 0), label: '' },
{ value: 50 * 1000 * 1000, color: ColorRGBA(255, 127, 0), label: '' },
{
value: 500 * 1000 * 1000,
color: ColorRGBA(255, 0, 0),
label: '500 Million',
},
],
interpolate: true,
// This property is used to specify fallback color for regions which have no data.
color: ColorRGBA(255, 255, 255),
}),
}),
)
Auto cursor formatting
MapChart
cursor formatting is configured with setCursorResultTableFormatter
method:
mapChart.setCursorResultTableFormatter((builder, region, value, longitude, latitude) => {
builder.addRow(region.name)
// Region value is supplied to the formatter function. Note, that it is `undefined` for regions which were not invalidated by the user.
if (value) {
builder.addRow(`Population: `, '', `${(value / (1000 * 1000)).toFixed(1)} million`)
} else {
builder.addRow(`No population data available`)
}
return builder
})
More map examples: