From 97b7fe6a6fd0e06c4dd849ec11a5fa58136a6899 Mon Sep 17 00:00:00 2001 From: Steven Tischer Date: Thu, 23 Jul 2026 14:03:21 +0200 Subject: [PATCH] add topic treemap component and integrate into author comparison view --- .../src/app/api/model/authorTopicProfile.ts | 67 +++++++ .../compare-authors.component.html | 32 ++++ .../compare-authors.component.scss | 36 ++++ .../compare-authors.component.ts | 62 +++++- .../topic-treemap.component.html | 11 ++ .../topic-treemap.component.scss | 11 ++ .../topic-treemap/topic-treemap.component.ts | 177 ++++++++++++++++++ 7 files changed, 395 insertions(+), 1 deletion(-) create mode 100644 webapp/src/app/shared/ui/topic-treemap/topic-treemap.component.html create mode 100644 webapp/src/app/shared/ui/topic-treemap/topic-treemap.component.scss create mode 100644 webapp/src/app/shared/ui/topic-treemap/topic-treemap.component.ts diff --git a/webapp/src/app/api/model/authorTopicProfile.ts b/webapp/src/app/api/model/authorTopicProfile.ts index 00d5593a..79dd0621 100644 --- a/webapp/src/app/api/model/authorTopicProfile.ts +++ b/webapp/src/app/api/model/authorTopicProfile.ts @@ -22,6 +22,68 @@ export interface SubfieldShare { count: number; } +/** + * A primary topic within a subfield. + */ +export interface TopicNode { + /** + * The OpenAlex ID of the primary topic. + */ + id: string; + /** + * The primary-topic display name. + */ + name: string; + /** + * Number of works whose primary topic is this topic. + */ + count: number; +} + +/** + * A subfield within a field, with its primary topics. + */ +export interface SubfieldNode { + /** + * The OpenAlex ID of the subfield. + */ + id: string; + /** + * The subfield display name. + */ + name: string; + /** + * Number of works whose primary topic belongs to this subfield. + */ + count: number; + /** + * Primary topics within this subfield, largest first. + */ + topics: Array; +} + +/** + * A field with its subfields, top of the primary-topic hierarchy. + */ +export interface FieldNode { + /** + * The OpenAlex ID of the field. + */ + id: string; + /** + * The field display name. + */ + name: string; + /** + * Number of works whose primary topic belongs to this field. + */ + count: number; + /** + * Subfields within this field, largest first. + */ + subfields: Array; +} + /** * Section 3: how an author\'s complete body of work distributes over topics. All counts are based on each work\'s primary topic. */ @@ -62,4 +124,9 @@ export interface AuthorTopicProfile { * Top subfields over all works, by primary topic, largest first. */ subfields: Array; + /** + * Field -> subfield -> topic hierarchy over all works, by primary topic, + * each level largest first. Drives the interactive topic treemap. + */ + field_tree: Array; } diff --git a/webapp/src/app/modules/compare-authors/compare-authors.component.html b/webapp/src/app/modules/compare-authors/compare-authors.component.html index eac43fd0..6dad5c9d 100644 --- a/webapp/src/app/modules/compare-authors/compare-authors.component.html +++ b/webapp/src/app/modules/compare-authors/compare-authors.component.html @@ -93,6 +93,38 @@
+ +

Themenlandschaft

+

Field → Subfield → Thema

+
+ @for ( + author of orderedProfiles(); + track author.author_id; + let i = $index + ) { + + } +
+ } @else {

Keine Daten für das Themenprofil.

} diff --git a/webapp/src/app/modules/compare-authors/compare-authors.component.scss b/webapp/src/app/modules/compare-authors/compare-authors.component.scss index f05c3335..6e1ac55d 100644 --- a/webapp/src/app/modules/compare-authors/compare-authors.component.scss +++ b/webapp/src/app/modules/compare-authors/compare-authors.component.scss @@ -262,6 +262,42 @@ height: 320px; } +.treemap-author-tabs { + display: flex; + flex-wrap: wrap; + gap: 6px; + margin: 4px 0 10px; +} + +.treemap-author-tab { + display: inline-flex; + align-items: center; + gap: 6px; + padding: 5px 12px; + border: 1px solid var(--sg-border, #d7deee); + border-radius: 999px; + background: var(--sg-surface, #fff); + color: var(--sg-text-muted, #64708f); + font-size: 12px; + font-weight: 600; + cursor: pointer; + transition: + background 0.15s ease, + border-color 0.15s ease, + color 0.15s ease; + + &:hover { + border-color: var(--sg-primary, #2563eb); + color: var(--sg-text, #101a3d); + } + + &.active { + border-color: var(--sg-primary, #2563eb); + background: color-mix(in srgb, var(--sg-primary, #2563eb) 10%, #fff); + color: var(--sg-text, #101a3d); + } +} + .chart-grid { display: grid; grid-template-columns: 1fr 1fr; diff --git a/webapp/src/app/modules/compare-authors/compare-authors.component.ts b/webapp/src/app/modules/compare-authors/compare-authors.component.ts index 299c1f47..24729c8b 100644 --- a/webapp/src/app/modules/compare-authors/compare-authors.component.ts +++ b/webapp/src/app/modules/compare-authors/compare-authors.component.ts @@ -12,6 +12,11 @@ import { RankingService, } from '../../api'; +import { + TopicTreemapComponent, + TreemapNode, +} from '../../shared/ui/topic-treemap/topic-treemap.component'; + const AUTHOR_COLORS = ['#1565c0', '#e6550d', '#2ca02c', '#9467bd']; const SUBFIELD_COLORS = [ '#2563eb', @@ -108,7 +113,13 @@ interface SegmentDef { @Component({ selector: 'app-compare-authors', standalone: true, - imports: [DecimalPipe, NgTemplateOutlet, MatIconModule, NgxEchartsDirective], + imports: [ + DecimalPipe, + NgTemplateOutlet, + MatIconModule, + NgxEchartsDirective, + TopicTreemapComponent, + ], templateUrl: './compare-authors.component.html', styleUrl: './compare-authors.component.scss', }) @@ -569,6 +580,55 @@ export class CompareAuthorsComponent implements OnInit { } as EChartsOption; }); + readonly selectedTreemapAuthor = signal(0); + + readonly treemapProfile = computed(() => { + const profiles = this.orderedProfiles(); + if (profiles.length === 0) return null; + const index = Math.min(this.selectedTreemapAuthor(), profiles.length - 1); + return profiles[index]; + }); + + selectTreemapAuthor(index: number): void { + this.selectedTreemapAuthor.set(index); + } + + authorColor(authorId: string): string { + return ( + this.summary().find((a) => a.authorId === authorId)?.color ?? OTHER_COLOR + ); + } + + readonly treemapNodes = computed(() => { + const profile = this.treemapProfile(); + if (!profile) return []; + + return profile.field_tree.map((field) => ({ + id: field.id, + name: field.name, + value: field.count, + children: field.subfields.map((subfield) => ({ + id: subfield.id, + name: subfield.name, + value: subfield.count, + children: subfield.topics.map((topic) => ({ + id: topic.id, + name: topic.name, + value: topic.count, + })), + })), + })); + }); + + readonly treemapTooltip = (params: any): string => { + const path = (params.treePathInfo ?? []) + .slice(1) + .map((p: any) => p.name) + .join(' › '); + const count = Number(params.value ?? 0).toLocaleString('de-DE'); + return `${path || params.name}
${count} Werke`; + }; + // turns row definitions into table rows, marking the best value per row private buildRows(defs: RowDef[]): MetricRow[] { const authors = this.summary(); diff --git a/webapp/src/app/shared/ui/topic-treemap/topic-treemap.component.html b/webapp/src/app/shared/ui/topic-treemap/topic-treemap.component.html new file mode 100644 index 00000000..7d06aaac --- /dev/null +++ b/webapp/src/app/shared/ui/topic-treemap/topic-treemap.component.html @@ -0,0 +1,11 @@ +@if (nodes.length) { +
+} @else { +

+ {{ loading ? loadingText : emptyText }} +

+} diff --git a/webapp/src/app/shared/ui/topic-treemap/topic-treemap.component.scss b/webapp/src/app/shared/ui/topic-treemap/topic-treemap.component.scss new file mode 100644 index 00000000..90374d18 --- /dev/null +++ b/webapp/src/app/shared/ui/topic-treemap/topic-treemap.component.scss @@ -0,0 +1,11 @@ +.topic-treemap-chart { + width: 100%; +} + +.topic-treemap-empty { + display: grid; + min-height: 260px; + margin: 0; + place-items: center; + color: #64748b; +} diff --git a/webapp/src/app/shared/ui/topic-treemap/topic-treemap.component.ts b/webapp/src/app/shared/ui/topic-treemap/topic-treemap.component.ts new file mode 100644 index 00000000..c9fcc7a7 --- /dev/null +++ b/webapp/src/app/shared/ui/topic-treemap/topic-treemap.component.ts @@ -0,0 +1,177 @@ +import { Component, Input, OnChanges } from '@angular/core'; +import { EChartsOption } from 'echarts'; +import { NgxEchartsDirective } from 'ngx-echarts'; + +/** One node of the treemap hierarchy. `children` nests the next level. */ +export interface TreemapNode { + name: string; + value: number; + /** Optional stable id used when colouring by id. */ + id?: string; + children?: TreemapNode[]; + /** Extra payload made available to a custom tooltip formatter. */ + [key: string]: unknown; +} + +const DEFAULT_COLORS = [ + '#2563eb', + '#059669', + '#d97706', + '#dc2626', + '#7c3aed', + '#0284c7', + '#ea580c', + '#4f46e5', + '#0f766e', + '#be123c', +]; + +@Component({ + selector: 'app-topic-treemap', + imports: [NgxEchartsDirective], + templateUrl: './topic-treemap.component.html', + styleUrl: './topic-treemap.component.scss', +}) +export class TopicTreemapComponent implements OnChanges { + @Input() nodes: TreemapNode[] = []; + @Input() loading = false; + @Input() seriesName = ''; + @Input() emptyText = 'Keine Daten vorhanden.'; + @Input() loadingText = 'Wird geladen ...'; + /** CSS height of the chart area. */ + @Input() height = '560px'; + /** Colour buckets by node id or by node name. */ + @Input() colorMappingBy: 'id' | 'name' = 'name'; + /** Override the default tooltip; receives the raw ECharts params. */ + @Input() tooltipFormatter?: (params: any) => string; + + options: EChartsOption = {}; + private readonly colors = DEFAULT_COLORS; + + ngOnChanges(): void { + this.options = this.buildOptions(); + } + + private buildOptions(): EChartsOption { + const options = { + tooltip: { + formatter: this.tooltipFormatter ?? this.defaultTooltip(), + }, + series: [ + { + name: this.seriesName, + type: 'treemap', + data: this.nodes, + roam: false, + nodeClick: 'zoomToNode', + sort: 'desc', + drillDownIcon: '', + leafDepth: 1, + color: this.colors, + colorMappingBy: this.colorMappingBy, + breadcrumb: { + show: true, + bottom: 0, + height: 24, + left: 8, + right: 8, + emptyItemWidth: 80, + itemStyle: { + color: '#f8fafc', + borderColor: '#cbd5e1', + borderWidth: 1, + textStyle: { color: '#334155', fontWeight: 600 }, + }, + emphasis: { + itemStyle: { + color: '#e0f2fe', + borderColor: '#93c5fd', + textStyle: { color: '#0f172a', fontWeight: 700 }, + }, + }, + }, + label: { + show: true, + formatter: (params: any) => + `${params.name}\n${this.formatCount(params.value)}`, + color: '#ffffff', + fontWeight: 600, + lineHeight: 18, + position: 'inside', + align: 'center', + verticalAlign: 'middle', + overflow: 'truncate', + }, + upperLabel: { + show: true, + formatter: (params: any) => + `${params.name} · ${this.formatCount(params.value)}`, + height: 30, + color: '#ffffff', + fontWeight: 700, + overflow: 'truncate', + }, + levels: [ + { + color: this.colors, + upperLabel: { show: false }, + itemStyle: { + borderColor: '#ffffff', + borderWidth: 3, + gapWidth: 3, + }, + }, + { + colorSaturation: [0.35, 0.7], + upperLabel: { show: false }, + itemStyle: { + borderColor: '#ffffff', + borderWidth: 2, + gapWidth: 2, + }, + }, + { + colorSaturation: [0.45, 0.78], + upperLabel: { show: false }, + itemStyle: { + borderColor: '#ffffff', + borderWidth: 2, + gapWidth: 2, + }, + }, + ], + top: 8, + right: 8, + bottom: 32, + left: 8, + animationDurationUpdate: 450, + emphasis: { focus: 'self', label: { color: '#ffffff' } }, + itemStyle: { + borderRadius: 4, + borderColor: '#ffffff', + borderWidth: 3, + gapWidth: 3, + }, + }, + ], + }; + + return options as EChartsOption; + } + + private defaultTooltip(): (params: any) => string { + return (params: any) => { + const path = (params.treePathInfo ?? []) + .slice(1) + .map((p: any) => p.name) + .join(' › '); + return `${path || params.name}
${this.formatCount( + params.value, + )}`; + }; + } + + private formatCount(value: unknown): string { + return Number(value ?? 0).toLocaleString('de-DE'); + } +}