ECharts in an Astro Blog

A Chart That Cannot Read Your CSS

25.11.2026 | 15 Jumada Al-Akhirah 1448
8 min read

بِسْمِ ٱللّٰهِ ٱلرَّحْمَـٰنِ ٱلرَّحِيمِ

A page has one source of truth for how it looks: its stylesheet. Colours are CSS variables, dark mode swaps them, and a reader who picks another font changes one more variable. Every element follows, except the ones drawn by a library that does not read CSS.

ECharts is such a library. It draws charts from a JavaScript object of options, and it takes colours, fonts and sizes as values inside that object. On this blog that meant a chart in the wrong colours after a theme switch and in the wrong font after a reader setting, unless something carried the page’s choices across. This is how the blog’s E_Chart component does that, what it costs, and where it stops.

The chart on the Umami post

The site has one ECharts chart. It sits in the post on self-hosted analytics and compares two scripts after gzip: the Umami tracker and the Partytown runtime a site would add to move such scripts off the main thread.

Gzipped script size: Umami tracker vs Partytown runtime (loader + worker)

Switch the theme at the top of the page and the chart follows, in the same colours as the text around it. That is the part readers see. The rest of this post is the part they do not.

internal link pub/digital-solutions/setups/umami-analytics

Why ECharts cannot read the theme

The site’s colours are CSS variables in OKLCH, such as --muted-foreground: oklch(0.60 0.008 265). ECharts needs the colour itself, not a reference to it, for two reasons.

The first is that its options are plain values. A variable only means something inside the browser’s style system, and an option object is not part of it. ECharts passes a string like var(--border) through to its SVG, where the browser resolves it, but that is luck rather than support.

The second is that ECharts computes with colours. It derives a lighter shade of a bar for hover, for example, so it parses every colour it is given. Its parser (zrender 6.0) reads hex, rgb, hsl and colour names such as red. Given oklch(0.60 0.008 265) or var(--muted-foreground), it returns nothing, and every colour derived from that one breaks.

Fonts and sizes have the same problem. The chart’s text does not inherit the font of the article around it; ECharts needs a font name and a pixel size in its options.

Carrying the theme across

E_Chart closes the gap with a small bridge that runs before every render:

The colour conversion is the trick that makes it work. The browser already knows how to turn any CSS colour into pixels, so the component lets it: paint one pixel on a canvas in the variable’s colour, read the pixel back, write it as hex.

function cssVarToHex(cssVar: string, element: Element = document.documentElement): string {
const raw = getComputedStyle(element).getPropertyValue(cssVar).trim();
const canvas = document.createElement("canvas");
canvas.width = canvas.height = 1;
const ctx = canvas.getContext("2d");
if (!ctx) return "#888888";
ctx.fillStyle = raw;
ctx.fillRect(0, 0, 1, 1);
const [r, g, b] = ctx.getImageData(0, 0, 1, 1).data;
return "#" + [r, g, b].map(n => n.toString(16).padStart(2, "0")).join("");
}

The element argument matters for one colour. The body text of a post is not a page-wide token but a variable set on the article (--tw-prose-body), so the component reads it from the article element. Chart labels then have exactly the colour of the sentences next to them.

With the colours in hex, the component registers an ECharts theme named jav: axis lines and grid in the border colour, labels in the body-text colour, the tooltip as a card with the site’s radius, and one series colour, the muted grey, so charts stay as quiet as the rest of the site. Two ECharts defaults are switched off on the way. On dark backgrounds ECharts draws an outline around label text, which the site’s colours do not need. On hover it brightens a bar, where the site’s flat style dims it instead.

Fonts and reader settings

The blog has a reader panel for font and text size. It saves the choice in localStorage and applies it to the article through CSS. The chart cannot see that CSS, so the component reads the same storage keys and repeats the panel’s decisions: which font variable belongs to which choice, and the size factors for small, medium and large, applied to a chart-sized base of 12 pixels.

That repetition is the weakest part of the bridge. The panel’s font table and size factors now live in two files, and nothing fails if one changes without the other. The chart would simply keep the old font.

When to draw again

A bridge that runs once is not enough, because the page changes after loading. The component draws the chart again when:

What readers download

ECharts is not small, so the component makes sure only pages with a chart pay for it, and only once the chart is near the screen:

Measured from this site’s production build, the chart script on the Umami post is 1,096 KB as files, 357 KB after gzip and 290 KB after brotli, which is the compressed form the server sends. The chart it draws compares a 1.43 KB tracker with a 14.8 KB runtime. Astaghfirullah: to make the point that 14.8 KB of overhead is too much, the page downloads about twenty times as much to draw two bars.

Most of that is ECharts code the chart does not use. The component imports the whole package so that a post can use any chart type without touching the component. A version that imports only the bar chart, the grid, the tooltip and the SVG renderer came to 135 KB after brotli in a test bundle with the same Vite version. That is less than half, and still nine times the runtime the chart argues against.

Labels and options

Two smaller decisions shape how a chart is written:

Where it stops

A chart outside the stylesheet

Every part of the bridge exists because the chart cannot read the page it sits on. The colour conversion, the copied reader settings, the observers that draw again: each repeats something the stylesheet already knows. That repetition is the real cost of a chart library that keeps its own theme, more than the kilobytes. It is also the question the next step started from: is there a chart library that reads the page directly?

internal link pub/coding/web/echarts-to-tanstack-charts
FAQ
Can ECharts use CSS variables at all?

Partly. A string such as var(--border) passes through to the SVG and the browser resolves it. But ECharts also computes colours itself, for example a lighter shade on hover, and its colour parser only reads hex, rgb, hsl and colour names such as red. For oklch() or var() it returns nothing, so every colour ECharts derives from one of those breaks. Converting the tokens to hex before ECharts sees them avoids that.

Why not render ECharts on the server instead?

ECharts can render to an SVG string without a browser (since version 5.3), and that SVG needs no JavaScript to show. The catch is that it is drawn once: every colour not set explicitly comes out fixed, and tooltips or a later theme switch need the full library in the browser again. For a site with a theme switch and reader settings, the browser version is the one that keeps up.

Is the full ECharts package necessary?

No. ECharts can be imported piece by piece: only the chart types, components and renderer a page uses. For one bar chart with a grid, a tooltip and the SVG renderer, a test bundle with the same Vite version came to 135 KB after brotli instead of 285 KB. This site's component still imports the whole package, because it was meant for any chart type a post might need.