Attach a custom plugin to Vue-Chart

倾然丶 夕夏残阳落幕 提交于 2021-02-08 08:47:43

问题


We need to attach a custom plugin to vue-chart. Please guide us how to implement on the same

import { Line, mixins } from 'vue-chartjs'
export default {
    namespaced: true,
    extends: Line,
    props: ['chartData', 'options'],
    mounted() {
        this.renderChart(this.chartData, this.chartData.options)
    }
}

This is how we are using the line chart of Vue-chart. How to attach the plugin here

https://blog.larapulse.com/javascript/creating-chart-js-plugins We want to try this. But since we are using vue-chart which internally uses chart.js. Need some help to attach the plugin. please guide us

I want to apply some background color to the chart for one specific colummn in the chart


回答1:


Using the annotation plugin for Chart.js as example, you can use the addPlugin function to attach it:

import { Line, mixins } from 'vue-chartjs'
import chartjsPluginAnnotation from "chartjs-plugin-annotation"

export default {
    namespaced: true,
    extends: Line,
    props: ['chartData', 'options'],
    mounted() {
        //Arguments is an Array of Plugins (https://vue-chartjs.org/api/#addplugin)
        this.addPlugin([chartjsPluginAnnotation]),
        this.renderChart(this.chartData, this.chartData.options)
    }
}

After this just pass the plugin's options on your component as usual. In this case, if you wanted to draw a vertical line, it would be something like this:

   computed: {
        chart() {
          return {
            chartData: {
              labels: this.data.labels,
              datasets: [
                {
                  label: "Score",
                  data: this.data.data
                }
              ],
            options: {
              annotation: {
                annotations: [
                  {
                    type: "line",
                    mode: "vertical",
                    scaleID: "x-axis-0",
                    borderColor: "#4ecca3",
                    value: parseInt(this.data.line),
                    borderDash: [4, 4],
                    label: {
                      content: this.data.line,
                      enabled: true,
                      position: "top",
                      xAdjust: 15,
                      backgroundColor: '#4ecca3',
                      fontSize: 10,
                    }
                  }
                ]
              }
            },
            }
          };
        }



回答2:


vue-chart-js provide method to attach plugins. Use this way:

import the plugin

import ChartDataLabels from 'chartjs-plugin-datalabels';

then, call addPlugin in mounted

 mounted() {
    this.addPlugin(ChartDataLabels);
    this.renderChart(
      this.chartData,
      this.options,
    );
 }

Below is PieChart.vue script in case you create pie chart :

<script>
import { Pie, mixins } from 'vue-chartjs';
import ChartDataLabels from 'chartjs-plugin-datalabels';

Chart.plugins.unregister(ChartDataLabels);

const { reactiveProp } = mixins;

export default {
  extends: Pie,
  mixins: [reactiveProp],
  props: {
    options: {
      type: Object,
      default: null,
    },
  },

  mounted() {
    this.addPlugin(ChartDataLabels);
    this.renderChart(
      this.chartData,
      this.options,
    );
  },
};
</script>



回答3:


import chartjsPluginAnnotation from "chartjs-plugin-annotation";

And:

  mounted() {
    Chart.plugins.register(chartjsPluginAnnotation);
    this.addPlugin(chartjsPluginAnnotation);
    this.renderChart(this.chartData, this.options);
  }


来源:https://stackoverflow.com/questions/56273630/attach-a-custom-plugin-to-vue-chart

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!