Skip to content

Commit

Permalink
fix(legacy-charts): fix render multiple charts at one page (#803)
Browse files Browse the repository at this point in the history
* fix(legacy-charts): fix render multiple charts at one page

fix the error, when multiple charts can't render on one page

fix #800 #801

* fix: change syntax
  • Loading branch information
thabarbados authored Apr 7, 2022
1 parent 3ad8bc2 commit fe97040
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 deletions legacy/src/Charts.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
} from '../../src/utils'

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

return {
props: {
Expand Down Expand Up @@ -68,6 +68,8 @@ export function generateChart(chartId, chartType, chartController) {
ChartJS.register(chartController)
},
mounted() {
_chartRef = { current: null }

if ('datasets' in this.chartData && this.chartData.datasets.length > 0) {
chartCreate(this.renderChart, this.chartData, this.chartOptions)
this.$emit(ChartEmits.ChartRendered)
Expand All @@ -80,8 +82,8 @@ export function generateChart(chartId, chartType, chartController) {
},
methods: {
renderChart(data, options) {
if (_chart !== null) {
chartDestroy(_chart)
if (_chartRef?.current !== null) {
chartDestroy(_chartRef.current)
this.$emit(ChartEmits.ChartDestroyed)
}

Expand All @@ -93,7 +95,7 @@ export function generateChart(chartId, chartType, chartController) {
const canvasEl2DContext = this.$refs.canvas.getContext('2d')

if (canvasEl2DContext !== null) {
_chart = new ChartJS(canvasEl2DContext, {
_chartRef.current = new ChartJS(canvasEl2DContext, {
type: chartType,
data: chartData,
options,
Expand All @@ -109,28 +111,28 @@ export function generateChart(chartId, chartType, chartController) {
if (Object.keys(oldData).length > 0) {
const isEqualLabelsAndDatasetsLength = compareData(newData, oldData)

if (isEqualLabelsAndDatasetsLength && _chart !== null) {
setChartDatasets(_chart.data, newData, this.datasetIdKey)
if (isEqualLabelsAndDatasetsLength && _chartRef?.current !== null) {
setChartDatasets(_chartRef.current.data, newData, this.datasetIdKey)

if (newData.labels !== undefined) {
setChartLabels(_chart, newData.labels)
setChartLabels(_chartRef.current, newData.labels)
this.$emit(ChartEmits.LabelsUpdated)
}

chartUpdate(_chart)
chartUpdate(_chartRef.current)
this.$emit(ChartEmits.ChartUpdated)
} else {
if (_chart !== null) {
chartDestroy(_chart)
if (_chartRef?.current !== null) {
chartDestroy(_chartRef.currentt)
this.$emit(ChartEmits.ChartDestroyed)
}

chartCreate(this.renderChart, this.chartData, this.chartOptions)
this.$emit(ChartEmits.ChartRendered)
}
} else {
if (_chart !== null) {
chartDestroy(_chart)
if (_chartRef?.current !== null) {
chartDestroy(_chartRef.current)
this.$emit(ChartEmits.ChartDestroyed)
}

Expand All @@ -140,8 +142,8 @@ export function generateChart(chartId, chartType, chartController) {
}
},
beforeDestroy() {
if (_chart !== null) {
chartDestroy(_chart)
if (_chartRef?.current !== null) {
chartDestroy(_chartRef.current)
this.$emit(ChartEmits.ChartDestroyed)
}
},
Expand Down

0 comments on commit fe97040

Please sign in to comment.