I have a service which is an Observable that gets updated with new data.
In my custom Chart component I subscribe to that Observable like so:
constructor(private readonly poller: PollerService) {
this.poller.data$.subscribe(data => {
this.type = data.sensors[this.id].type;
this.addMeasurement(data.sensors[this.id].measurements);
});
}
private initChart(): void {
if (this.charts.length) {
return;
}
const measurement = this.measurements[this.measurements.length - 1];
const units = measurement.map(channel => channel.unit);
this.view = [this.width, this.height / units.length];
this.charts = units.map(unit => ([{
name: unit,
series: [],
}]));
}
private toChartData(): void {
this.initChart();
const newChartData: any = {};
for (const measurement of this.measurements) {
for (const channel of measurement) {
newChartData[channel.unit] ||= [];
(newChartData[channel.unit] as Series[]).push({
value: channel.value,
name: new DatePipe('it-IT').transform(new Date(channel.timestamp), 'HH:mm:ss.SSS')!,
});
}
}
for (const chart of this.charts) {
const unit = chart[0].name;
chart[0].series = newChartData[unit];
}
// array-destructuring reassignment doesn't work and ChangeDetectionRef.detectChanges() doesn't work
}
private addMeasurement(measurement: Measurement[]): void {
this.measurements.push(measurement);
if (this.measurements.length > this.poller.historyLength) {
this.measurements.shift();
}
this.toChartData();
}
Essentially, I have multiple charts (which data is valid) inside an array and then I render them using a for-block like so: https://pastebin.com/7ayhdpN3
The chart doesn't get updated, but the span correctly gets updated. How can I fix this bug?
Pastebin
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.