Callout
const config = {
type: 'line',
data,
options: {
scales: {
y: {
beginAtZero: true,
max: 140,
min: 0
}
},
plugins: {
annotation: {
annotations: {
annotation
}
}
}
}
};
const annotation = {
type: 'label',
backgroundColor: 'rgba(245,245,245)',
callout: {
enabled: true,
borderColor: (ctx) => ctx.chart.data.datasets[0].borderColor
},
content: (ctx) => 'Maximum value is ' + maxValue(ctx).toFixed(2),
font: {
size: 16
},
position: {
x: (ctx) => maxIndex(ctx) <= 3 ? 'start' : maxIndex(ctx) >= 10 ? 'end' : 'center',
y: 'center'
},
xAdjust: (ctx) => maxIndex(ctx) <= 3 ? 60 : maxIndex(ctx) >= 10 ? -60 : 0,
xValue: (ctx) => maxLabel(ctx),
yAdjust: -60,
yValue: (ctx) => maxValue(ctx)
};
const DATA_COUNT = 12;
const MIN = 0;
const MAX = 100;
const numberCfg = {count: DATA_COUNT, min: MIN, max: MAX};
const data = {
labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
datasets: [{
data: Utils.numbers(numberCfg)
}]
};
function maxValue(ctx) {
const values = ctx.chart.data.datasets[0].data;
return Math.max(...values);
}
function maxIndex(ctx) {
const max = maxValue(ctx);
const dataset = ctx.chart.data.datasets[0];
return dataset.data.indexOf(max);
}
function maxLabel(ctx) {
return ctx.chart.data.labels[maxIndex(ctx)];
}