11import {
2+ AfterViewInit ,
23 Component ,
34 ElementRef ,
45 EventEmitter ,
56 HostBinding ,
67 Input ,
8+ NgZone ,
9+ OnChanges ,
710 OnDestroy ,
811 OnInit ,
912 Output ,
13+ Renderer2 ,
14+ SimpleChanges ,
1015 ViewChild
1116} from '@angular/core' ;
12- import { coerceBooleanProperty , coerceNumberProperty } from '@angular/cdk/coercion' ;
17+ import { BooleanInput , coerceBooleanProperty , coerceNumberProperty , NumberInput } from '@angular/cdk/coercion' ;
1318
14- import assign from 'lodash/assign' ;
15- import find from 'lodash/find' ;
16- import merge from 'lodash/merge' ;
19+ import { assign , find , merge } from 'lodash' ;
1720
18- import { Chart , ChartData , ChartOptions , ChartType , Plugin } from 'chart.js/auto' ;
21+ import Chart , { ChartData , ChartOptions , ChartType , Plugin } from 'chart.js/auto' ;
1922import * as chartjs from 'chart.js' ;
2023
2124import { customTooltips as cuiCustomTooltips } from '@coreui/chartjs' ;
2225import { IChartjs } from './chartjs.interface' ;
2326
27+ let nextId = 0 ;
28+
2429@Component ( {
2530 selector : 'c-chart' ,
2631 templateUrl : './chartjs.component.html' ,
2732 styleUrls : [ './chartjs.component.scss' ]
2833} )
29- export class ChartjsComponent implements IChartjs , OnDestroy , OnInit {
34+ // export class ChartjsComponent implements IChartjs, AfterViewInit, OnDestroy, OnInit {
35+ export class ChartjsComponent implements IChartjs , AfterViewInit , OnDestroy , OnInit , OnChanges {
36+
37+ static ngAcceptInputType_height : NumberInput ;
38+ static ngAcceptInputType_width : NumberInput ;
39+ static ngAcceptInputType_redraw : BooleanInput ;
40+
3041 @Input ( ) customTooltips = true ;
3142 @Input ( ) data : ChartData | ( ( canvas : HTMLCanvasElement ) => ChartData ) | undefined ;
3243
44+ @HostBinding ( 'style.height.px' )
3345 @Input ( )
3446 set height ( value : number ) {
3547 this . _height = coerceNumberProperty ( value ) ;
@@ -39,7 +51,7 @@ export class ChartjsComponent implements IChartjs, OnDestroy, OnInit {
3951 }
4052 private _height = 150 ;
4153
42- @Input ( ) id ?: string ;
54+ @Input ( ) id = `c-chartjs- ${ nextId ++ } ` ;
4355 @Input ( ) options ?: ChartOptions ;
4456 @Input ( ) plugins ?: Plugin [ ] ;
4557
@@ -61,8 +73,8 @@ export class ChartjsComponent implements IChartjs, OnDestroy, OnInit {
6173 get width ( ) {
6274 return this . _width ;
6375 }
64-
6576 private _width = 300 ;
77+
6678 @Input ( ) wrapper = true ;
6779
6880 @Output ( ) getDatasetAtEvent = new EventEmitter < any > ( ) ;
@@ -71,7 +83,7 @@ export class ChartjsComponent implements IChartjs, OnDestroy, OnInit {
7183
7284 @ViewChild ( 'canvasElement' ) canvasElement ! : ElementRef ;
7385
74- private chart : Chart | undefined ;
86+ chart : Chart | undefined ;
7587
7688 @HostBinding ( 'class' )
7789 get hostClasses ( ) {
@@ -89,10 +101,20 @@ export class ChartjsComponent implements IChartjs, OnDestroy, OnInit {
89101 : merge ( { } , this . data ) ;
90102 }
91103
92- constructor ( ) { }
104+ constructor (
105+ private elementRef : ElementRef ,
106+ private ngZone : NgZone ,
107+ private renderer : Renderer2
108+ ) { }
93109
94110 ngOnInit ( ) : void {
111+ // this.setupChart();
112+ // this.updateChart();
113+ }
114+
115+ ngAfterViewInit ( ) : void {
95116 this . setupChart ( ) ;
117+ this . updateChart ( ) ;
96118 }
97119
98120 ngOnDestroy ( ) : void {
@@ -102,20 +124,25 @@ export class ChartjsComponent implements IChartjs, OnDestroy, OnInit {
102124 setupChart ( ) {
103125 if ( ! this . canvasElement ) return ;
104126
105- const canvasRef = this . canvasElement . nativeElement ;
106-
107127 if ( this . customTooltips ) {
108128 chartjs . defaults . plugins . tooltip . enabled = false ;
109129 chartjs . defaults . plugins . tooltip . mode = 'index' ;
110130 chartjs . defaults . plugins . tooltip . position = 'nearest' ;
111131 chartjs . defaults . plugins . tooltip . external = cuiCustomTooltips ;
112132 }
113133
114- this . chart = new Chart ( canvasRef . value , {
115- type : this . type ,
116- data : this . computedData ,
117- options : this . options as ChartOptions ,
118- plugins : this . plugins
134+ const ctx : CanvasRenderingContext2D = this . canvasElement . nativeElement . getContext ( '2d' ) ;
135+
136+ return this . ngZone . runOutsideAngular ( ( ) => {
137+ this . chart = new Chart ( ctx , {
138+ type : this . type ,
139+ data : this . computedData ,
140+ options : this . options as ChartOptions ,
141+ plugins : this . plugins
142+ } ) ;
143+ setTimeout ( ( ) => {
144+ this . renderer . setStyle ( this . canvasElement . nativeElement , 'display' , 'block' ) ;
145+ } ) ;
119146 } ) ;
120147 }
121148
@@ -139,13 +166,20 @@ export class ChartjsComponent implements IChartjs, OnDestroy, OnInit {
139166 updateChart ( ) {
140167 if ( ! this . chart ) return ;
141168
169+ if ( this . redraw ) {
170+ this . destroyChart ( ) ;
171+ setTimeout ( ( ) => {
172+ this . setupChart ( ) ;
173+ } ) ;
174+ }
175+
142176 if ( this . options ) {
143177 this . chart . options = { ...this . options } ;
144178 }
145179
146180 if ( ! this . chart . config . data ) {
147181 this . chart . config . data = this . computedData ;
148- this . chart . update ( ) ;
182+ this . ngZone . runOutsideAngular ( ( ) => { this . chart ? .update ( ) ; } ) ;
149183 return ;
150184 }
151185
@@ -181,6 +215,14 @@ export class ChartjsComponent implements IChartjs, OnDestroy, OnInit {
181215 } ;
182216 } ) ;
183217
184- this . chart ?. update ( ) ;
218+ this . ngZone . runOutsideAngular ( ( ) => {
219+ setTimeout ( ( ) => {
220+ this . chart ?. update ( ) ;
221+ } ) ;
222+ } ) ;
223+ }
224+
225+ ngOnChanges ( changes : SimpleChanges ) : void {
226+ this . updateChart ( ) ;
185227 }
186228}
0 commit comments