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): multi-chart data change bug #814

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
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
42 changes: 27 additions & 15 deletions legacy/src/Charts.js
Expand Up @@ -23,7 +23,7 @@ import {
} from '../../src/utils'

export function generateChart(chartId, chartType, chartController) {
let _chartRef = null
// let _chartRef = null
Copy link
Collaborator

Choose a reason for hiding this comment

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

please remove comment


return {
props: {
Expand Down Expand Up @@ -64,11 +64,16 @@ export function generateChart(chartId, chartType, chartController) {
default: () => []
}
},
data() {
return {
chartRef: null
}
},
created() {
ChartJS.register(chartController)
},
mounted() {
_chartRef = { current: null }
this.chartRef = { current: null }

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

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

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

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

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

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

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

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