From 0775f3efc76374c7afea6bb9cb55c1921b957469 Mon Sep 17 00:00:00 2001 From: genja Date: Thu, 12 Nov 2026 21:17:36 +0100 Subject: [PATCH] fixed hierarchy chart render warning --- .../hierarchy-chart.component.html | 14 +-- .../hierarchy-chart.component.scss | 10 +- .../hierarchy-chart.component.ts | 109 ++++++++++++++++-- .../hierarchy/hierarchy.component.html | 7 +- 4 files changed, 120 insertions(+), 20 deletions(-) diff --git a/webapp/src/app/modules/hierarchy/hierarchy-chart/hierarchy-chart.component.html b/webapp/src/app/modules/hierarchy/hierarchy-chart/hierarchy-chart.component.html index cebd0d75..6489776a 100644 --- a/webapp/src/app/modules/hierarchy/hierarchy-chart/hierarchy-chart.component.html +++ b/webapp/src/app/modules/hierarchy/hierarchy-chart/hierarchy-chart.component.html @@ -88,13 +88,13 @@ -
+ @if (expanded()) { +
+ }
} diff --git a/webapp/src/app/modules/hierarchy/hierarchy-chart/hierarchy-chart.component.scss b/webapp/src/app/modules/hierarchy/hierarchy-chart/hierarchy-chart.component.scss index 5545b2b0..9d201698 100644 --- a/webapp/src/app/modules/hierarchy/hierarchy-chart/hierarchy-chart.component.scss +++ b/webapp/src/app/modules/hierarchy/hierarchy-chart/hierarchy-chart.component.scss @@ -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%; } diff --git a/webapp/src/app/modules/hierarchy/hierarchy-chart/hierarchy-chart.component.ts b/webapp/src/app/modules/hierarchy/hierarchy-chart/hierarchy-chart.component.ts index 5357b175..01ae7615 100644 --- a/webapp/src/app/modules/hierarchy/hierarchy-chart/hierarchy-chart.component.ts +++ b/webapp/src/app/modules/hierarchy/hierarchy-chart/hierarchy-chart.component.ts @@ -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(true); readonly lang = signal<'de' | 'en'>('de'); readonly levels = signal([]); + private readonly chartHost = + viewChild>('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(); universityRoot: RawNode | null = null; @@ -109,6 +120,8 @@ export class HierarchyChartComponent implements OnInit { private isOwnNavigation = false; hierarchyData = input.required(); + /** False while the parent expansion panel is collapsed. */ + readonly expanded = input(true); pendingSuggestion = input(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); }, diff --git a/webapp/src/app/modules/hierarchy/hierarchy.component.html b/webapp/src/app/modules/hierarchy/hierarchy.component.html index d184d1e0..39bc717c 100644 --- a/webapp/src/app/modules/hierarchy/hierarchy.component.html +++ b/webapp/src/app/modules/hierarchy/hierarchy.component.html @@ -25,9 +25,12 @@ suggestion.status === 'new' + ) ?? null + " (analyzeOrgEvent)="analyzeOrg($event)" (suggestionSaved)="reloadSuggestions()" />