Skip to content

Commit 659bca5

Browse files
committed
more properties
1 parent 208b144 commit 659bca5

File tree

2 files changed

+74
-4
lines changed

2 files changed

+74
-4
lines changed

custom-elements.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,20 @@
3131
"type": {
3232
"text": "string"
3333
}
34+
},
35+
{
36+
"kind": "field",
37+
"name": "enableXScale",
38+
"type": {
39+
"text": "boolean"
40+
}
41+
},
42+
{
43+
"kind": "field",
44+
"name": "enableYScale",
45+
"type": {
46+
"text": "boolean"
47+
}
3448
}
3549
],
3650
"attributes": [
@@ -45,6 +59,14 @@
4559
{
4660
"name": "background-color",
4761
"fieldName": "backgroundColor"
62+
},
63+
{
64+
"name": "enable-x-scale",
65+
"fieldName": "enableXScale"
66+
},
67+
{
68+
"name": "enable-y-scale",
69+
"fieldName": "enableYScale"
4870
}
4971
],
5072
"superclass": {

src/chart/ChartJsWebcomponent.ts

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
import { BaseCustomWebComponentConstructorAppend, css, html } from "@node-projects/base-custom-webcomponent";
22
import Chart from 'chart.js/auto/auto.js';
3+
//import Chart from 'chart.js/auto';
34

45
type DataObject = { value: number, label: number | string };
56
type DataArray = [value: number, label: number | string];
67
type Data = DataObject | DataArray;
78

9+
function requestAnimationFramePromise() {
10+
return new Promise(resolve => requestAnimationFrame(resolve));
11+
}
812
export 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

Comments
 (0)