no access to chart data in legendCallback
See original GitHub issueExpected 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:
- Created 5 years ago
- Comments:11 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.
@gtzures oops I changed the repo name:
https://github.com/jamessouth/dashboard/tree/master/src/components
https://jamessouth.github.io/dashboard/