Basic
const config = {
type: 'line',
data,
options: {
plugins: {
annotation: {
annotations: {
annotation1,
annotation2,
annotation3
}
}
}
}
};
const annotation1 = {
type: 'polygon',
backgroundColor: 'rgba(0, 255, 255, 0.4)',
borderColor: 'black',
borderWidth: 3,
radius: 25,
scaleID: 'y',
xValue: (ctx) => value(ctx, 0, 2, 'x'),
yValue: (ctx) => value(ctx, 0, 2, 'y')
};
const annotation2 = {
type: 'polygon',
backgroundColor: 'transparent',
borderColor: 'red',
borderWidth: 5,
radius: 25,
scaleID: 'y',
sides: 5,
xValue: (ctx) => value(ctx, 1, 4, 'x'),
yValue: (ctx) => value(ctx, 1, 4, 'y')
};
const annotation3 = {
type: 'polygon',
backgroundColor: 'transparent',
borderColor: 'gray',
borderWidth: 3,
radius: 30,
sides: 8,
scaleID: 'y',
xValue: (ctx) => value(ctx, 1, 6, 'x'),
yValue: (ctx) => value(ctx, 1, 6, 'y')
};
function value(ctx, datasetIndex, index, prop) {
const meta = ctx.chart.getDatasetMeta(datasetIndex);
const parsed = meta.controller.getParsed(index);
return parsed ? parsed[prop] : NaN;
}
const DATA_COUNT = 8;
const MIN = 10;
const MAX = 100;
Utils.srand(5);
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)
}, {
data: Utils.numbers(numberCfg)
}, {
data: Utils.numbers(numberCfg)
}]
};