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
);
}
}
@@ -13,6 +13,15 @@ export interface ChartThemeColors {
cardBg: string;
text: string;
splitLine: string;
// Semantic canvas palette tokens (evaluated hex strings for canvas context)
primary: string;
blueHighlight: string;
warning: string;
success: string;
error: string;
authorColors: string[];
subfieldColors: string[];
}
@Injectable({
@@ -41,6 +50,50 @@ export class ChartThemeService {
tooltipTextColor: textVal,
labelColor: dark ? '#e8edf8' : '#ffffff',
cardBg: dark ? '#131b2e' : '#ffffff',
primary: dark ? '#a78bfa' : '#7255d9',
blueHighlight: dark ? '#60a5fa' : '#2563eb',
warning: dark ? '#fbbf24' : '#d97706',
success: dark ? '#34d399' : '#16a34a',
error: dark ? '#f87171' : '#dc2626',
authorColors: dark
? ['#60a5fa', '#fbbf24', '#34d399', '#c084fc']
: ['#1565c0', '#e6550d', '#2ca02c', '#9467bd'],
subfieldColors: dark
? [
'#60a5fa',
'#34d399',
'#fbbf24',
'#c084fc',
'#22d3ee',
'#f472b6',
'#a3e635',
'#f59e0b',
]
: [
'#2563eb',
'#16a34a',
'#d97706',
'#7c3aed',
'#0891b2',
'#db2777',
'#65a30d',
'#b45309',
],
};
});
/**
* Resolves a CSS custom property string into a concrete computed color value
*/
resolveCssColor(variable: string, fallback: string = ''): string {
if (typeof getComputedStyle !== 'function') {
return fallback;
}
const val = getComputedStyle(document.documentElement)
.getPropertyValue(variable)
.trim();
return val || fallback;
}
}