/* * The colours, fonts and sizes of a chart from the --pk-* tokens of a report's theme: what a drawn * chart (pivotkit-react's chart, an SVG, a printed report) and a workbook's colour scales read. A * file of its own, so a report's first load, which draws no chart yet, does not carry it * (pivotkit-element's build). */ import { CHART_COLORS } from "./chart-plan.js"; import { pxOf } from "./defaults.js"; /** * The theme of toEChartsOption and toXlsx from the --pk-* tokens of a report: the computed style of a * report's root, or any object with getPropertyValue (a map of tokens, as pivotkit-report passes). * A token missing, or not what the chart can read (a weight as a keyword, a size not in px), keeps * DEFAULT_THEME's value. pivotkit-react resolves the sizes to px first (chart-decorate.js, chartStyle). * @param {{ getPropertyValue(name: string): string }} style * @returns {import("./chart.js").ChartTheme} */ export function chartThemeFrom(style) { const v = (name) => style.getPropertyValue(name).trim(); const colors = []; for (let i = 1; i <= CHART_COLORS; i++) { const c = v(`--pk-chart-${i}`); if (c) { colors.push(c); } } /** @type {import("./chart.js").ChartTheme} */ const theme = { colors, textColor: v("--pk-muted") || undefined, axisColor: v("--pk-chart-axis") || v("--pk-border") || undefined, gridColor: v("--pk-grid") || v("--pk-border") || undefined, fontFamily: v("font-family") || undefined, backgroundColor: "transparent", referenceColor: v("--pk-chart-reference") || undefined }; // a number for ECharts (1 to 1000, as in CSS); a host's keyword (bold) or an empty token keeps DEFAULT_THEME's const strong = Number(v("--pk-weight-strong")); if (strong >= 1 && strong <= 1000) { theme.strongWeight = strong; } const title = pxOf(v("--pk-chart-title-size")); if (title !== null && title >= 1) { theme.titleSize = title; } const text = pxOf(v("--pk-chart-text-size")); if (text !== null && text >= 1) { theme.textSize = text; } const low = v("--pk-heat-low"); const high = v("--pk-heat-high"); if (low && high) { theme.heatmapColors = [low, high]; } // the colours of a workbook's colour scale and data bars whose view names none (toXlsx), as CSS // writes them: a caller that writes a workbook makes them #rrggbb first const [scaleLow, scaleMid, scaleHigh, bar] = ["--pk-scale-low", "--pk-scale-mid", "--pk-scale-high", "--pk-chart-1"].map(v); if (scaleLow && scaleHigh) { theme.colorScale = scaleMid ? [scaleLow, scaleMid, scaleHigh] : [scaleLow, scaleHigh]; } if (bar) { theme.dataBar = bar; } return theme; }