better energy change detection

This commit is contained in:
Patrick Haßel 2025-11-21 10:52:38 +01:00
parent 2bd115f095
commit 9213c20116
5 changed files with 29 additions and 41 deletions

View File

@ -3,29 +3,19 @@
<app-location-power [location]="location"></app-location-power>
<app-location-energy [location]="location" [interval]="Interval.DAY" [offset]="offsetDay" unit="⌀W" [factor]="12 * 1000" [maxY]="850" [minY]="-850">
<ng-content #SeriesHistoryHeading>
<div style="display: flex; width: 100%">
&nbsp;
<div (click)="offsetDay += 1">&larr;</div>
&nbsp;
<div (click)="offsetDay = Math.max(0, offsetDay -1)">&rarr;</div>
&nbsp;
<div style="flex: 1">{{ offsetDayTitle() }}</div>
</div>
</ng-content>
<div style="display: flex; width: 100%; gap: 0.25em;">
<div (click)="offsetDayAdd(+1)">&larr;</div>
<div (click)="offsetDayAdd(-1)">&rarr;</div>
<div>{{ offsetDayTitle() }}</div>
</div>
</app-location-energy>
<app-location-energy [location]="location" [interval]="Interval.MONTH" [offset]="offsetMonth">
<ng-content #SeriesHistoryHeading>
<div style="display: flex; width: 100%">
&nbsp;
<div (click)="offsetMonth += 1">&larr;</div>
&nbsp;
<div (click)="offsetMonth = Math.max(0, offsetMonth -1)">&rarr;</div>
&nbsp;
<div style="flex: 1">{{ offsetMonthTitle() }}</div>
</div>
</ng-content>
<div style="display: flex; width: 100%; gap: 0.25em;">
<div (click)="offsetMonthAdd(+1)">&larr;</div>
<div (click)="offsetMonthAdd(-1)">&rarr;</div>
<div>{{ offsetMonthTitle() }}</div>
</div>
</app-location-energy>
@if (configService.locationConfig) {

View File

@ -92,9 +92,7 @@ export class LocationDetail implements OnInit, OnDestroy {
private readonly onLocationChange = (location: Location | null): void => {
this.location = location;
if (this.location) {
this.menuService.title = this.location.name;
}
this.menuService.title = this.location?.name || "";
};
ngOnDestroy(): void {
@ -122,4 +120,12 @@ export class LocationDetail implements OnInit, OnDestroy {
return this.datePipe.transform(d, 'yyyy MMMM') || '';
}
protected offsetDayAdd(delta: number) {
this.offsetDay = Math.max(0, this.offsetDay + delta);
}
protected offsetMonthAdd(delta: number) {
this.offsetMonth = Math.max(0, this.offsetMonth + delta);
}
}

View File

@ -1,4 +1,4 @@
import {AfterViewInit, Component, Inject, Input, LOCALE_ID, ViewChild} from '@angular/core';
import {Component, Inject, Input, LOCALE_ID, OnChanges, SimpleChanges, ViewChild} from '@angular/core';
import {Interval} from '../../../series/Interval';
import {PointService} from '../../../point/point-service';
import {Location} from '../../Location';
@ -22,7 +22,7 @@ const COLOR_BACK_CONSUME = "#ffc07a";
templateUrl: './energy-charts.html',
styleUrl: './energy-charts.less',
})
export class EnergyCharts implements AfterViewInit {
export class EnergyCharts implements OnChanges {
@ViewChild(BaseChartDirective)
chart?: BaseChartDirective;
@ -118,7 +118,8 @@ export class EnergyCharts implements AfterViewInit {
//
}
ngAfterViewInit(): void {
ngOnChanges(changes: SimpleChanges): void {
console.log("ngOnChanges", changes);
const series = [
this.location.energyPurchase,
this.location.energyDeliver,

View File

@ -2,7 +2,7 @@
<div class="SectionHeading">
<div class="SectionHeadingText">
{{ heading }}
<ng-content #SeriesHistoryHeading></ng-content>
<ng-content></ng-content>
</div>
</div>
<div class="SectionBody">
@ -86,6 +86,6 @@
</div>
<app-energy-charts [location]="location" [interval]="interval" [offset]="offset" [unit]="unit" [factor]="factor" [maxY]="maxY" [minY]="minY"></app-energy-charts>
<app-energy-charts #chart [location]="location" [interval]="interval" [offset]="offset" [unit]="unit" [factor]="factor" [maxY]="maxY" [minY]="minY"></app-energy-charts>
</div>

View File

@ -1,4 +1,4 @@
import {AfterViewInit, Component, Input, OnDestroy, OnInit} from '@angular/core';
import {Component, Input, OnChanges, OnDestroy, OnInit, SimpleChanges} from '@angular/core';
import {Location} from '../Location';
import {Series} from '../../series/Series';
import {Next} from '../../common';
@ -18,7 +18,7 @@ import {EnergyCharts} from './charts/energy-charts';
templateUrl: './location-energy.html',
styleUrl: './location-energy.less',
})
export class LocationEnergy implements OnInit, AfterViewInit, OnDestroy {
export class LocationEnergy implements OnInit, OnChanges, OnDestroy {
protected readonly Interval = Interval;
@ -49,17 +49,8 @@ export class LocationEnergy implements OnInit, AfterViewInit, OnDestroy {
@Input()
heading!: string;
private _o_: number = 0;
@Input()
set offset(value: number) {
this._o_ = value;
this.ngAfterViewInit();
}
get offset(): number {
return this._o_;
}
offset: number = 0;
@Input()
unit: string = "";
@ -94,7 +85,7 @@ export class LocationEnergy implements OnInit, AfterViewInit, OnDestroy {
this.subs.push(this.serieService.subscribe(this.update));
}
ngAfterViewInit(): void {
ngOnChanges(changes: SimpleChanges): void {
this.fetch(null, this.location?.energyPurchase, history => this.purchase = history);
this.fetch(null, this.location?.energyDeliver, history => this.deliver = history);
this.fetch(null, this.location?.energyProduce, history => this.produce = history);