Import plugin
Is it possible to integrate a plugin into react-chartjs? For example https://github.com/xch89820/Chart.Funnel.js
I tried using
Chart.pluginService.register({
// something
});
with no success.
I was having a hard time getting this working using a scatter plot and then realized that my scaleID was y-axis-1 not y-axis-0 in case that helps anyone else out there!
I was having a hard time getting this working using a scatter plot and then realized that my scaleID was
y-axis-1noty-axis-0in case that helps anyone else out there!
Contradictory to your solution, I got mine to work when I have y-axis-1 instead of y-axis-0.
Can I use a plugin to make a waterfall chart? like this: https://github.com/everestate/chartjs-plugin-waterfall I am already losing my mind for not being able to create this type of chat.
I realized I was placing the annotation options under plugins property ( like with the zoom plugin ) but with this plugin you need to have it on the root of your options object. Hope this helps someone get unstuck; was super annoying to debug.
I realized I was placing the annotation options under plugins property ( like with the zoom plugin ) but with this plugin you need to have it on the root of your options object. Hope this helps someone get unstuck; was super annoying to debug.
@maxwell-oroark great find! I tried seemingly every solution listed in this thread, and it turns out I had annotation nested under plugins too. Moving it to the outer options worked for me as well. Thank you!
I realized I was placing the annotation options under plugins property ( like with the zoom plugin ) but with this plugin you need to have it on the root of your options object. Hope this helps someone get unstuck; was super annoying to debug.
@maxwell-oroark great find! I tried seemingly every solution listed in this thread, and it turns out I had
annotationnested underpluginstoo. Moving it to the outer options worked for me as well. Thank you!
Doesn't work for me
I'm using Chartjs v3.0 Does anyone know how to get it work with Chartjs v3.0?
I would like to use this plugin to change the line strike through when dataset is hidden.
Instead of having the text-decoration with the line I would like to make the text with more opacity if hidden and viceversa when not hidden.
There are not so many example around and I literally tried a lot of times but with no success, can someone help me please?
See screenshot below for what I would like (opacity instead of red color)

i'm using those versions:
"chart.js": "3.3.2",
"react-chartjs-2": "3.0.3",
"chartjs-plugin-annotation": "1.0.2",
The following code worked for me
import { Chart } from 'chart.js';
import annotationPlugin from 'chartjs-plugin-annotation';
import { Line } from 'react-chartjs-2';
Chart.register(annotationPlugin);
const options = {
scales: {
y: {
type: 'linear',
grace: "5%",
beginAtZero: true,
},
},
plugins: {
annotation: {
annotations: [{
id: "slo",
type: 'line',
mode: 'horizontal',
value:10,
scaleID: "y",
borderWidth: 2,
borderDash: [10, 5],
label: {
enabled: true,
content: `SLO:`,
position: 'start',
}
}
]
}
},
};
<Line data={data} options={options}/>
After looking up from various sources up to @Assem-Hafez answers, I can confirm the similar solutions based on his answer works well too on my Next.js version 10 project + Typescript 4.2.4.
I'm using these following packages:
react-chartjs-2version3.0.5chart.jsversion3.5.1chartjs-plugin-annotationversion1.0.2
You must follow the plugin registration procedure based on this documentation: https://www.chartjs.org/chartjs-plugin-annotation/guide/integration.html#bundlers-webpack-rollup-etc. But instead of using Chart class from chart.js, you can use the one from react-chartjs-2.
Below are the full component page that display a Bar chart component allong with simple annotation:
import React from 'react';
import type * as ChartJs from 'chart.js';
import { Chart, Bar } from 'react-chartjs-2';
import annotationPlugin from 'chartjs-plugin-annotation';
Chart.register(annotationPlugin);
/**
* References:
* https://github.com/reactchartjs/react-chartjs-2/issues/201#issuecomment-853401442
* https://www.chartjs.org/chartjs-plugin-annotation/guide/usage.html
*/
const TestPage: React.FunctionComponent = () => {
const data: ChartJs.ChartData = {
labels: ['40%', '60%', '80%', '100%'],
datasets: [
{
label: '# of Votes',
data: [45, 30, 75, 95],
backgroundColor: [
'#CD7F32',
'#C0C0C0',
'#FFD700',
'#8C92AC',
],
borderColor: [
'rgba(255, 99, 132, 1)',
'rgba(54, 162, 235, 1)',
'rgba(255, 206, 86, 1)',
'rgba(75, 192, 192, 1)',
],
borderWidth: 1,
},
],
};
const options: ChartJs.ChartOptions = {
indexAxis: 'y',
plugins: {
annotation: {
annotations: [{
id: 'box1',
type: 'box',
xMin: 0,
xMax: 40,
yMin: 0,
yMax: 3,
backgroundColor: 'rgba(255, 99, 132, 0.25)'
}]
}
}
}
return (
<div>
<Bar data={data} options={options} />
</div>
)
}
export default TestPage;
i'm using those versions:
"chart.js": "3.3.2", "react-chartjs-2": "3.0.3", "chartjs-plugin-annotation": "1.0.2",The following code worked for me
import { Chart } from 'chart.js'; import annotationPlugin from 'chartjs-plugin-annotation'; import { Line } from 'react-chartjs-2'; Chart.register(annotationPlugin);const options = { scales: { y: { type: 'linear', grace: "5%", beginAtZero: true, }, }, plugins: { annotation: { annotations: [{ id: "slo", type: 'line', mode: 'horizontal', value:10, scaleID: "y", borderWidth: 2, borderDash: [10, 5], label: { enabled: true, content: `SLO:`, position: 'start', } } ] } }, };<Line data={data} options={options}/>
After many hours of trying everything I've found on google and here... This worked!!! Thank you very much!
https://stackoverflow.com/questions/68046847/adding-data-labels-inside-charts-in-reactjs-is-not-working This is the easiest way to resolve! Thanks to the one who asked this and the one who precisely answered this! :)
the solutions do not work for v4