implement dynamic color changes for e-chart (test)

This commit is contained in:
Kazgatari
2026-08-01 19:31:47 +02:00
parent 496d2c88f0
commit 8a801d0bb5
2 changed files with 97 additions and 21 deletions
@@ -1,7 +1,8 @@
import { Component, computed, input } from '@angular/core';
import { Component, computed, inject, input } from '@angular/core';
import { EChartsOption } from 'echarts';
import { NgxEchartsDirective } from 'ngx-echarts';
import { WorkPrimaryCategory } from '../../../api';
import { ChartThemeService } from '../../../shared/services/chart-theme.service';
import { SUBFIELD_COLORS } from '../compare-authors-colors';
export interface AuthorCategoryLegendItem {
@@ -69,6 +70,8 @@ export const AUTHOR_CATEGORY_LEGEND: readonly AuthorCategoryLegendItem[] = [
export class AuthorCategoryPieComponent {
counts = input<Partial<Record<WorkPrimaryCategory, number>> | null>(null);
private readonly chartTheme = inject(ChartThemeService);
readonly total = computed(() =>
Object.values(this.counts() ?? {}).reduce(
(sum, count) => sum + Math.max(0, count),
@@ -79,11 +82,40 @@ export class AuthorCategoryPieComponent {
readonly options = computed<EChartsOption>(() => {
const counts = this.counts() ?? {};
const total = this.total();
const textColor = this.cssColor('--sg-text', '#0f172a');
const mutedColor = this.cssColor('--sg-text-muted', '#64748b');
const colors = this.chartTheme.colors();
const textColor = colors.text;
const mutedColor = colors.mutedTextColor;
const tooltipBg = colors.cardBg;
const tooltipBorder = colors.splitLine;
const palette = colors.subfieldColors;
const tooltipBg = this.cssColor('--sg-card-bg', '#ffffff');
const tooltipBorder = this.cssColor('--sg-border', '#dde5f2');
const categoryLegend: readonly AuthorCategoryLegendItem[] = [
categoryLegendItem(
WorkPrimaryCategory.BasicResearch,
'Grundlagenforschung',
palette[0],
),
categoryLegendItem(
WorkPrimaryCategory.AppliedResearch,
'Anwendungsbasierte Forschung',
palette[1],
),
categoryLegendItem(
WorkPrimaryCategory.ExperimentalDevelopment,
'Experimentelle Entwicklung',
palette[2],
),
categoryLegendItem(
WorkPrimaryCategory.NonResearch,
'Nicht-forschende Tätigkeit',
palette[3],
),
categoryLegendItem(
WorkPrimaryCategory.NotClassified,
'Nicht klassifiziert',
palette[4],
),
];
return {
animation: false,
@@ -139,24 +171,15 @@ export class AuthorCategoryPieComponent {
scale: true,
scaleSize: 6,
},
data: AUTHOR_CATEGORY_LEGEND.map((item) => ({
name: item.label,
value: Math.max(0, counts[item.category] ?? 0),
itemStyle: { color: item.color },
})).filter((item) => item.value > 0),
data: categoryLegend
.map((item) => ({
name: item.label,
value: Math.max(0, counts[item.category] ?? 0),
itemStyle: { color: item.color },
}))
.filter((item) => item.value > 0),
},
],
};
});
private cssColor(variable: string, fallback: string): string {
if (typeof getComputedStyle !== 'function') {
return fallback;
}
return (
getComputedStyle(document.documentElement)
.getPropertyValue(variable)
.trim() || fallback
);
}
}