Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(legacy-charts): fix render multiple charts at one page #803

Merged
merged 2 commits into from Apr 7, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
57 changes: 42 additions & 15 deletions legacy/src/Charts.js
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,12 @@ export function generateChart(chartId, chartType, chartController) {
},
methods: {
renderChart(data, options) {
if (_chart !== null) {
chartDestroy(_chart)
if (
_chartRef !== null &&
'current' in _chartRef &&
_chartRef.current !== null
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
_chartRef !== null &&
'current' in _chartRef &&
_chartRef.current !== null
_chartRef?.current

?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

used

) {
chartDestroy(_chartRef.current)
this.$emit(ChartEmits.ChartDestroyed)
}

Expand All @@ -92,8 +98,12 @@ export function generateChart(chartId, chartType, chartController) {

const canvasEl2DContext = this.$refs.canvas.getContext('2d')

if (canvasEl2DContext !== null) {
_chart = new ChartJS(canvasEl2DContext, {
if (
canvasEl2DContext !== null &&
_chartRef !== null &&
'current' in _chartRef
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed

) {
_chartRef.current = new ChartJS(canvasEl2DContext, {
type: chartType,
data: chartData,
options,
Expand All @@ -109,28 +119,41 @@ 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 !== null &&
'current' in _chartRef &&
_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 !== null &&
'current' in _chartRef &&
_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 !== null &&
'current' in _chartRef &&
_chartRef.current !== null
) {
chartDestroy(_chartRef.current)
this.$emit(ChartEmits.ChartDestroyed)
}

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