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've done something similar here -> https://github.com/jerairrest/react-chartjs-2/issues/74
Related to that topic, I am looking for a solution to draw a vertical line like here: http://jsfiddle.net/dbyze2ga/14/
I found this plugin https://github.com/chartjs/chartjs-plugin-annotation how would I be able to import and use it correctly ?
You should be able to just register a custom plugin to the Chart instance. As for how to import a plugin I'm not 100% how we should accomplish this. You can pass the extra options to the chart instance for sure.
@fabianbaier The vertical line can be drawn with a custom plugin as follows:
Chart.pluginService.register({
id: 'threshold',
afterDraw: (chart, easing) => {
if (typeof chart.chart.config.data.datasets[0].install != 'undefined') {
if (chart.chart.config.data.datasets[0].install != '-1') {
var meta = chart.getDatasetMeta(0);
var x = meta.data[chart.chart.config.data.datasets[0].install]._model.x;
chart.chart.ctx.restore();
chart.chart.ctx.beginPath();
chart.chart.ctx.setLineDash([5, 5]);
chart.chart.ctx.strokeStyle = '#000000';
chart.chart.ctx.moveTo(x, 0);
chart.chart.ctx.lineTo(x, 10000);
chart.chart.ctx.stroke();
}
}
}
});
I'd also be interested to see an example implementation with a pre-built plugin like Data Labels
@brianbolnick I had the same question about the Data Labels plugin and got it to work, though maybe not precisely how it was intended.
npm install chartjs-plugin-datalabels
The documentation had no references (that I saw) as to the proper way to import and use it via npm, so I just tried import 'chartjs-plugin-datalabels' in the container component that held my charts.
Voila! Data labels on my charts.
After that, you can pass the plugins property to your options object for your chart, and set things how you like according to the docs
const options = {
responsive: true,
plugins: {
datalabels: {
color: 'black'
}
}
}
Class PieChart extends React.Component {
...
render(){
return(
<div>
<Pie data={data} options={options}/>
</div>
)
}
}
This is the simplest solution I've found so far.
@DennisKo
import ChartComponent from 'react-chartjs-2';
import 'chartjs-funnel';
<ChartComponent
{...props}
ref={ref => this.chartInstance = ref && ref.chartInstance}
type="funnel"
/>
That method is not working for me with: 'chartjs-plugin-annotation'
import 'chartjs-plugin-annotation'; ...
<Line
...
options={{
maintainAspectRatio: false,
lineTension: 0,
...
plugins:{
annotation: {
events: ["click"],
annotations: [
{
drawTime: "afterDatasetsDraw",
id: "hline",
type: "line",
mode: "horizontal",
scaleID: "y-axis-0",
value: 30,
borderColor: "black",
borderWidth: 5,
label: {
backgroundColor: "red",
content: "Test Label",
enabled: true
},
onClick: function(e) {
// The annotation is is bound to the this variable
console.log("Annotation", e.type, this);
}
}]
}
}}/>

I simply can not see the horizontal line.
I copied the code for annotations from: https://codepen.io/anon/pen/aKjevd which does basically what I need mine to do.
Any update on this? chartjs-plugin-annotation still doesn't work with the current version.
Nvm this actually works, it just need an import inside the component import "chartjs-plugin-annotation";
@terryma and @codeandproduce How did you make it work? Edit: I needed to update ChartJS to 2.7.2
It worked for me if my options object for react-charts-2 look like this
options={{
responsive:true,
annotation: {
annotations: [
{
// drawTime: "afterDatasetsDraw",
// id: "hline",
type: "line",
mode: "horizontal",
scaleID: "y-axis-0",
value: 129,
borderColor: "black",
borderWidth: 10,
label: {
backgroundColor: "red",
content: "Test Label",
enabled: true
}
}
]
}
}}
But still my label as well as line is cutting down into half when I set the line on y-axis is greater than the graph allocated values and it works perfectly fine when my straight is touching the plotted curve
I got the annotations plugin up and running. Please ensure you are using the latest versions of the packages:
- chart.js: 2.7.3
- chartjs-plugin-annotation: 0.5.7
- react-chartjs-2: 2.7.4
When using chart.js version 2.7.2 it did not work (no error in console, but no line drawn/visible in the chart).
As pointed out in earlier posts, you should also import the chartjs-plugin-annotation:
import * as React from 'react';
import * as chartjs from 'chart.js';
import { Bar, ChartData, LinearComponentProps } from 'react-chartjs-2';
import 'chartjs-plugin-annotation';
Then you can add the annotation to the chart options object (for available configuration options, see https://github.com/chartjs/chartjs-plugin-annotation)
I've installed the latest versions of the above packages, but still have no luck... Can anyone help with this??
@austinderek did you have any luck in the end with getting the annotation to render? I cannot for the life of me get mine to show up.
EDIT: Not to worry, I managed to get it to work by copying the example from Raju10100
It worked me if my options object for react-charts-2 look like this
options={{ responsive:true, annotation: { annotations: [ { // drawTime: "afterDatasetsDraw", // id: "hline", type: "line", mode: "horizontal", scaleID: "y-axis-0", value: 129, borderColor: "black", borderWidth: 10, label: { backgroundColor: "red", content: "Test Label", enabled: true } } ] } }}But still my label as well as line is cutting down into half when I set the line on y-axis is greater than the graph allocated values and it works perfectly fine when my straight is touching the plotted curve
this worked for me finally.
@DennisKo did you get the funnel to work ? I can't make it appear on my project :/ I've tried some things that people said with the annotation plugin but couldn't make it work either..
@phhoffmann please post a code example or sandbox which reproduces your problem
Please does anyone know how to get the funnel to work with react-chartjs-2?
Hi everyone, In my case, chartjs-plugin-annotation plugin works well with this way.
import React from 'react'
import { Bar } from 'react-chartjs-2'
import * as ChartAnnotation from 'chartjs-plugin-annotation'
class MyChart extends React.Component {
render() {
const data = {
datasets: [
{
label: 'number',
fill: true,
beginAtZero: true,
data: [0, 3, 5, 1, 7, 5, 7, 4, 9],
},
],
labels: ['1', '2', '3', '4', '5', '6', '7', '8', '9'],
}
const options = {
annotation: {
annotations: [
{
type: 'line',
mode: 'horizontal',
scaleID: 'y-axis-0',
value: 6,
borderColor: 'black',
borderWidth: 10,
label: {
backgroundColor: 'red',
content: 'Test Label',
enabled: true,
},
},
],
},
}
return <Bar data={data} width={100} height={50} options={options} plugins={ChartAnnotation} />
}
}
export default MyChart
