Skip to content

Commit

Permalink
fix: legacy multiple annotations (#910)
Browse files Browse the repository at this point in the history
* Multiple charts with annotations

Handle multiple instances of a `vue-chartjs` wrapper component when the annotations plugin is in use

* Update index.md
  • Loading branch information
kidGodzilla committed Sep 26, 2022
1 parent 6df3c69 commit 2a20eb8
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 9 deletions.
11 changes: 6 additions & 5 deletions legacy/src/Charts.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {
const ANNOTATION_PLUGIN_KEY = 'annotation'

export function generateChart(chartId, chartType, chartController) {
let _chartRef = null
let _chartRef = {}

return {
props: {
Expand Down Expand Up @@ -69,7 +69,8 @@ export function generateChart(chartId, chartType, chartController) {
},
data() {
return {
_chart: null
_chart: null,
_id: Math.random().toString(36).substring(2),
}
},
computed: {
Expand All @@ -84,7 +85,7 @@ export function generateChart(chartId, chartType, chartController) {
ChartJS.register(chartController)
},
mounted() {
_chartRef = { current: null }
_chartRef[this.$data._id] = null

if ('datasets' in this.chartData && this.chartData.datasets.length > 0) {
chartCreate(this.renderChart, this.chartData, this.chartOptions)
Expand Down Expand Up @@ -185,11 +186,11 @@ export function generateChart(chartId, chartType, chartController) {
chartUpdate(currentChart)
},
getCurrentChart() {
return this.hasAnnotationPlugin ? _chartRef.current : this.$data._chart
return this.hasAnnotationPlugin ? _chartRef[this.$data._id] : this.$data._chart
},
setCurrentChart(chart) {
this.hasAnnotationPlugin
? (_chartRef.current = chart)
? (_chartRef[this.$data._id] = chart)
: (this.$data._chart = chart)
}
},
Expand Down
4 changes: 0 additions & 4 deletions website/src/guide/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -321,10 +321,6 @@ Charts will emit events if the data changes. You can listen to them in the chart
- `chart:updated` - if the update handler performs an update instead of a re-render
- `labels:updated` - if new labels were set

## chartjs-plugin-annotation

When using [chartjs-plugin-annotation](https://www.chartjs.org/chartjs-plugin-annotation/latest/) and **Vue 2** simultaneously, you will not be able to place multiple reactive charts on one page.

## Examples

### Chart with props
Expand Down

0 comments on commit 2a20eb8

Please sign in to comment.