# Election map: moving 'which elections exist and how to draw them' into one config file, with no per-dataset branches left in the site code

> Adding a new election cycle needed no code change, but a new kind of election still touched many places. Datasets, party colors, cycles, the update flow and panel fields now live in one datasets.json.

- Date: 2026-12-01
- Topics: Data, Next.js
- Chinese original: https://kbs0830.com/blog/election-map-config-driven

The conclusion of [the previous post](/en/blog/election-map-modular-2028) was: adding a new **election cycle** needs no code change, but adding a new **kind of election** still means changing several places. This post deals with the latter.

## Before: what was hard-coded

Counting them up, the site had thirty-odd instances of `s.ds === "C"`, `"L"`, `"V"`: legend text, tooltips, list headers, and which sections the detail panel shows, all branching on a dataset code. On the Python side three places were also hard-coded separately: party colors, the groups in `update.py`, and the mayor election cycle table.

## One config, read by both sides

`config/datasets.json` is now the single source:

- `parties`: party colors, short names, fallback colors
- `datasets[]`: for each dataset its code, file name, type (with candidates / party-only / referendum), cycle rules, metric list, and `ui` (detail panel sections, list columns, all kinds of explanatory text)
- `groups`: which fetch and cleaning scripts each group of `update.py` runs
- `mayorCycles`: the cycles for mayors, councilors, and village/neighborhood chiefs
- `vocab`: the names the config is allowed to use

The site does not read the Python project; a sync script copies the same file in.

## What remains as code: the "shape" of a record

A config file cannot express this: column 6 of a record is seats, column 7 is the independent share. So how values are read (`VALUES`), the detail panel sections, and how the two list columns are computed are still code; the config refers to them by **name**, and the list of names lives in `vocab`. So:

- Adding a dataset that only uses existing names: change only the config file.
- Needing a new value or section: add a function in code and add its name to `vocab`; if the two lists disagree, a test fails.

## How I knew nothing broke

1. **Save the output before refactoring.** On the Python side I saved the nine JSON files in `dist/` and the six files of the old static site, then compared item by item after the refactor: contents are identical. The comparison even caught that I had computed the leading value of "units with only independents" as 0 (it should be 100).
2. **The site side draws the map once for every metric of every dataset**, and checks that no tooltip for any unit contains `undefined`, `NaN` or an unreplaced `{`. This test is the proof that the config really drives the screen.
3. **vocab against code**: the list of names in the config must be exactly the same as the ones implemented in code.
4. **A hash of the config**: when Python exports, it computes the SHA-1 of `datasets.json` and puts it in `meta.json`, and the site's test compares it with its own copy. If it is out of sync or edited by hand, the test fails.
5. A production build with 13 E2E tests, type check and lint.

## What is not done yet

- A brand-new **type** (neither party nor agree/disagree) still needs its drawing code written. The config file governs things of the same type.
- The fetch and cleaning scripts are still one per dataset because each data source is different; they are only registered in the config, not shared.
- There is no real 2028 data yet, so the whole flow has only been tested with fake data.

---

Author: PINGWEI LI (李秉威, Li Ping-Wei)
Original page: https://kbs0830.com/en/blog/election-map-config-driven
