11import { BaseCustomWebComponentConstructorAppend , css , html } from "@node-projects/base-custom-webcomponent" ;
22import Chart from 'chart.js/auto/auto.js' ;
3+ //import Chart from 'chart.js/auto';
34
45type DataObject = { value : number , label : number | string } ;
56type DataArray = [ value : number , label : number | string ] ;
67type Data = DataObject | DataArray ;
78
9+ function requestAnimationFramePromise ( ) {
10+ return new Promise ( resolve => requestAnimationFrame ( resolve ) ) ;
11+ }
812export class ChartJsWebcomponent extends BaseCustomWebComponentConstructorAppend {
913
1014 public static override readonly style = css `
@@ -26,7 +30,9 @@ export class ChartJsWebcomponent extends BaseCustomWebComponentConstructorAppend
2630 public static properties = {
2731 data : Object ,
2832 borderColor : String ,
29- backgroundColor : String
33+ backgroundColor : String ,
34+ enableXScale : Boolean ,
35+ enableYScale : Boolean
3036 }
3137
3238 #data: Data [ ]
@@ -62,6 +68,28 @@ export class ChartJsWebcomponent extends BaseCustomWebComponentConstructorAppend
6268 }
6369 }
6470
71+ #enableXScale: boolean
72+ public get enableXScale ( ) {
73+ return this . #enableXScale;
74+ }
75+ public set enableXScale ( value ) {
76+ this . #enableXScale = value ;
77+ if ( this . #ready) {
78+ this . #renderChart( ) ;
79+ }
80+ }
81+
82+ #enableYScale: boolean
83+ public get enableYScale ( ) {
84+ return this . #enableYScale;
85+ }
86+ public set enableYScale ( value ) {
87+ this . #enableYScale = value ;
88+ if ( this . #ready) {
89+ this . #renderChart( ) ;
90+ }
91+ }
92+
6593 #root: HTMLCanvasElement ;
6694 #ready: boolean ;
6795 #chart: Chart < "line" , number [ ] , string | number > ;
@@ -75,12 +103,12 @@ export class ChartJsWebcomponent extends BaseCustomWebComponentConstructorAppend
75103
76104 public ready ( ) {
77105 this . _parseAttributesToProperties ( true ) ;
78-
106+
79107 this . #renderChart( ) ;
80108 this . #ready = true ;
81109 }
82110
83- #renderChart( ) {
111+ async #renderChart( ) {
84112 let labels : ( string | number ) [ ] ;
85113 let values : number [ ] ;
86114 if ( Array . isArray ( this . #data[ 0 ] ) ) {
@@ -94,6 +122,9 @@ export class ChartJsWebcomponent extends BaseCustomWebComponentConstructorAppend
94122 if ( this . #chart)
95123 this . #chart. destroy ( ) ;
96124
125+ if ( this . offsetWidth == 0 || this . offsetHeight == 0 )
126+ await requestAnimationFramePromise ( ) ;
127+
97128 this . #chart = new Chart (
98129 this . #root,
99130 {
@@ -115,8 +146,25 @@ export class ChartJsWebcomponent extends BaseCustomWebComponentConstructorAppend
115146 plugins : {
116147 legend : {
117148 display : false
149+ } ,
150+ tooltip : {
151+ enabled : false
152+ }
153+ } ,
154+ elements : {
155+ point :{
156+ radius : 0
157+ }
158+ } ,
159+ scales : {
160+ x : {
161+ display : this . #enableXScale
162+ } ,
163+ y : {
164+ display : this . #enableYScale
118165 }
119- }
166+ } ,
167+
120168 }
121169 }
122170 ) ;
0 commit comments