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've done something similar here -> https://github.com/jerairrest/react-chartjs-2/issues/74

jerairrest avatar Sep 28 '17 13:09 jerairrest

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 ?

fabianbaier avatar Oct 10 '17 05:10 fabianbaier

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.

jerairrest avatar Oct 12 '17 13:10 jerairrest

@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();
            }
        }
    }
});

jerairrest avatar Oct 12 '17 13:10 jerairrest

I'd also be interested to see an example implementation with a pre-built plugin like Data Labels

brianbolnick avatar Oct 13 '17 05:10 brianbolnick

@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.

screen shot 2017-11-16 at 8 14 56 pm

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.

dbrodie122 avatar Nov 17 '17 01:11 dbrodie122

@DennisKo

import ChartComponent from 'react-chartjs-2';
import 'chartjs-funnel';
<ChartComponent
  {...props}
  ref={ref => this.chartInstance = ref && ref.chartInstance}
  type="funnel"
/>

elmeister avatar Jun 08 '18 02:06 elmeister

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); } }] } }}/>

screen shot 2018-06-25 at 5 49 20 pm

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.

cwkeam avatar Jun 25 '18 21:06 cwkeam

Any update on this? chartjs-plugin-annotation still doesn't work with the current version.

terryma avatar Oct 17 '18 23:10 terryma

Nvm this actually works, it just need an import inside the component import "chartjs-plugin-annotation";

terryma avatar Oct 17 '18 23:10 terryma

@terryma and @codeandproduce How did you make it work? Edit: I needed to update ChartJS to 2.7.2

viniciusfont avatar Oct 26 '18 14:10 viniciusfont

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

kkammili avatar Nov 01 '18 22:11 kkammili

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)

StijnVh avatar Nov 06 '18 15:11 StijnVh

I've installed the latest versions of the above packages, but still have no luck... Can anyone help with this??

austinderek avatar Nov 06 '18 21:11 austinderek

@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

Rennzie avatar Mar 20 '19 12:03 Rennzie

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.

mani-coder avatar Jun 12 '19 22:06 mani-coder

@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 avatar Oct 28 '19 13:10 phhoffmann

@phhoffmann please post a code example or sandbox which reproduces your problem

pringelmann avatar Dec 11 '19 05:12 pringelmann

Please does anyone know how to get the funnel to work with react-chartjs-2?

SirPhemmiey avatar Jan 05 '20 07:01 SirPhemmiey

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

mychart

cndlhvn avatar Jan 29 '20 07:01 cndlhvn