Chart Options in Use
Here's a snippet of my current chart options related to the Y-axis:
Code: Select all
{
yAxis: {
opposite: false,
gridLineColor: "#F3F4F5",
tickWidth: 0,
gridLineWidth: 1,
labels: {
formatter: function () {
return '$' + numCompact(this.value, 0);
},
y: 6,
style: {
fontSize: 15,
color: "#606367",
},
},
}
}
I'm using the following function to format the Y-axis labels in a more compact form, like "1.2k", "1.2m", etc.
Code: Select all
function numCompact(num: number, precision = 2) {
const map = [
{ suffix: "t", threshold: 1e12 },
{ suffix: "b", threshold: 1e9 },
{ suffix: "m", threshold: 1e6 },
{ suffix: "k", threshold: 1e3 },
{ suffix: "", threshold: 1 },
];
const found = map.find((x) => Math.abs(num) >= x.threshold);
if (found) {
const formatted = (num / found.threshold).toFixed(precision) + found.suffix;
return formatted;
}
return num;
}
With the current setup, I've fixed the precision parameter to zero in the numCompact() method. As a result, there is a possibility of having identical labels on the Y-axis. I'm looking for a way to dynamically adjust this precision parameter based on the values displayed to ensure uniqueness among Y-axis labels.
Questions
1. Is there a native Highcharts way to achieve this level of dynamic customization for Y-axis labels?
2. If not, can you recommend an optimal strategy to feed a dynamic precision parameter into numCompact() to ensure unique Y-axis labels? This would require knowing all the label values ahead of time, and I haven't found a way to get that in the docs.
I would really appreciate your expertise on this matter. Thank you in advance for your assistance.