Skip to content

Commit 2a20eb8

Browse files
authored
fix: legacy multiple annotations (#910)
* Multiple charts with annotations Handle multiple instances of a `vue-chartjs` wrapper component when the annotations plugin is in use * Update index.md
1 parent 6df3c69 commit 2a20eb8

File tree

2 files changed

+6
-9
lines changed

2 files changed

+6
-9
lines changed

legacy/src/Charts.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import {
2626
const ANNOTATION_PLUGIN_KEY = 'annotation'
2727

2828
export function generateChart(chartId, chartType, chartController) {
29-
let _chartRef = null
29+
let _chartRef = {}
3030

3131
return {
3232
props: {
@@ -69,7 +69,8 @@ export function generateChart(chartId, chartType, chartController) {
6969
},
7070
data() {
7171
return {
72-
_chart: null
72+
_chart: null,
73+
_id: Math.random().toString(36).substring(2),
7374
}
7475
},
7576
computed: {
@@ -84,7 +85,7 @@ export function generateChart(chartId, chartType, chartController) {
8485
ChartJS.register(chartController)
8586
},
8687
mounted() {
87-
_chartRef = { current: null }
88+
_chartRef[this.$data._id] = null
8889

8990
if ('datasets' in this.chartData && this.chartData.datasets.length > 0) {
9091
chartCreate(this.renderChart, this.chartData, this.chartOptions)
@@ -185,11 +186,11 @@ export function generateChart(chartId, chartType, chartController) {
185186
chartUpdate(currentChart)
186187
},
187188
getCurrentChart() {
188-
return this.hasAnnotationPlugin ? _chartRef.current : this.$data._chart
189+
return this.hasAnnotationPlugin ? _chartRef[this.$data._id] : this.$data._chart
189190
},
190191
setCurrentChart(chart) {
191192
this.hasAnnotationPlugin
192-
? (_chartRef.current = chart)
193+
? (_chartRef[this.$data._id] = chart)
193194
: (this.$data._chart = chart)
194195
}
195196
},

website/src/guide/index.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -321,10 +321,6 @@ Charts will emit events if the data changes. You can listen to them in the chart
321321
- `chart:updated` - if the update handler performs an update instead of a re-render
322322
- `labels:updated` - if new labels were set
323323

324-
## chartjs-plugin-annotation
325-
326-
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.
327-
328324
## Examples
329325

330326
### Chart with props

0 commit comments

Comments
 (0)