web/elements: fix chart not rendering if update events happens before initial render
Signed-off-by: Jens Langhammer <jens.langhammer@beryju.org>
This commit is contained in:
parent
62250f4ec6
commit
f53343141e
|
@ -22,7 +22,7 @@ export abstract class AKChart<T> extends LitElement {
|
||||||
abstract apiRequest(): Promise<T>;
|
abstract apiRequest(): Promise<T>;
|
||||||
abstract getChartData(data: T): ChartData;
|
abstract getChartData(data: T): ChartData;
|
||||||
|
|
||||||
chart?: Chart;
|
chart: Chart;
|
||||||
|
|
||||||
@property()
|
@property()
|
||||||
centerText?: string;
|
centerText?: string;
|
||||||
|
@ -45,17 +45,22 @@ export abstract class AKChart<T> extends LitElement {
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
super();
|
super();
|
||||||
|
const canvas = this.shadowRoot?.querySelector<HTMLCanvasElement>("canvas");
|
||||||
|
if (!canvas) {
|
||||||
|
console.warn("Failed to get canvas element");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const ctx = canvas.getContext("2d");
|
||||||
|
if (!ctx) {
|
||||||
|
console.warn("failed to get 2d context");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
this.chart = this.configureChart(undefined, ctx);
|
||||||
window.addEventListener("resize", () => {
|
window.addEventListener("resize", () => {
|
||||||
if (this.chart) {
|
this.chart.resize();
|
||||||
this.chart.resize();
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
window.addEventListener(EVENT_REFRESH, () => {
|
window.addEventListener(EVENT_REFRESH, () => {
|
||||||
this.apiRequest().then((r: T) => {
|
this.firstUpdated();
|
||||||
if (!this.chart) return;
|
|
||||||
this.chart.data = this.getChartData(r);
|
|
||||||
this.chart.update();
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
const matcher = window.matchMedia("(prefers-color-scheme: light)");
|
const matcher = window.matchMedia("(prefers-color-scheme: light)");
|
||||||
const handler = (ev?: MediaQueryListEvent) => {
|
const handler = (ev?: MediaQueryListEvent) => {
|
||||||
|
@ -71,18 +76,9 @@ export abstract class AKChart<T> extends LitElement {
|
||||||
}
|
}
|
||||||
|
|
||||||
firstUpdated(): void {
|
firstUpdated(): void {
|
||||||
this.apiRequest().then((r) => {
|
this.apiRequest().then((r: T) => {
|
||||||
const canvas = this.shadowRoot?.querySelector<HTMLCanvasElement>("canvas");
|
this.chart.data = this.getChartData(r);
|
||||||
if (!canvas) {
|
this.chart.update();
|
||||||
console.warn("Failed to get canvas element");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
const ctx = canvas.getContext("2d");
|
|
||||||
if (!ctx) {
|
|
||||||
console.warn("failed to get 2d context");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
this.chart = this.configureChart(r, ctx);
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -155,10 +151,10 @@ export abstract class AKChart<T> extends LitElement {
|
||||||
} as ChartOptions;
|
} as ChartOptions;
|
||||||
}
|
}
|
||||||
|
|
||||||
configureChart(data: T, ctx: CanvasRenderingContext2D): Chart {
|
configureChart(data: T | undefined, ctx: CanvasRenderingContext2D): Chart {
|
||||||
const config = {
|
const config = {
|
||||||
type: this.getChartType(),
|
type: this.getChartType(),
|
||||||
data: this.getChartData(data),
|
data: data ? this.getChartData(data) : {},
|
||||||
options: this.getOptions(),
|
options: this.getOptions(),
|
||||||
plugins: this.getPlugins(),
|
plugins: this.getPlugins(),
|
||||||
};
|
};
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
Reference in New Issue