1
1
import { Component , onMounted , onWillUnmount , useEffect , useRef } from "@odoo/owl" ;
2
2
import { Chart , ChartConfiguration } from "chart.js/auto" ;
3
3
import { ComponentsImportance } from "../../../../constants" ;
4
- import { deepCopy } from "../../../../helpers" ;
4
+ import { deepCopy , deepEquals } from "../../../../helpers" ;
5
+ import { Store , useStore } from "../../../../store_engine" ;
5
6
import { FigureUI , SpreadsheetChildEnv } from "../../../../types" ;
6
7
import { ChartJSRuntime } from "../../../../types/chart/chart" ;
7
8
import { css } from "../../../helpers" ;
8
9
import { chartJsExtensionRegistry } from "./chart_js_extension" ;
10
+ import { ChartAnimationStore } from "./chartjs_animation_store" ;
9
11
import {
10
12
funnelTooltipPositioner ,
11
13
getFunnelChartController ,
@@ -70,6 +72,7 @@ export class ChartJsComponent extends Component<Props, SpreadsheetChildEnv> {
70
72
private canvas = useRef ( "graphContainer" ) ;
71
73
private chart ?: Chart ;
72
74
private currentRuntime ! : ChartJSRuntime ;
75
+ private animationStore : Store < ChartAnimationStore > | undefined ;
73
76
74
77
private currentDevicePixelRatio = window . devicePixelRatio ;
75
78
@@ -90,6 +93,9 @@ export class ChartJsComponent extends Component<Props, SpreadsheetChildEnv> {
90
93
}
91
94
92
95
setup ( ) {
96
+ if ( this . env . model . getters . isDashboard ( ) ) {
97
+ this . animationStore = useStore ( ChartAnimationStore ) ;
98
+ }
93
99
onMounted ( ( ) => {
94
100
const runtime = this . chartRuntime ;
95
101
this . currentRuntime = runtime ;
@@ -115,12 +121,28 @@ export class ChartJsComponent extends Component<Props, SpreadsheetChildEnv> {
115
121
}
116
122
117
123
private createChart ( chartData : ChartConfiguration < any > ) {
124
+ if ( this . env . model . getters . isDashboard ( ) && this . animationStore ) {
125
+ const chartType = this . env . model . getters . getChart ( this . props . figureUI . id ) ?. type ;
126
+ if ( chartType && this . animationStore . animationPlayed [ this . props . figureUI . id ] !== chartType ) {
127
+ chartData = this . enableAnimationInChartData ( chartData ) ;
128
+ this . animationStore . disableAnimationForChart ( this . props . figureUI . id , chartType ) ;
129
+ }
130
+ }
131
+
118
132
const canvas = this . canvas . el as HTMLCanvasElement ;
119
133
const ctx = canvas . getContext ( "2d" ) ! ;
120
134
this . chart = new window . Chart ( ctx , chartData ) ;
121
135
}
122
136
123
137
private updateChartJs ( chartData : ChartConfiguration < any > ) {
138
+ if ( this . env . model . getters . isDashboard ( ) ) {
139
+ const chartType = this . env . model . getters . getChart ( this . props . figureUI . id ) ?. type ;
140
+ if ( chartType && this . hasChartDataChanged ( ) && this . animationStore ) {
141
+ chartData = this . enableAnimationInChartData ( chartData ) ;
142
+ this . animationStore . disableAnimationForChart ( this . props . figureUI . id , chartType ) ;
143
+ }
144
+ }
145
+
124
146
if ( chartData . data && chartData . data . datasets ) {
125
147
this . chart ! . data = chartData . data ;
126
148
if ( chartData . options ?. plugins ?. title ) {
@@ -132,4 +154,18 @@ export class ChartJsComponent extends Component<Props, SpreadsheetChildEnv> {
132
154
this . chart ! . config . options = chartData . options ;
133
155
this . chart ! . update ( ) ;
134
156
}
157
+
158
+ private hasChartDataChanged ( ) {
159
+ return ! deepEquals (
160
+ this . currentRuntime . chartJsConfig . data ,
161
+ this . chartRuntime . chartJsConfig . data
162
+ ) ;
163
+ }
164
+
165
+ private enableAnimationInChartData ( chartData : ChartConfiguration < any > ) {
166
+ return {
167
+ ...chartData ,
168
+ options : { ...chartData . options , animation : { animateRotate : true } } ,
169
+ } ;
170
+ }
135
171
}
0 commit comments