3π
β
It looks like you are trying to implement the annotations plugin as if you would integrate it to a current chart.js version. E.g. your define your annotations in plugins
.
This is an example that works with the given versions. Please note that I added the annotation
object right away in options
what gives the wanted annotation. (In my example in form of a red line.)
import React, { useEffect, useRef } from "react";
import Chart from "chart.js";
import annotationPlugin from "chartjs-plugin-annotation";
Chart.pluginService.register(annotationPlugin);
const LineGraph = () => {
const chartRef = useRef(null);
useEffect(() => {
if (chartRef?.current) {
const chartToDraw = chartRef.current.getContext("2d");
new Chart(chartToDraw, {
type: "line",
data: {
labels: ["Jan", "Feb", "March"],
datasets: [
{
label: "Sales",
data: [86, 67, 91]
}
]
},
options: {
annotation: {
drawTime: "afterDatasetsDraw", // (default)
events: ["click"],
dblClickSpeed: 350, // ms (default)
annotations: [
{
drawTime: "afterDraw",
id: "a-line-1",
type: "line",
mode: "horizontal",
scaleID: "y-axis-0",
value: "25",
borderColor: "red",
borderWidth: 2
}
]
}
}
});
}
}, []);
return (
<div className={{ width: "100%", height: 500 }}>
<canvas id="myChart" ref={chartRef} />
</div>
);
};
export default LineGraph;
You can see it working here:
https://codesandbox.io/s/chart-js-2-9-4-annotation-example-f3h7d?file=/src/LineGraph.js
Chart.js plugin annotation documentation for 0.5.7:
https://www.chartjs.org/chartjs-plugin-annotation/0.5.7/
Source:stackexchange.com