Was thinking that it would be nice to be able to set a range of colors for a heatmap instead of just two (min and max). Like what we do for the gradient list.
Something like...
function am4themes_myHeatmap(target) {
if (target instanceof am4core.ColorSet) {
target.list = [
am4core.color("#F7E3D4"),
am4core.color("#FFC480"),
am4core.color("#DC60BF"),
am4core.color("#A43B7D"),
am4core.color("#5B0A25")
];
}
}
If something like this already exists, I would the love to see it.
Something like this didn't exist.
Unfortunately there's not a simple way to just use
heatRulesandHeatLegendand have them use additional colors. But it's not overly complicated to emulateheatRulesand if you're only using 1markerin yourHeatLegend(i.e. one long bar as opposed to multiple bars), to override its gradient with a custom one.I grabbed 2 colors from the image you provided and threw those and black in an array:
It's not necessary, but can be useful. Those are the colors of a 3-color gradient. I elected to use 3 colors so we can split the calculations evenly between left/right halves of the gradient, it ought to simplify the demo below. The picture you shared might require an additional color in the left half, you would have to adjust calculations accordingly, but it's just as doable.
To emulate
heatRules, we'll provide an adapter formapPolygons'fill. In there, we'll compare amapPolygon'svalueagainst the min/max of the values, the latter can be found via the series'dataItem.values["value"].lowand.high. This will give us a percentage in decimals to grab a color from a range of colors. The utility function to pick a color from a range isam4core.colors.interpolate, its first two arguments areiRGBs (plain object withr,g,b, andaproperties/values), and the third is the percentage in decimals. If the percentage is within the first half, we'll have the adapter return a color between the first two inheatColorsabove, if it's in the second half, we'll return a color from the latter two.Here's what that code looks like:
If you have a 1-marker
heatLegend, i.e. just a bar with a gradient going across, you can make your own gradient and assign it in an adapter, too:If you have multiple markers in a
heatLegend(as per the top heat legend in your picture), custom coloring would be more like what we did for theheatRules, except instead of an adapter, because we need to know their place and there's nodataItemorindexavailable, we'll iterate through themarkersonce they're ready and then override their colors there:I forked our US heat (choropleth) map demo with the above code and then some to get closer to the look/feel of the image you shared:
https://codepen.io/team/amcharts/pen/7fd84c880922a6fc50f80330d778654a