@@ -90,9 +90,26 @@ export class ChartJsWebcomponent extends BaseCustomWebComponentConstructorAppend
9090 }
9191 }
9292
93+ #type: 'line' | 'bar' ;
94+ public get type ( ) {
95+ return this . #type;
96+ }
97+ public set type ( value ) {
98+ if ( this . #type != value ) {
99+ if ( this . #chart) {
100+ this . #chart. destroy ( ) ;
101+ this . #chart = null ;
102+ }
103+ this . #type = value ;
104+ if ( this . #ready) {
105+ this . #renderChart( ) ;
106+ }
107+ }
108+ }
109+
93110 #root: HTMLCanvasElement ;
94111 #ready: boolean ;
95- #chart: Chart < " line" , number [ ] , string | number > ;
112+ #chart: Chart < ' line' | 'bar' , number [ ] , string | number > ;
96113
97114 constructor ( ) {
98115 super ( ) ;
@@ -119,55 +136,64 @@ export class ChartJsWebcomponent extends BaseCustomWebComponentConstructorAppend
119136 values = this . #data. map ( row => ( < DataObject > row ) . value ) ;
120137 }
121138
122- if ( this . #chart)
123- this . #chart. destroy ( ) ;
139+ // if (this.#chart)
140+ // this.#chart.destroy();
124141
125142 if ( this . offsetWidth == 0 || this . offsetHeight == 0 )
126143 await requestAnimationFramePromise ( ) ;
127144
128- this . #chart = new Chart (
129- this . #root,
130- {
131- type : 'line' ,
132- data : {
133- labels : labels ,
134- datasets : [
135- {
136- data : values ,
137- fill : true ,
138- borderColor : this . #borderColor,
139- backgroundColor : this . #backgroundColor
140- }
141- ]
142- } ,
143- options : {
144- responsive : true ,
145- maintainAspectRatio : false ,
146- plugins : {
147- legend : {
148- display : false
149- } ,
150- tooltip : {
151- enabled : false
152- }
153- } ,
154- elements : {
155- point :{
156- radius : 0
157- }
145+ if ( this . #chart) {
146+ this . #chart. data . labels = labels ;
147+ this . #chart. data . datasets [ 0 ] . data = values ;
148+ this . #chart. data . datasets [ 0 ] . borderColor = this . #borderColor;
149+ this . #chart. data . datasets [ 0 ] . backgroundColor = this . #backgroundColor;
150+ this . #chart. options . scales . x . display = this . #enableXScale;
151+ this . #chart. options . scales . y . display = this . #enableYScale;
152+ } else {
153+ this . #chart = new Chart (
154+ this . #root,
155+ {
156+ type : this . #type,
157+ data : {
158+ labels : labels ,
159+ datasets : [
160+ {
161+ data : values ,
162+ fill : true ,
163+ borderColor : this . #borderColor,
164+ backgroundColor : this . #backgroundColor
165+ }
166+ ]
158167 } ,
159- scales : {
160- x : {
161- display : this . #enableXScale
168+ options : {
169+ responsive : true ,
170+ maintainAspectRatio : false ,
171+ plugins : {
172+ legend : {
173+ display : false
174+ } ,
175+ tooltip : {
176+ enabled : false
177+ }
162178 } ,
163- y : {
164- display : this . #enableYScale
165- }
166- } ,
167-
179+ elements : {
180+ point : {
181+ radius : 0
182+ }
183+ } ,
184+ scales : {
185+ x : {
186+ display : this . #enableXScale
187+ } ,
188+ y : {
189+ display : this . #enableYScale
190+ }
191+ } ,
192+
193+ }
168194 }
169- }
170- ) ;
195+ ) ;
196+ }
171197 }
172198}
173199
0 commit comments