Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VueChart.js with inline plugins example #277

Closed
winou75 opened this issue Dec 13, 2017 · 9 comments
Closed

VueChart.js with inline plugins example #277

winou75 opened this issue Dec 13, 2017 · 9 comments

Comments

@winou75
Copy link

winou75 commented Dec 13, 2017

Hi guys, it might not be the place to ask this kind of question, but as a newbie, I don't really understand how to use plugins ...

I'd like to add this plugin PieceLabel to my component below.

export default {
  name: 'pie-chart',
  mixins: [reactiveProp],
  extends: Pie,
  props: {
    options: Object,
    chartData: Object,
  },
  mounted() {
    const { options, chartData, renderChart } = this

    this.addPlugin({
    id: 'pieceLabel',
      beforeInit: function (chart) {
        console.log(chart)
      }
    })

    options: {
      pieceLabel: {
        mode: 'label',
        precision: 1
      },
   }
    renderChart(chartData, params)
  },
}

The issue is how does it recognize the id ??
Thanks a lot

@apertureless
Copy link
Owner

Well, I guess you have to install the plugin over npm
npm install chart.piecelabel.js

Then import or require it

import * as PriceLabel from 'chart.piecelabel.js' 
// If the import is not working you could try to require it
const PriceLabel = require('chart.pricelabel.js')

and then add it as a plugin

this.addPlugin(PriceLabel)

This would be the approch for a per chart basis. To register it globally for all charts (that are supported by that plugin)

You have to import the chart instance fist.

import Chart from 'chart.js'
Chart.plugins.register(PriceLabel)

@winou75
Copy link
Author

winou75 commented Dec 13, 2017

you are a marvellous person.

@winou75 winou75 closed this as completed Dec 13, 2017
@ebisbe
Copy link

ebisbe commented Aug 8, 2018

I think this should be in the docs. And also how to pass configuration to the plugin

@apertureless
Copy link
Owner

@ebisbe not sure what you mean.
There is a section how to add inline plugins in the docs:

https://vue-chartjs.org/#/home?id=inline-plugins

And also the plugins prop if you want to pass it to your chart:

https://vue-chartjs.org/#/home?id=props

@ebisbe
Copy link

ebisbe commented Aug 8, 2018

Edit:
I've found the issue. It's releated to the plugin it self.

@apertureless I'm trying to use annotation plugin but I'm not really being able to use it. So far I have a custom component that wraps LineChart with
import annotation from 'chartjs-plugin-annotation'
and

 mounted() {
    this.addPlugin(annotation)
    this.renderChart(this.applyColors(this.chartData), this.chartOptions)
  },

But I'm not sure where or how I have to pass the Annotations configuration.

@apertureless
Copy link
Owner

apertureless commented Aug 8, 2018

I guess you are using this one? https://github.com/chartjs/chartjs-plugin-annotation

Well, they say:

To configure the annotations plugin, you can simply add new config options to your chart config.

So have you tried this?

const options = {
	responsive: true,
	title: {
		display: true,
		text: 'Put your other options in here'
	}
	annotation: {
		events: ['click'],
		annotations: []
	}
}


this.renderChart(data, options)

@ebisbe
Copy link

ebisbe commented Aug 8, 2018

Yes, I figured it out. There's an issue with using array labels [['lable','text'],['label2','text2']]. I've just switched to ['label', 'label2'].

thanks, anyway

@mreduar
Copy link

mreduar commented Feb 14, 2023

@ebisbe not sure what you mean. There is a section how to add inline plugins in the docs:

https://vue-chartjs.org/#/home?id=inline-plugins

And also the plugins prop if you want to pass it to your chart:

https://vue-chartjs.org/#/home?id=props

Greetings, this is no longer available and is already on V5, how to add inline plugins now? would like to do this:

plugins: [{
   beforeInit: function(chart) {
      chart.data.labels.forEach((e, i, a) => {
         if (/\n/.test(e)) {
            a[i] = e.split(/\n/);
         }
      });
   }
}]

@apertureless
Copy link
Owner

@mreduar
You can still pass plugins over the plugin prop to the chart. https://vue-chartjs.org/api/#props
Should work pretty much the same way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants