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.

no access to chart data in legendCallback

See original GitHub issue

Expected Behavior

Passing in the chart as the argument gives you the nested config object, which in turn has the data object used to create the chart, with datasets and labels nested inside, from which the custom legend can be generated. This is how I did it in another (non-vue) chart.js project.

Actual Behavior

chart.config.data.datasets and .labels are empty. My chart otherwise seems to work just fine, it renders correctly and is properly affected by my options object.

wrapper.vue

<template>
  <div>
      <BarChart
      :options="chartOptions"
      :chart-data="chartData">
      </BarChart>
  </div>
</template>

<script>
import BarChart from './BarChart.vue';

export default {
  data() {
    return {
      chartData: null,
      chartOptions: {
        legendCallback(barChart) {
          console.log(barChart);  <-- barChart.config.data does not have the chart data/labels as expected
        },
        legend: {
          display: false,
        },
      },
    };
  },
  components: {
    BarChart,
  },

BarChart.vue

<script>
import { Bar, mixins } from 'vue-chartjs';

const { reactiveProp } = mixins;

export default {
  extends: Bar,
  mixins: [reactiveProp],
  props: ['options'],
  mounted() {
    this.renderChart(this.chartData, this.options);
    this.$data._chart.generateLegend();
  },
  name: 'BarChart',
};
</script>

Environment

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

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:11 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
jamessouthcommented, Aug 4, 2018

As a workaround, I moved the legendCallback out of the options object and into methods in the wrapper, then in my API call method, right after setting this.chartData with the API data, I call legendCallback with said data. legendCallback then creates a ul and appends to the div holding the chart.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Chart.js legendcallback - Stack Overflow
So here is a JSFiddle that has the solution I am looking for but with something missing that I am trying to achieve....
Read more >
Using Chart.js's legendCallback and generateLegend() with ...
I simply access the relevant index in the chart instance's data and toggle it's 'hidden' attribute and then toggle the element's class name...
Read more >
How to modify legend completely in Chart JS | Chart.JS 2.9.x
Chapters. View all · Intro · Intro · Intro · Legend callback · Legend callback · Legend callback · Extracting the data ·...
Read more >
Legend | Chart.js
Legend. The chart legend displays data about the datasets that are appearing on the chart. # Configuration options.
Read more >
Modify chart legend entries - Microsoft Support
For additional editing options, or to modify legend entries without affecting the worksheet data, you can make changes to the legend entries in...
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