score:2
Accepted answer
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: stackoverflow.com
Related Query
- Chartjs v7.0 with plugin zoom, strange effect when use "drag" mode.
- Use chartjs-plugin-annotation 0.5.7 in Chartjs 2.9.x with React
- How to properly use the chartjs datalabels plugin
- how to get React chartjs to resize back down with window
- Use Chartjs with reactjs
- how to use chart.js with react Hook?
- Use transparent stroke color with chartjs pie chart
- How to use an Addon with ChartJS on Angular 4
- Graph streaming real-time data with react and chartjs
- How to use an Aurelia JSPM plugin with WebPack
- Chartjs with zoom plugin zooms too much with wheel just with single turn
- Chartjs not working with d3 from csv source
- React component wont re-render ChartJS chart with redux props
- What kind of graph could I use to achieve this with ChartJS (or similar)?
- Can't Draw Horizontal Line on Graph Using ChartJS Annotation Plugin and Primevue Chart
- Cannot read property 'length' of undefined for ChartJS when I use it inside React
- chartjs with the plugin chartjs-plugin-dragdata can i set fix dropdown values
- Updating Chartjs to 2.5 with custom code
- Styling background (fill) with ChartJs and React
- Trying to export chart with Chartjs and React but getting erorr
- Can i use crosshairs with OHLC/candlestick charts using ChartJS
- add a unit to label with chartjs plugin datalabels
- Use chartjs with rails
- i can't create a line vertically with react annotation
- Can I use chartJS for drawing a stock map with rectangle?
- Annotation plugin not working with Chart.js 2.8.0
- I want to use an object to help create a chart with Chartjs
- Chartjs annotation plugin colored box colors get summed after switching back and forth
- 'require is not defined' error when attempting to use chartjs in javascript code
- React - how to get array from store before first render and use it as dataset for ChartJS
More Query from same tag
- Problem displaying two graphs on a single chart using javascript and chart.js
- How we can set our own colors for each label in charts
- Angular-charts/Chart.js is not working
- Ajax call to trigger a controller function
- show more dataset than labels in chart.js
- Hide all labels and tooltips in Chart.js and make it very small size
- Chart.js only displays font changes upon hover
- Scatter plot in Chart.js with annotations
- Is it possible to add a drop shadow to chart.js line chart?
- Display Chart.js chart in Node.js
- Creating mixed Bar Chart with ReactJS using recharts or react-chartjs-2
- How to use Real time chart in react?
- Mixed chart scatter plot with chart.js
- My for loop does not iterate in Javascript
- chartjs is issue with large amount data
- Chart.js V2 formatting / styling labels
- Multiple charts rendering on top of each other
- How to trigger ChartJS legend onClick with out interrupting its normal working
- How does chart updating works within the context of react?
- Chart.js legend text showing undefined
- angular-chart error on line chart: unable to parse color
- How to line break a label object in ChartsJS (nested arrays won't work)
- Chart.js - Where do I find which components should be registered?
- Drawing a doughnut chart with columns inside to represent hourly stats in chart.js
- Using CHARTJS with a JavaScript Array
- Chart.js - Multiple Doughnut Charts on same Canvas
- Dynamically update the options of a chart in chartjs using Javascript
- Stacked Mixed Horizontal Bar Chart in Charts.Js Axes Positioning
- react-chart-js-2 keep bar width no matter what the number of bar is
- How to change chart.js grid, and axis color?