Average
const config = {
type: 'line',
data,
options: {
plugins: {
annotation: {
annotations: {
annotation
}
}
}
}
};
const annotation = {
type: 'line',
borderColor: 'black',
borderDash: [6, 6],
borderDashOffset: 0,
borderWidth: 3,
label: {
enabled: true,
content: (ctx) => 'Average: ' + average(ctx).toFixed(2),
position: 'end'
},
scaleID: 'y',
value: (ctx) => average(ctx)
};
const DATA_COUNT = 8;
const MIN = 10;
const MAX = 100;
Utils.srand(8);
const labels = [];
for (let i = 0; i < DATA_COUNT; ++i) {
labels.push('' + i);
}
const numberCfg = {count: DATA_COUNT, min: MIN, max: MAX};
const data = {
labels: labels,
datasets: [{
data: Utils.numbers(numberCfg)
}]
};
function average(ctx) {
const values = ctx.chart.data.datasets[0].data;
return values.reduce((a, b) => a + b, 0) / values.length;
}