question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using ChartJs-Plugin-Datalabels as a local plugin

See original GitHub issue

I have a Vue application with several Chart types using Chart.js. Iam currently trying to use the Chartjs-Plugin-Datalabels with only one of these charts, the Bar chart.

However, as soon as I import the ChartJS-Plugin-Datalabels plugin, it becomes activated for all of the Chart types in my application, even if I only import it within the BarChart.vue file.

<script>
import { Bar, HorizontalBar, mixins } from "vue-chartjs";
import ChartJSPluginDatalabels from "chartjs-plugin-datalabels"; //Activates in every chart type

export default {
    name: "BarChartWidget",
    mixins: [ mixins.reactiveProp ],
    props: [
        "chartData",
        "options",
    ],
    extends: Bar,
    mounted(){
        this.renderChart(this.chartData, this.options);
    }
}
</script>

I have tried adding this.addPlugin(ChartJSPluginDatalabels); to the mounted function, but this makes no difference.

Is there a way to use this Plugin only in this specific Vue component?

Environment

  • vue.js version: 2.5.16
  • vue-chart.js version: 3.3.2
  • npm version: 5.6.0

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
martinvalcommented, Feb 28, 2019

Hi there @RBrNx,

I’m trying to apply what’s outlined in this thread, but I can’t get the data labels to show just on a single chart.

In my main.js file, this seems to be working fine:

import Chart from 'chart.js'
import ChartJsPluginDataLabels from 'chartjs-plugin-datalabels'
Chart.defaults.global.plugins.datalabels.display = false;

If I disable the last line the labels show on all the charts.

Then in 1 particular chart, I try to do this, but no labels are showing:

<script>
import { Bar } from 'vue-chartjs'
import ChartJsPluginDataLabels from 'chartjs-plugin-datalabels'

export default {
  extends: Bar,
  mounted () {
    this.addPlugin(ChartJsPluginDataLabels);
    this.renderChart({
        ......
    })
  }
}
</script>

Any ideas why this isn’t working ?

1reaction
filsuckcommented, Apr 4, 2019

Working fine with, import Chart from ‘chart.js’; import ChartDataLabels from ‘chartjs-plugin-datalabels’; Chart.plugins.unregister(ChartDataLabels);

Read more comments on GitHub >

github_iconTop Results From Across the Web

Getting Started | chartjs-plugin-datalabels
chartjs -plugin-datalabels must be loaded after the Chart.js library! ... It must be manually registered either globally or locally (see #42 ...
Read more >
Plugins
Plugins. Plugins are the most efficient way to customize or change the default behavior of a chart. They have been introduced at version ......
Read more >
1. How to add chartjs-plugin-datalabels to Chart.JS - YouTube
1 How to add chartjs plugin datalabels in chart.jsIn this video we will explore how to add the chartjs plugin datalabels in Chart.JS....
Read more >
chart.js - ChartDataLabels is globally active for some reason
As described here in the docs if you use Chart.register(ChartDatalabels) you register it as a global plugin which means it applies to all ......
Read more >
How to use React with Chart js and plugins
How To Use label plugin in Chartjs-plugin-data labels. · npm install chartjs-plugin-datalabels --save · import {Chart} from 'chart.js'; import ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found