react-chartjs-2 icon indicating copy to clipboard operation
react-chartjs-2 copied to clipboard

Import plugin

Open DennisKo opened this issue 9 years ago • 32 comments

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.

DennisKo avatar Sep 28 '17 12:09 DennisKo

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!

jordansoltman avatar Jan 31 '20 20:01 jordansoltman

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!

Contradictory to your solution, I got mine to work when I have y-axis-1 instead of y-axis-0.

Nawal00 avatar Apr 22 '20 15:04 Nawal00

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.

ederfleming avatar Jun 18 '20 14:06 ederfleming

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 avatar Sep 17 '20 20:09 maxwell-oroark

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!

johnpena avatar Nov 09 '20 20:11 johnpena

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!

Doesn't work for me

I'm using Chartjs v3.0 Does anyone know how to get it work with Chartjs v3.0?

paulhayessilex avatar Jan 05 '21 10:01 paulhayessilex

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) Screenshot 2021-03-02 at 09 47 04

Matteobikk90 avatar Mar 02 '21 08:03 Matteobikk90

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}/>

Assem-Hafez avatar Jun 02 '21 21:06 Assem-Hafez

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-2 version 3.0.5
  • chart.js version 3.5.1
  • chartjs-plugin-annotation version 1.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;

Tieantono avatar Sep 20 '21 17:09 Tieantono

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!

scole954387 avatar Feb 13 '22 18:02 scole954387

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! :)

RudrikaJnext avatar Mar 12 '22 05:03 RudrikaJnext

the solutions do not work for v4

aikastor avatar Jun 30 '22 19:06 aikastor