refactor/wordcloud #270

Merged
kallweit merged 2 commits from refactor/wordcloud into dev 2026-08-10 18:44:07 +02:00
6 changed files with 268 additions and 193 deletions
@@ -0,0 +1,28 @@
@if (loading) {
<div class="word-cloud-state">Forschungsthemen werden geladen ...</div>
} @else if (loadFailed) {
<div class="word-cloud-state">
Forschungsthemen konnten nicht geladen werden.
</div>
} @else if (words.length) {
<div class="word-cloud-chart">
@for (word of words; track word.name) {
<button
type="button"
class="word-cloud-word"
[style.color]="word.color"
[style.fontSize.px]="word.fontSize"
[style.fontWeight]="word.fontWeight"
[disabled]="!word.topicId"
[appComponentTooltip]="AppTopicTooltipComponent"
[tooltipInputs]="{ topicId: word.rawTopicId }"
[tooltipDisabled]="!word.rawTopicId"
[tooltipMaxWidth]="'500px'"
(click)="selectWord(word)">
{{ word.name }}
</button>
}
</div>
} @else {
<p class="word-cloud-state">Keine Forschungsthemen gefunden.</p>
}
@@ -0,0 +1,50 @@
:host {
display: block;
min-height: 420px;
}
.word-cloud-chart {
width: 100%;
min-height: 420px;
display: flex;
flex-wrap: wrap;
align-content: center;
align-items: baseline;
justify-content: center;
gap: 0.35em 1em;
padding: 1.5rem 1rem;
box-sizing: border-box;
}
.word-cloud-word {
appearance: none;
border: none;
background: none;
padding: 0;
margin: 0;
font-family: 'Open Sans', Arial, sans-serif;
line-height: 1.1;
cursor: pointer;
transition: transform 0.15s ease;
&:hover:not(:disabled),
&:focus-visible {
transform: scale(1.08);
outline: none;
}
&:disabled {
cursor: default;
}
}
.word-cloud-state {
margin: 0;
min-height: 420px;
display: flex;
align-items: center;
justify-content: center;
color: var(--sg-text-muted);
font-size: 14px;
text-align: center;
}
@@ -0,0 +1,164 @@
import { Component, EventEmitter, Input, Output } from '@angular/core';
import { ComponentTooltipDirective } from '../../../../directives/component-tooltip.directive';
import { AppTopicTooltipComponent } from '../../../../shared/ui/app-topic-tooltip/app-topic-tooltip.component';
import { topicHierarchyColor } from '../../../../resolvers/utils/topic-colors';
import { TopicLegendTopic } from '../../../../resolvers/utils/topic-legend';
export interface WordCloudTopicOccurrence {
name: string;
topicId: string;
year: number | undefined;
fieldId: string | undefined;
fieldName: string | undefined;
domainId: string | undefined;
domainName: string | undefined;
subfieldId: string | undefined;
subfieldName: string | undefined;
}
export interface WordCloudWord {
name: string;
topicId: string;
rawTopicId: string;
color: string;
fontSize: number;
fontWeight: number;
}
export interface ResearchWordcloud {
words: WordCloudWord[];
legendTopics: TopicLegendTopic[];
}
const displayedWordLimit = 30;
const minimumFontSize = 12;
const maximumFontSize = 32;
export function buildResearchWordcloud(
occurrences: WordCloudTopicOccurrence[],
startYear: number | undefined,
endYear: number | undefined,
): ResearchWordcloud {
const counts = new Map<string, number>();
const legendTopicsByName = new Map<string, TopicLegendTopic>();
for (const occurrence of occurrences) {
if (!isWithinYearRange(occurrence.year, startYear, endYear)) {
continue;
}
counts.set(occurrence.name, (counts.get(occurrence.name) ?? 0) + 1);
if (!legendTopicsByName.has(occurrence.name)) {
legendTopicsByName.set(occurrence.name, {
id: occurrence.topicId,
name: occurrence.name,
field_id: occurrence.fieldId,
field_name: occurrence.fieldName,
domain_id: occurrence.domainId,
domain_name: occurrence.domainName,
subfield_id: occurrence.subfieldId,
subfield_name: occurrence.subfieldName,
});
}
}
if (counts.size === 0) {
return { words: [], legendTopics: [] };
}
const values = Array.from(counts.values());
const minimumValue = Math.min(...values);
const maximumValue = Math.max(...values);
const intensity = (value: number): number =>
maximumValue === minimumValue
? 1
: (value - minimumValue) / (maximumValue - minimumValue);
const displayed = Array.from(counts.entries())
.sort((left, right) => right[1] - left[1])
.slice(0, displayedWordLimit);
const legendTopics = displayed
.map(([name]) => legendTopicsByName.get(name))
.filter((topic): topic is TopicLegendTopic => Boolean(topic));
const words = displayed.map(([name, value]): WordCloudWord => {
const legendTopic = legendTopicsByName.get(name);
const scale = intensity(value);
return {
name,
topicId: legendTopic?.id ?? '',
rawTopicId: stripOpenAlexId(legendTopic?.id),
color: topicHierarchyColor(legendTopic),
fontSize: Math.round(
minimumFontSize + scale * (maximumFontSize - minimumFontSize),
),
fontWeight: scale > 0.66 ? 800 : scale > 0.33 ? 600 : 400,
};
});
return { words: centreWords(words), legendTopics };
}
function isWithinYearRange(
year: number | undefined,
startYear: number | undefined,
endYear: number | undefined,
): boolean {
if (startYear !== undefined && (year === undefined || year < startYear)) {
return false;
}
if (endYear !== undefined && (year === undefined || year > endYear)) {
return false;
}
return true;
}
function centreWords(words: WordCloudWord[]): WordCloudWord[] {
const centred: WordCloudWord[] = [];
words.forEach((word, index) => {
if (index % 2 === 0) {
centred.push(word);
return;
}
centred.unshift(word);
});
return centred;
}
function stripOpenAlexId(id: string | null | undefined): string {
return (id ?? '').replace('https://openalex.org/', '');
}
@Component({
selector: 'app-research-wordcloud',
standalone: true,
imports: [ComponentTooltipDirective],
templateUrl: './research-wordcloud.component.html',
styleUrl: './research-wordcloud.component.scss',
})
export class ResearchWordcloudComponent {
readonly AppTopicTooltipComponent = AppTopicTooltipComponent;
@Input() words: WordCloudWord[] = [];
@Input() loading = false;
@Input() loadFailed = false;
@Output() wordSelect = new EventEmitter<WordCloudWord>();
selectWord(word: WordCloudWord): void {
if (!word.topicId) {
return;
}
this.wordSelect.emit(word);
}
}
@@ -19,30 +19,11 @@
(topicSelect)="onTimelineTopicSelect($event)"
(yearSelect)="onTimelineYearSelect($event)"
(loadEarlierYears)="onTimelineLoadEarlierYears()">
<div timeline-wordcloud-content class="word-cloud-view">
@if (loadingWordCloud) {
<div class="word-cloud-state">Forschungsthemen werden geladen ...</div>
} @else if (wordCloudWords.length) {
<div class="word-cloud-chart">
@for (word of wordCloudWords; track word.name) {
<button
type="button"
class="word-cloud-word"
[style.color]="word.color"
[style.fontSize.px]="word.fontSize"
[style.fontWeight]="word.fontWeight"
[disabled]="!word.topicId"
[matTooltip]="word.tooltip"
[matTooltipDisabled]="!word.tooltip"
matTooltipClass="topic-chip-tooltip"
(click)="onWordClick(word)">
{{ word.name }}
</button>
}
</div>
} @else if (wordCloudEmpty) {
<p class="word-cloud-empty">Keine Forschungsthemen gefunden.</p>
}
</div>
<app-research-wordcloud
timeline-wordcloud-content
[words]="wordCloudWords"
[loading]="loadingWordCloud"
[loadFailed]="wordCloudLoadFailed"
(wordSelect)="onWordClick($event)" />
</app-research-timeline>
</section>
@@ -3,54 +3,3 @@
display: block;
box-sizing: border-box;
}
.word-cloud-view {
min-height: 420px;
}
.word-cloud-chart {
width: 100%;
min-height: 420px;
display: flex;
flex-wrap: wrap;
align-content: center;
align-items: baseline;
justify-content: center;
gap: 0.35em 1em;
padding: 1.5rem 1rem;
box-sizing: border-box;
}
.word-cloud-word {
appearance: none;
border: none;
background: none;
padding: 0;
margin: 0;
font-family: 'Open Sans', Arial, sans-serif;
line-height: 1.1;
cursor: pointer;
transition: transform 0.15s ease;
&:hover:not(:disabled),
&:focus-visible {
transform: scale(1.08);
outline: none;
}
&:disabled {
cursor: default;
}
}
.word-cloud-state,
.word-cloud-empty {
margin: 0;
min-height: 420px;
display: flex;
align-items: center;
justify-content: center;
color: var(--sg-text-muted);
font-size: 14px;
text-align: center;
}
@@ -1,8 +1,6 @@
import { CommonModule } from '@angular/common';
import { Component, DestroyRef, OnInit, inject } from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { ActivatedRoute, ParamMap, Router } from '@angular/router';
import { MatTooltipModule } from '@angular/material/tooltip';
import { combineLatest, distinctUntilChanged, map } from 'rxjs';
import { TopicCount, TopicsByYear, WorksService } from '../../api';
@@ -14,27 +12,19 @@ import {
TopicTimelineElement,
TopicTimelineTopic,
} from './components/research-timeline/research-timeline.component';
import {
buildResearchWordcloud,
ResearchWordcloudComponent,
WordCloudTopicOccurrence,
WordCloudWord,
} from './components/research-wordcloud/research-wordcloud.component';
import { workQueryParamKeys } from '../../resolvers/work-page.resolver';
import { TopicLegendTopic } from '../../resolvers/utils/topic-legend';
import { topicHierarchyColor } from '../../resolvers/utils/topic-colors';
import { buildTopicHierarchyTooltip } from '../../resolvers/utils/topic-tooltip';
export interface WordCloudWord {
name: string;
value: number;
topicId: string | undefined;
fieldName: string | undefined;
domainName: string | undefined;
color: string;
fontSize: number;
fontWeight: number;
tooltip: string;
}
@Component({
selector: 'app-person-overview',
standalone: true,
imports: [CommonModule, ResearchTimelineComponent, MatTooltipModule],
imports: [ResearchTimelineComponent, ResearchWordcloudComponent],
templateUrl: './person-overview.component.html',
styleUrl: './person-overview.component.scss',
})
@@ -73,19 +63,9 @@ export class PersonOverviewComponent implements OnInit {
wordCloudWords: WordCloudWord[] = [];
wordCloudLegendTopics: TopicLegendTopic[] = [];
loadingWordCloud = false;
wordCloudEmpty = false;
wordCloudLoadFailed = false;
private rawTopicOccurrences: Array<{
name: string;
topicId: string;
year: number | undefined;
fieldId: string | undefined;
fieldName: string | undefined;
domainId: string | undefined;
domainName: string | undefined;
subfieldId: string | undefined;
subfieldName: string | undefined;
}> = [];
private rawTopicOccurrences: WordCloudTopicOccurrence[] = [];
ngOnInit(): void {
combineLatest(this.route.pathFromRoot.map((route) => route.paramMap))
@@ -351,14 +331,14 @@ export class PersonOverviewComponent implements OnInit {
if (!authorId || authorId === '0') {
this.wordCloudWords = [];
this.wordCloudLegendTopics = [];
this.wordCloudEmpty = false;
this.wordCloudLoadFailed = false;
this.loadingWordCloud = false;
this.rawTopicOccurrences = [];
return;
}
this.loadingWordCloud = true;
this.wordCloudEmpty = false;
this.wordCloudLoadFailed = false;
this.wordCloudWords = [];
this.wordCloudLegendTopics = [];
@@ -389,104 +369,27 @@ export class PersonOverviewComponent implements OnInit {
this.loadingWordCloud = false;
if (this.rawTopicOccurrences.length === 0) {
this.wordCloudLegendTopics = [];
this.wordCloudEmpty = true;
return;
}
setTimeout(() => this.rebuildWordCloud());
},
error: (error) => {
console.error('Word cloud could not be loaded.', error);
this.loadingWordCloud = false;
this.rawTopicOccurrences = [];
this.wordCloudLegendTopics = [];
this.wordCloudEmpty = true;
this.wordCloudLoadFailed = true;
},
});
}
private rebuildWordCloud(): void {
const start = this.timelineStartYear;
const end = this.timelineEndYear;
const counts = new Map<string, number>();
const nameToId = new Map<string, string>();
const nameToColorInput = new Map<string, TopicLegendTopic>();
const wordCloud = buildResearchWordcloud(
this.rawTopicOccurrences,
this.timelineStartYear,
this.timelineEndYear,
);
for (const occ of this.rawTopicOccurrences) {
if (start !== undefined && (occ.year === undefined || occ.year < start))
continue;
if (end !== undefined && (occ.year === undefined || occ.year > end))
continue;
counts.set(occ.name, (counts.get(occ.name) ?? 0) + 1);
if (!nameToId.has(occ.name)) {
nameToId.set(occ.name, occ.topicId);
nameToColorInput.set(occ.name, {
id: occ.topicId,
name: occ.name,
field_id: occ.fieldId,
field_name: occ.fieldName,
domain_id: occ.domainId,
domain_name: occ.domainName,
subfield_id: occ.subfieldId,
subfield_name: occ.subfieldName,
});
}
}
if (counts.size === 0) {
this.wordCloudWords = [];
this.wordCloudLegendTopics = [];
this.wordCloudEmpty = true;
return;
}
this.wordCloudEmpty = false;
const allValues = Array.from(counts.values());
const minVal = Math.min(...allValues);
const maxVal = Math.max(...allValues);
const intensity = (v: number): number =>
maxVal === minVal ? 1 : (v - minVal) / (maxVal - minVal);
const sorted = Array.from(counts.entries()).sort((a, b) => b[1] - a[1]);
const topNames = sorted.slice(0, 30).map(([name]) => name);
this.wordCloudLegendTopics = topNames
.map((name) => nameToColorInput.get(name))
.filter((topic): topic is TopicLegendTopic => Boolean(topic));
const words = sorted.slice(0, 30).map(([name, value]): WordCloudWord => {
const t = intensity(value);
const colorInput = nameToColorInput.get(name);
return {
name,
value,
topicId: nameToId.get(name),
fieldName: colorInput?.field_name ?? undefined,
domainName: colorInput?.domain_name ?? undefined,
color: topicHierarchyColor(colorInput),
fontSize: Math.round(12 + t * (32 - 12)),
fontWeight: t > 0.66 ? 800 : t > 0.33 ? 600 : 400,
tooltip: buildTopicHierarchyTooltip({
topicName: name,
subfieldName: colorInput?.subfield_name,
fieldName: colorInput?.field_name,
domainName: colorInput?.domain_name,
}),
};
});
const centred: WordCloudWord[] = [];
words.forEach((word, i) => {
if (i % 2 === 0) {
centred.push(word);
} else {
centred.unshift(word);
}
});
this.wordCloudWords = centred;
this.wordCloudWords = wordCloud.words;
this.wordCloudLegendTopics = wordCloud.legendTopics;
}
private mapTimelineElements(