add topic treemap component and integrate into author comparison view
LocationBasedServices/smartguide/pipeline/head This commit looks good

This commit is contained in:
Steven Tischer
2026-07-23 14:03:21 +02:00
parent eb84d3014f
commit 97b7fe6a6f
7 changed files with 395 additions and 1 deletions
@@ -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<TopicNode>;
}
/**
* 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<SubfieldNode>;
}
/**
* 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<SubfieldShare>;
/**
* Field -> subfield -> topic hierarchy over all works, by primary topic,
* each level largest first. Drives the interactive topic treemap.
*/
field_tree: Array<FieldNode>;
}
@@ -93,6 +93,38 @@
<div echarts [options]="breadthChart()" class="profile-chart"></div>
</div>
</div>
<h3 class="chart-title">Themenlandschaft</h3>
<p class="chart-hint">Field → Subfield → Thema</p>
<div class="treemap-author-tabs" role="tablist" aria-label="Autor:in">
@for (
author of orderedProfiles();
track author.author_id;
let i = $index
) {
<button
type="button"
role="tab"
class="treemap-author-tab"
[class.active]="treemapProfile()?.author_id === author.author_id"
[attr.aria-selected]="
treemapProfile()?.author_id === author.author_id
"
(click)="selectTreemapAuthor(i)">
<span
class="legend-swatch"
[style.background]="authorColor(author.author_id)"></span>
{{ author.display_name }}
</button>
}
</div>
<app-topic-treemap
[nodes]="treemapNodes()"
seriesName="Themen"
colorMappingBy="id"
height="420px"
emptyText="Keine Themendaten für diese:n Autor:in."
[tooltipFormatter]="treemapTooltip"></app-topic-treemap>
} @else {
<p class="compare-hint">Keine Daten für das Themenprofil.</p>
}
@@ -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;
@@ -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<AuthorTopicProfile | null>(() => {
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<TreemapNode[]>(() => {
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}<br /><strong>${count}</strong> Werke`;
};
// turns row definitions into table rows, marking the best value per row
private buildRows(defs: RowDef[]): MetricRow[] {
const authors = this.summary();
@@ -0,0 +1,11 @@
@if (nodes.length) {
<div
echarts
[options]="options"
class="topic-treemap-chart"
[style.height]="height"></div>
} @else {
<p class="topic-treemap-empty">
{{ loading ? loadingText : emptyText }}
</p>
}
@@ -0,0 +1,11 @@
.topic-treemap-chart {
width: 100%;
}
.topic-treemap-empty {
display: grid;
min-height: 260px;
margin: 0;
place-items: center;
color: #64748b;
}
@@ -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}<br /><strong>${this.formatCount(
params.value,
)}</strong>`;
};
}
private formatCount(value: unknown): string {
return Number(value ?? 0).toLocaleString('de-DE');
}
}