Skip to content

Commit 115ad33

Browse files
committed
chart type and refresh
1 parent d010b0c commit 115ad33

File tree

2 files changed

+84
-45
lines changed

2 files changed

+84
-45
lines changed

custom-elements.json

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,16 @@
2222
"kind": "field",
2323
"name": "borderColor",
2424
"type": {
25-
"text": "string"
25+
"text": "string",
26+
"editor": "color"
2627
}
2728
},
2829
{
2930
"kind": "field",
3031
"name": "backgroundColor",
3132
"type": {
32-
"text": "string"
33+
"text": "string",
34+
"editor": "color"
3335
}
3436
},
3537
{
@@ -45,6 +47,13 @@
4547
"type": {
4648
"text": "boolean"
4749
}
50+
},
51+
{
52+
"kind": "field",
53+
"name": "type",
54+
"type": {
55+
"text": "'line'|'bar'"
56+
}
4857
}
4958
],
5059
"attributes": [
@@ -67,6 +76,10 @@
6776
{
6877
"name": "enable-y-scale",
6978
"fieldName": "enableYScale"
79+
},
80+
{
81+
"name": "type",
82+
"fieldName": "type"
7083
}
7184
],
7285
"superclass": {

src/chart/ChartJsWebcomponent.ts

Lines changed: 69 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)