diff --git a/webapp/src/app/modules/person-overview/components/research-timeline/research-timeline.component.ts b/webapp/src/app/modules/person-overview/components/research-timeline/research-timeline.component.ts index cd8e52ec..0c05f0b3 100644 --- a/webapp/src/app/modules/person-overview/components/research-timeline/research-timeline.component.ts +++ b/webapp/src/app/modules/person-overview/components/research-timeline/research-timeline.component.ts @@ -30,6 +30,10 @@ import { toggleTopicLegendSubfieldSelection, } from '../../../../resolvers/utils/topic-legend'; import { buildTopicHierarchyTooltip } from '../../../../resolvers/utils/topic-tooltip'; +import { + formatYearRange, + normalizeSelectedYears, +} from '../../../../resolvers/utils/year-range'; import { workQueryParamKeys } from '../../../../resolvers/work-page.resolver'; export interface TopicTimelineTopic { @@ -303,12 +307,12 @@ export class ResearchTimelineComponent implements OnChanges, AfterViewChecked { this.topicSelect.emit({ topic, - years: this.normalizeSelectedYears(years), + years: normalizeSelectedYears(years), }); } selectYears(years: number[]): void { - const selectedYears = this.normalizeSelectedYears(years); + const selectedYears = normalizeSelectedYears(years); if (selectedYears.length === 0) { return; @@ -364,7 +368,7 @@ export class ResearchTimelineComponent implements OnChanges, AfterViewChecked { return null; } - const selectedYears = this.normalizeSelectedYears(years); + const selectedYears = normalizeSelectedYears(years); const cacheKey = `${topic.id}|${selectedYears.join(',')}`; const cached = this.topicActionCache.get(cacheKey); @@ -374,7 +378,7 @@ export class ResearchTimelineComponent implements OnChanges, AfterViewChecked { const person = this.personName || 'dieser Person'; const action: TimelineTopicAction = { - label: `Publikationen von ${person} ${this.formatYearRange(selectedYears)}`, + label: `Publikationen von ${person} ${formatYearRange(selectedYears)}`, link: this.personPublicationsLink, queryParams: { [workQueryParamKeys.page]: 0, @@ -421,18 +425,9 @@ export class ResearchTimelineComponent implements OnChanges, AfterViewChecked { } const person = this.personName || 'dieser Person'; - const selectedYears = this.normalizeSelectedYears(years); + const selectedYears = normalizeSelectedYears(years); - return `Publikationen von ${person} zum Thema ${topic.name} ${this.formatYearRange(selectedYears)}`; - } - - private formatYearRange(years: number[]): string { - const firstYear = years[0]; - const lastYear = years[years.length - 1]; - - return firstYear === lastYear - ? `(${firstYear})` - : `(${firstYear}–${lastYear})`; + return `Publikationen von ${person} zum Thema ${topic.name} ${formatYearRange(selectedYears)}`; } getYearPublicationTooltip(item: TopicTimelineElement): string { @@ -1040,12 +1035,6 @@ export class ResearchTimelineComponent implements OnChanges, AfterViewChecked { .slice(0, this.displayedPhaseTopicLimit); } - private normalizeSelectedYears(years: number[]): number[] { - return Array.from(new Set(years)) - .filter((year) => Number.isFinite(year)) - .sort((left, right) => left - right); - } - private getDominantPrimaryTopicCandidates( item: TopicTimelineElement, ): TopicTimelineTopic[] { diff --git a/webapp/src/app/modules/person-overview/components/research-wordcloud/research-wordcloud.component.ts b/webapp/src/app/modules/person-overview/components/research-wordcloud/research-wordcloud.component.ts index e6824385..70d0e77d 100644 --- a/webapp/src/app/modules/person-overview/components/research-wordcloud/research-wordcloud.component.ts +++ b/webapp/src/app/modules/person-overview/components/research-wordcloud/research-wordcloud.component.ts @@ -8,25 +8,21 @@ import { getTopicLegendSubfieldId, TopicLegendTopic, } from '../../../../resolvers/utils/topic-legend'; +import type { + TopicTimelineElement, + TopicTimelineTopic, +} from '../research-timeline/research-timeline.component'; +import { + formatYearRange, + normalizeSelectedYears, +} from '../../../../resolvers/utils/year-range'; import { workQueryParamKeys } from '../../../../resolvers/work-page.resolver'; interface WordTooltipInputs { topicId: string; secondaryActionLabel: string | null; secondaryActionLink: unknown[] | null; - secondaryActionQueryParams: Record; -} - -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; + secondaryActionQueryParams: Record; } export interface WordCloudWord { @@ -49,67 +45,46 @@ const displayedWordLimit = 30; const minimumFontSize = 12; const maximumFontSize = 32; +interface AggregatedTopic { + name: string; + count: number; + legendTopic: TopicLegendTopic; +} + export function buildResearchWordcloud( - occurrences: WordCloudTopicOccurrence[], + elements: readonly TopicTimelineElement[], startYear: number | undefined, endYear: number | undefined, ): ResearchWordcloud { - const counts = new Map(); - const legendTopicsByName = new Map(); + const yearsInRange = elements.filter((element) => + isWithinYearRange(element.year, startYear, endYear), + ); - for (const occurrence of occurrences) { - if (!isWithinYearRange(occurrence.year, startYear, endYear)) { - continue; - } + const aggregated = aggregateTopics(yearsInRange); - 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) { + if (aggregated.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]) + const displayed = Array.from(aggregated.values()) + .sort( + (left, right) => + right.count - left.count || left.name.localeCompare(right.name), + ) .slice(0, displayedWordLimit); - const legendTopics = displayed - .map(([name]) => legendTopicsByName.get(name)) - .filter((topic): topic is TopicLegendTopic => Boolean(topic)); + const intensity = buildIntensityScale(displayed); - const words = displayed.map(([name, value]): WordCloudWord => { - const legendTopic = legendTopicsByName.get(name); - const scale = intensity(value); + const words = displayed.map((topic): WordCloudWord => { + const legendTopic = topic.legendTopic; + const scale = intensity(topic.count); return { - name, - topicId: legendTopic?.id ?? '', - rawTopicId: stripOpenAlexId(legendTopic?.id), - fieldLegendId: legendTopic ? getTopicLegendFieldId(legendTopic) : '', - subfieldLegendId: legendTopic - ? getTopicLegendSubfieldId(legendTopic) - : '', + name: topic.name, + topicId: legendTopic.id ?? '', + rawTopicId: stripOpenAlexId(legendTopic.id), + fieldLegendId: getTopicLegendFieldId(legendTopic), + subfieldLegendId: getTopicLegendSubfieldId(legendTopic), color: topicHierarchyColor(legendTopic), fontSize: Math.round( minimumFontSize + scale * (maximumFontSize - minimumFontSize), @@ -118,7 +93,72 @@ export function buildResearchWordcloud( }; }); - return { words: centreWords(words), legendTopics }; + return { + words: centreWords(words), + legendTopics: displayed.map((topic) => topic.legendTopic), + }; +} + +function aggregateTopics( + elements: readonly TopicTimelineElement[], +): Map { + const aggregated = new Map(); + + for (const element of elements) { + for (const topic of element.primary_topics ?? []) { + if (!topic.name) { + continue; + } + + const key = topic.id ?? topic.name; + const existing = aggregated.get(key); + + if (existing) { + existing.count += topic.count; + continue; + } + + aggregated.set(key, { + name: topic.name, + count: topic.count, + legendTopic: toLegendTopic(topic), + }); + } + } + + return aggregated; +} + +function buildIntensityScale( + topics: readonly AggregatedTopic[], +): (count: number) => number { + const weights = topics.map((topic) => Math.sqrt(Math.max(topic.count, 0))); + const minimumWeight = Math.min(...weights); + const maximumWeight = Math.max(...weights); + + if (maximumWeight === minimumWeight) { + return () => 0.5; + } + + return (count) => + (Math.sqrt(Math.max(count, 0)) - minimumWeight) / + (maximumWeight - minimumWeight); +} + +function toLegendTopic(topic: TopicTimelineTopic): TopicLegendTopic { + return { + id: topic.id, + name: topic.name, + field_id: topic.field_id, + field_name: topic.field_name, + field_description: topic.field_description, + domain_id: topic.domain_id, + domain_name: topic.domain_name, + domain_description: topic.domain_description, + subfield_id: topic.subfield_id, + subfield_name: topic.subfield_name, + subfield_description: topic.subfield_description, + }; } function isWithinYearRange( @@ -184,6 +224,11 @@ export class ResearchWordcloudComponent { this.rebuildTooltipInputs(); } + @Input() set selectedYears(value: number[]) { + this._selectedYears = normalizeSelectedYears(value); + this.rebuildTooltipInputs(); + } + @Input() loading = false; @Input() loadFailed = false; @Input() activeFieldIds: ReadonlySet = new Set(); @@ -194,6 +239,7 @@ export class ResearchWordcloudComponent { private _words: WordCloudWord[] = []; private _personPublicationsLink: unknown[] | null = null; private _personName = ''; + private _selectedYears: number[] = []; private tooltipInputsByTopic = new Map(); selectWord(word: WordCloudWord): void { @@ -217,15 +263,23 @@ export class ResearchWordcloudComponent { } wordAriaLabel(word: WordCloudWord): string { + return `${this.publicationsLabel()} zum Thema ${word.name}${this.yearRangeSuffix()}`; + } + + private publicationsLabel(): string { return this._personName - ? `Publikationen von ${this._personName} zum Thema ${word.name}` - : `Publikationen dieser Person zum Thema ${word.name}`; + ? `Publikationen von ${this._personName}` + : 'Publikationen dieser Person'; + } + + private yearRangeSuffix(): string { + return this._selectedYears.length > 0 + ? ` ${formatYearRange(this._selectedYears)}` + : ''; } private rebuildTooltipInputs(): void { - const label = this._personName - ? `Publikationen von ${this._personName}` - : 'Publikationen dieser Person'; + const label = `${this.publicationsLabel()}${this.yearRangeSuffix()}`; const link = this._personPublicationsLink; this.tooltipInputsByTopic = new Map( @@ -239,6 +293,9 @@ export class ResearchWordcloudComponent { secondaryActionLink: link, secondaryActionQueryParams: { [workQueryParamKeys.page]: 0, + ...(this._selectedYears.length > 0 + ? { [workQueryParamKeys.years]: this._selectedYears } + : {}), [workQueryParamKeys.topicId]: word.topicId, }, }, diff --git a/webapp/src/app/modules/person-overview/person-overview.component.html b/webapp/src/app/modules/person-overview/person-overview.component.html index 0596c00b..e1a23acf 100644 --- a/webapp/src/app/modules/person-overview/person-overview.component.html +++ b/webapp/src/app/modules/person-overview/person-overview.component.html @@ -29,6 +29,7 @@ [loadFailed]="wordCloudLoadFailed" [activeFieldIds]="activeLegendFieldIds" [activeSubfieldIds]="activeLegendSubfieldIds" + [selectedYears]="wordCloudYears" [personName]="personName" [personPublicationsLink]="personPublicationsLink" (wordSelect)="onWordClick($event)" /> diff --git a/webapp/src/app/modules/person-overview/person-overview.component.ts b/webapp/src/app/modules/person-overview/person-overview.component.ts index 1aeb032c..c049f600 100644 --- a/webapp/src/app/modules/person-overview/person-overview.component.ts +++ b/webapp/src/app/modules/person-overview/person-overview.component.ts @@ -15,7 +15,6 @@ import { import { buildResearchWordcloud, ResearchWordcloudComponent, - WordCloudTopicOccurrence, WordCloudWord, } from './components/research-wordcloud/research-wordcloud.component'; import { workQueryParamKeys } from '../../resolvers/work-page.resolver'; @@ -40,7 +39,7 @@ export class PersonOverviewComponent implements OnInit { 'timeline_legend_visible'; private static readonly timelineLegendExpandedQueryParam = 'timeline_legend_expanded'; - private static readonly timelineTopicCountLimit = 4; + private static readonly timelineTopicCountLimit = 20; private static readonly initialTimelineTargetPublications = 250; private readonly route = inject(ActivatedRoute); @@ -66,6 +65,7 @@ export class PersonOverviewComponent implements OnInit { wordCloudWords: WordCloudWord[] = []; wordCloudLegendTopics: TopicLegendTopic[] = []; + wordCloudYears: number[] = []; loadingWordCloud = false; wordCloudLoadFailed = false; activeLegendFieldIds: ReadonlySet = new Set(); @@ -73,8 +73,6 @@ export class PersonOverviewComponent implements OnInit { personName = ''; personPublicationsLink: unknown[] | null = null; - private rawTopicOccurrences: WordCloudTopicOccurrence[] = []; - ngOnInit(): void { combineLatest(this.route.pathFromRoot.map((route) => route.paramMap)) .pipe( @@ -88,7 +86,6 @@ export class PersonOverviewComponent implements OnInit { ? ['/person', authorId, 'publications'] : null; this.loadTopicTimeline(authorId); - this.loadWordCloud(authorId); }); this.loadPersonName(); @@ -99,12 +96,24 @@ export class PersonOverviewComponent implements OnInit { this.timelineViewMode = this.parseTimelineViewMode( queryParams.get(PersonOverviewComponent.timelineViewQueryParam), ); - this.timelineStartYear = this.parseOptionalYear( + + const startYear = this.parseOptionalYear( queryParams.get(PersonOverviewComponent.timelineStartYearQueryParam), ); - this.timelineEndYear = this.parseOptionalYear( + const endYear = this.parseOptionalYear( queryParams.get(PersonOverviewComponent.timelineEndYearQueryParam), ); + const yearRangeChanged = + startYear !== this.timelineStartYear || + endYear !== this.timelineEndYear; + + this.timelineStartYear = startYear; + this.timelineEndYear = endYear; + + if (yearRangeChanged) { + this.rebuildWordCloud(); + } + this.timelineLegendVisible = this.parseBooleanQueryParam( queryParams.get( PersonOverviewComponent.timelineLegendVisibleQueryParam, @@ -219,6 +228,9 @@ export class PersonOverviewComponent implements OnInit { relativeTo: this.route, queryParams: { [workQueryParamKeys.page]: 0, + ...(this.wordCloudYears.length > 0 + ? { [workQueryParamKeys.years]: this.wordCloudYears } + : {}), [workQueryParamKeys.topicId]: word.topicId, }, }); @@ -246,9 +258,11 @@ export class PersonOverviewComponent implements OnInit { return; } - this.topicTimelineElements = []; + this.setTimelineElements([]); this.loadingTimeline = true; + this.loadingWordCloud = true; this.timelineLoadFailed = false; + this.wordCloudLoadFailed = false; this.timelineCanLoadEarlierYears = false; this.timelineLoadingEarlierYears = false; this.timelineEarlierYearsLoadFailed = false; @@ -269,9 +283,11 @@ export class PersonOverviewComponent implements OnInit { const timelineElements = this.mapTimelineElements(response.elements); - this.topicTimelineElements = timelineElements; + this.setTimelineElements(timelineElements); this.loadingTimeline = false; + this.loadingWordCloud = false; this.timelineLoadFailed = false; + this.wordCloudLoadFailed = false; this.timelineCanLoadEarlierYears = response.is_limited ?? this.isLikelyLimitedTimeline(timelineElements); @@ -282,9 +298,11 @@ export class PersonOverviewComponent implements OnInit { } console.error('Topic timeline could not be loaded.', error); - this.topicTimelineElements = []; + this.setTimelineElements([]); this.loadingTimeline = false; + this.loadingWordCloud = false; this.timelineLoadFailed = true; + this.wordCloudLoadFailed = true; this.timelineCanLoadEarlierYears = false; this.timelineLoadingEarlierYears = false; this.timelineEarlierYearsLoadFailed = false; @@ -316,9 +334,11 @@ export class PersonOverviewComponent implements OnInit { response.elements, ); - this.topicTimelineElements = this.mergeTimelineElements( - this.topicTimelineElements, - completeTimelineElements, + this.setTimelineElements( + this.mergeTimelineElements( + this.topicTimelineElements, + completeTimelineElements, + ), ); this.timelineCanLoadEarlierYears = false; this.timelineLoadingEarlierYears = false; @@ -338,9 +358,11 @@ export class PersonOverviewComponent implements OnInit { } private resetTimelineState(): void { - this.topicTimelineElements = []; + this.setTimelineElements([]); this.loadingTimeline = false; + this.loadingWordCloud = false; this.timelineLoadFailed = false; + this.wordCloudLoadFailed = false; this.timelineCanLoadEarlierYears = false; this.timelineLoadingEarlierYears = false; this.timelineEarlierYearsLoadFailed = false; @@ -365,69 +387,37 @@ export class PersonOverviewComponent implements OnInit { }); } - private loadWordCloud(authorId: string): void { - if (!authorId || authorId === '0') { - this.wordCloudWords = []; - this.wordCloudLegendTopics = []; - this.wordCloudLoadFailed = false; - this.loadingWordCloud = false; - this.rawTopicOccurrences = []; - return; - } - - this.loadingWordCloud = true; - this.wordCloudLoadFailed = false; - this.wordCloudWords = []; - this.wordCloudLegendTopics = []; - - this.worksService - .getAuthorWorks(authorId, 1, 100) - .pipe(takeUntilDestroyed(this.destroyRef)) - .subscribe({ - next: (page) => { - this.rawTopicOccurrences = []; - page.elements?.forEach((work) => { - const year = work.publication_year ?? undefined; - work.topics?.forEach((topic) => { - if (topic.display_name && topic.id) { - this.rawTopicOccurrences.push({ - name: topic.display_name, - topicId: topic.id, - year, - fieldId: topic.field?.id ?? undefined, - fieldName: topic.field?.display_name ?? undefined, - domainId: topic.domain?.id ?? undefined, - domainName: topic.domain?.display_name ?? undefined, - subfieldId: topic.subfield?.id ?? undefined, - subfieldName: topic.subfield?.display_name ?? undefined, - }); - } - }); - }); - - this.loadingWordCloud = false; - - setTimeout(() => this.rebuildWordCloud()); - }, - error: (error) => { - console.error('Word cloud could not be loaded.', error); - this.loadingWordCloud = false; - this.rawTopicOccurrences = []; - this.wordCloudLegendTopics = []; - this.wordCloudLoadFailed = true; - }, - }); + private setTimelineElements(elements: TopicTimelineElement[]): void { + this.topicTimelineElements = elements; + this.rebuildWordCloud(); } private rebuildWordCloud(): void { const wordCloud = buildResearchWordcloud( - this.rawTopicOccurrences, + this.topicTimelineElements, this.timelineStartYear, this.timelineEndYear, ); this.wordCloudWords = wordCloud.words; this.wordCloudLegendTopics = wordCloud.legendTopics; + this.wordCloudYears = this.getSelectedTimelineYears(); + } + + private getSelectedTimelineYears(): number[] { + const yearsInRange = this.topicTimelineElements + .map((element) => element.year) + .filter( + (year) => + (this.timelineStartYear === undefined || + year >= this.timelineStartYear) && + (this.timelineEndYear === undefined || year <= this.timelineEndYear), + ) + .sort((left, right) => left - right); + + return yearsInRange.length === this.topicTimelineElements.length + ? [] + : yearsInRange; } private mapTimelineElements( diff --git a/webapp/src/app/resolvers/utils/year-range.ts b/webapp/src/app/resolvers/utils/year-range.ts new file mode 100644 index 00000000..0dbf2069 --- /dev/null +++ b/webapp/src/app/resolvers/utils/year-range.ts @@ -0,0 +1,14 @@ +export function normalizeSelectedYears(years: readonly number[]): number[] { + return Array.from(new Set(years)) + .filter((year) => Number.isFinite(year)) + .sort((left, right) => left - right); +} + +export function formatYearRange(years: readonly number[]): string { + const firstYear = years[0]; + const lastYear = years[years.length - 1]; + + return firstYear === lastYear + ? `(${firstYear})` + : `(${firstYear}–${lastYear})`; +}