fixed hierarchy chart render warning

This commit is contained in:
2026-11-12 21:17:36 +01:00
parent 5153e5876d
commit 0775f3efc7
4 changed files with 120 additions and 20 deletions
@@ -88,13 +88,13 @@
</div>
<mat-card-content class="uh-chart-viewport">
<div
echarts
[options]="level.options"
[autoResize]="true"
(chartClick)="onNodeClick($event, i)"
class="uh-chart"
[style.height.px]="level.height"></div>
@if (expanded()) {
<div
#chartHost
appPassiveWheel
class="uh-chart"
[style.height.px]="level.height"></div>
}
</mat-card-content>
</mat-card>
}
@@ -66,11 +66,14 @@
.uh-row {
display: flex;
flex-grow: 1;
flex-grow: 0;
flex-shrink: 0;
flex-basis: auto;
flex-direction: row;
flex-wrap: nowrap;
align-items: stretch;
gap: var(--uh-gap);
height: var(--uh-row-height);
overflow-x: auto;
overflow-y: hidden;
padding-bottom: 2px;
@@ -132,6 +135,7 @@
flex: 1 1 100%;
display: flex;
flex-direction: column;
min-height: 0;
overflow: hidden;
}
@@ -167,13 +171,13 @@
}
.uh-chart-viewport {
flex: 1 1 auto;
min-height: 0;
overflow-x: hidden;
overflow-y: auto;
height: 100%;
padding: 0 !important;
}
.uh-chart {
height: 100% !important;
width: 100%;
}
@@ -1,6 +1,7 @@
import {
Component,
DestroyRef,
ElementRef,
OnInit,
signal,
inject,
@@ -10,10 +11,10 @@ import {
EventEmitter,
effect,
untracked,
viewChild,
} from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { Router, ActivatedRoute } from '@angular/router';
import { NgxEchartsDirective } from 'ngx-echarts';
import { MatProgressSpinnerModule } from '@angular/material/progress-spinner';
import { MatToolbarModule } from '@angular/material/toolbar';
import { MatCardModule } from '@angular/material/card';
@@ -21,7 +22,8 @@ import { MatButtonModule } from '@angular/material/button';
import { MatIconModule } from '@angular/material/icon';
import { MatChipsModule } from '@angular/material/chips';
import { MatTooltipModule } from '@angular/material/tooltip';
import type { EChartsOption } from 'echarts';
import type { ECharts, EChartsOption } from 'echarts';
import { PassiveWheelDirective } from '../../../directives/passive-wheel.directive';
import { CredentialService } from '../../../shared/services/credential.service';
import { ChartThemeService } from '../../../shared/services/chart-theme.service';
import { HierarchyEditDialogComponent } from './hierarchy-edit-dialog/hierarchy-edit-dialog.component';
@@ -64,7 +66,7 @@ const MIN_TREE_HEIGHT = 200;
selector: 'app-hierarchy-chart',
standalone: true,
imports: [
NgxEchartsDirective,
PassiveWheelDirective,
MatProgressSpinnerModule,
MatToolbarModule,
MatCardModule,
@@ -92,10 +94,19 @@ export class HierarchyChartComponent implements OnInit {
private readonly credentialService = inject(CredentialService);
private readonly orgminerService = inject(OrgminerService);
private readonly chartTheme = inject(ChartThemeService);
/** Reference of the theme colors used for the current chart options. */
private lastChartTheme = this.chartTheme.colors();
readonly loading = signal<boolean>(true);
readonly lang = signal<'de' | 'en'>('de');
readonly levels = signal<TreeLevel[]>([]);
private readonly chartHost =
viewChild<ElementRef<HTMLDivElement>>('chartHost');
private chart: ECharts | null = null;
private chartHostElement: HTMLDivElement | null = null;
private chartResizeObserver: ResizeObserver | null = null;
private chartInitToken = 0;
private readonly snackBar = inject(MatSnackBar);
private readonly idMap = new Map<string, RawNode>();
universityRoot: RawNode | null = null;
@@ -109,6 +120,8 @@ export class HierarchyChartComponent implements OnInit {
private isOwnNavigation = false;
hierarchyData = input.required<HierarchyResponse>();
/** False while the parent expansion panel is collapsed. */
readonly expanded = input<boolean>(true);
pendingSuggestion = input<HierarchySuggestion | null>(null);
can_edit = computed(() => this.hierarchyData().can_edit ?? false);
can_suggest = computed(() => this.hierarchyData().can_suggest ?? false);
@@ -133,7 +146,10 @@ export class HierarchyChartComponent implements OnInit {
});
});
effect(() => {
this.chartTheme.colors();
const colors = this.chartTheme.colors();
if (colors === this.lastChartTheme) return;
this.lastChartTheme = colors;
untracked(() => {
if (!this.universityRoot) return;
this.levels.update((levels) =>
@@ -148,6 +164,15 @@ export class HierarchyChartComponent implements OnInit {
const suggestion = this.pendingSuggestion();
untracked(() => this.loadPendingSuggestionIntoEditor(suggestion));
});
effect(() => {
const levels = this.levels();
const expanded = this.expanded();
const host = this.chartHost()?.nativeElement ?? null;
const level = expanded ? (levels[levels.length - 1] ?? null) : null;
untracked(() => this.syncChart(host, level));
});
this.destroyRef.onDestroy(() => this.disposeChart());
}
ngOnInit(): void {
@@ -539,6 +564,70 @@ export class HierarchyChartComponent implements OnInit {
};
}
/* ------------------------------ chart instance ---------------------------- */
/**
* Owns the ECharts instance directly instead of delegating to ngx-echarts.
* This avoids two async `init()` calls racing when the options change while
* the chart is still being created (the "already initialized" warning).
*/
private syncChart(
host: HTMLDivElement | null,
level: TreeLevel | null,
): void {
if (!host || !level) {
this.disposeChart();
return;
}
if (this.chart && this.chartHostElement === host) {
this.chart.setOption(level.options, true);
return;
}
this.disposeChart();
this.chartHostElement = host;
const token = ++this.chartInitToken;
requestAnimationFrame(() => {
if (token !== this.chartInitToken || this.chartHostElement !== host) {
return;
}
void import('echarts').then(({ init }) => {
if (token !== this.chartInitToken || this.chartHostElement !== host) {
return;
}
const chart = init(host);
this.chart = chart;
chart.setOption(level.options, true);
chart.on('click', (params) => this.onChartNodeClick(params));
this.observeChartSize(host);
});
});
}
private onChartNodeClick(params: unknown): void {
const levelIndex = Math.max(0, this.levels().length - 1);
this.onNodeClick(params, levelIndex);
}
private observeChartSize(host: HTMLDivElement): void {
this.chartResizeObserver?.disconnect();
this.chartResizeObserver = new ResizeObserver(() => this.chart?.resize());
this.chartResizeObserver.observe(host);
}
private disposeChart(): void {
this.chartInitToken++;
this.chartResizeObserver?.disconnect();
this.chartResizeObserver = null;
this.chart?.dispose();
this.chart = null;
this.chartHostElement = null;
}
/* --------------------------------- saving -------------------------------- */
/**
@@ -551,7 +640,7 @@ export class HierarchyChartComponent implements OnInit {
this.orgminerService.saveHierarchy(children as any).subscribe({
next: (response) => {
this.snackBar.open('Hierarchy saved successfully', 'Close', {
this.snackBar.open('Hierarchie gespeichert.', 'OK', {
duration: 3000,
});
@@ -573,9 +662,13 @@ export class HierarchyChartComponent implements OnInit {
}
},
error: (error) => {
this.snackBar.open('Error saving hierarchy', 'Close', {
duration: 3000,
});
this.snackBar.open(
'Hierarchie konnte nicht gespeichert werden.',
'OK',
{
duration: 4000,
},
);
console.error('[HierarchyChart] error saving hierarchy:', error);
this.hierarchySaved.set(false);
},
@@ -25,9 +25,12 @@
</mat-expansion-panel-header>
<app-hierarchy-chart
[hierarchyData]="hierarchyData()"
[expanded]="hierarchyExpanded()"
[pendingSuggestion]="
suggestions().find((suggestion) => suggestion.status === 'new') ?? null
"
suggestions().find(
(suggestion) => suggestion.status === 'new'
) ?? null
"
(analyzeOrgEvent)="analyzeOrg($event)"
(suggestionSaved)="reloadSuggestions()" />
</mat-expansion-panel>