简体   繁体   中英

How to use chartjs-plugin-datalabels for specific charts using vue-chart.js

I'm trying to use chartjs-plugin-datalables for specific charts using vue-chartjs . So firstly i have unregistered the plugin globally from my main.js file as it is registered globally when imported.

import Chart from 'chart.js';
import ChartDataLabels from 'chartjs-plugin-datalabels';

Chart.defaults.global.plugins.datalabels.display = false;

Then i'm adding the plugin for specific chart. But no labels are showing.

import { Pie } from "vue-chartjs";
import ChartDataLabels from "chartjs-plugin-datalabels";
export default {
    extends: Pie,
    methods: {
        intChart() {
            var _this = this;
            this.renderChart({
                ..........       
            });
        }
    },

    mounted() {
        this.addPlugin(ChartDataLabels);
        this.intChart();
    }
};

If i disable the following line it works for all the charts.

Chart.defaults.global.plugins.datalabels.display = false;

Any idea why isn't it working for specific charts?

To disable the chartjs-plugin-datalabels globally i had to use

Chart.plugins.unregister(ChartDataLabels);

instead of

Chart.defaults.global.plugins.datalabels.display = false;

in my main.js file. Then addPlugin function works perfectly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM