A heatmap panel, ported from an open-source app
By the end of this lesson you will have a heatmap panel that draws your screen as tiles sized by market capitalisation and coloured by the day's change, its layout and colour scale ported from EODHD's open-source heatmap with the licence notice kept, tests that hold the port to the original's numbers, and a second built-in layout, Markets, where a click on a tile moves the quote panel.
EODHD APIs publishes seven example apps under the MIT licence, and three of them hold a panel your cockpit wants: eodhd-heatmap, eodhd-news-sentiment and eodhd-portfolio-tracker. This lesson and the next two take one recipe from each. The skill is porting: reading someone else's working code and moving the part that matters into yours, under your contract and your tests.
What to take from an app
The apps are built differently from your cockpit. Each is a set of plain script files that hang their functions on window, with its own small server and page shell; your cockpit is Next.js and TypeScript with a panel contract. Fork one and you maintain a second product. Copy a file whole and you bring globals into a typed codebase that spent three courses getting rid of them.
What you want from an app is its recipe: a few pure functions and the rules behind them. The heatmap's recipe is under two hundred lines of a file of about 2,400.
What the licence asks
MIT lets you use the code, change it and ship it in something you sell. Its one condition is that the copyright notice travels with the code, so the ported file opens with a comment naming the repository, the file and the licence. The licence covers the code only. What you may do with the data the API returns is set by your own EODHD subscription, as the app's README says.
Read, pin, port
Three steps, and their order is the method:
- Read. Ask the agent for the recipe as a list of rules, each with the file and line it comes from, and no code. Rules are quicker to check than code, and a rule without a line number is the agent's guess.
- Pin. Turn the rules into tests that use the original's own numbers: its colour cut-offs, a layout whose areas you can check by hand. Write them before the port. A test written after it checks the port against itself.
- Port. Into a pure module under your contract. Anything you change on purpose gets a test of its own and a line in the header comment, so the next reader can tell a decision from a slip.
The heatmap's recipe
Layout. A squarified treemap, the algorithm Bruls, Huizing and van Wijk published in 2000. It fills the rectangle a row at a time and keeps adding tiles to the current row while that does not make the row's worst aspect ratio worse, so tiles stay close to square and their labels fit. Every tile's area is proportional to its weight.
Two levels. Sectors first, each sized by the summed market capitalisation of its companies, largest first. Then each sector's companies inside its rectangle, below a strip that holds the sector's name.
A missing weight. A company with no market capitalisation gets a weight of 1. It shows as a sliver, so the company is still on the map.
Colour. Seven steps, read from the day's change rounded to one decimal:
| Change, rounded | Fill |
|---|---|
| 3% and above | #128651 |
| 1% to 2.9% | #177a44 |
| 0.1% to 0.9% | #35684f |
| 0 | #475569 |
| −0.1% to −0.9% | #a44148 |
| −1% to −2.9% | #b93e3e |
| −3% and below | #cb4242 |
The rounding comes first, and it decides the edges of every step.
A missing number is not zero
A tile whose change you do not have is grey and reads "—". Writing 0.0% on it would claim the price did not move, and you do not know that. The rule comes back twice in this unit, for a sentiment reading and for a position with no price: a dashboard that fills a gap with a plausible number gives you a wrong figure with nothing to mark it.
Colour is never the only signal
A colour scale says nothing to a screen reader, or to someone who cannot tell this red from this green. So every tile also prints its signed change, and its aria-label says it in words: "NVDA, Technology, up 2.6% today". White text on the seven fills has a contrast of 4.6:1 to 7.6:1, above the 4.5:1 that WCAG AA asks of normal text.
Price it before you draw it
The app draws an index: its member list from fundamentals (10 calls, kept for a day), a screener page (5), the exchange's trading hours (1) and, while the market is open, a live quote for each of the 30 tiles on screen. Its README puts a cold load at about 16 calls, or about 46 while the US market is open.
Your panel draws a screen you already have. A screener row carries the sector, the market capitalisation and the one-day change (refund_1d_p), so the map is one request, 5 calls. When a screen panel shows the same screen with the same sort, the container from this unit's first lesson makes the two panels one request. The Screener API is not on the Free plan: on a free key the panel shows the 403 sentence from the last lesson.
A layout of its own
The heatmap goes into a second built-in layout, Markets, and the default stays as it is: unit 4 prices the default's refresh, and a map in it would change every number there. In Markets a quote panel follows the selection, so a click on a tile shows that company's quote. That is lesson 2's wiring at work: the heatmap reports a symbol through onSelect, and nothing in it knows a quote panel exists.
Try it now
Two prompts. The first only reads; the second builds from what it read.
The prompt
Mock mode on, plan mode on, and paste:
Clone https://github.com/EodHistoricalData/eodhd-heatmap into
../eodhd-heatmap, next to this repo, and read vendor/heatmap.js.
Change nothing in either repo. Give me the recipe as a list of rules,
each with the file and line it comes from: how the treemap lays out
its tiles, how it groups and sizes sectors, what weight a company with
no market cap gets, how a change becomes a colour (every cut-off and
every colour), and what a tile shows when its change is unknown. No
code.
Then, once the rules read right:
Plan, then build, a heatmap panel from those rules. In a new
src/lib/heatmap.ts: squarify(items, rect), the squarified layout,
where a weight that is missing or not positive counts as 1;
treemap(rows, rect), sectors sized by summed market_capitalization,
largest first, and each sector's rows laid out inside its rectangle
below a strip for its name; changeOf(row), refund_1d_p, or null when
that is not a number, never 0; changeColor(change), the seven steps
on the change rounded to one decimal. The file opens with a comment
naming EodHistoricalData/eodhd-heatmap, vendor/heatmap.js and the MIT
licence. Write tests/heatmap.test.ts first: areas proportional to
weights, filling the rectangle with no overlap; every colour cut-off;
a row with no change giving null. Add a heatmap panel type whose
config is a screen config and whose dataNeeds asks for exactly the
screen a screen panel with the same filters and sort asks for. It
draws an SVG; each tile prints its ticker and signed change, or a
dash when the change is unknown, carries an aria-label that says the
same in words, and reports a click through onSelect. Add a second
built-in layout, Markets: a heatmap of the starter's screen sorted by
market cap, largest first, and a quote panel that follows the
selection; layoutsFrom offers it beside the saved layouts unless one
already has that name. Leave DEFAULT_LAYOUT alone. Mock mode. Run npm
test, typecheck, lint and check:leak, and show me the diff.
A good answer adds src/lib/heatmap.ts, the panel and the test, changes src/lib/layout.ts, src/lib/layouts.ts and the container's list of panels, and makes no new kind of request. If the agent pastes whole functions from the app with window in them, ask for the rules again: the port takes the rules and writes them in your idiom.
Check it yourself
- The new tests failed before
heatmap.tsexisted and pass now, beside the starter's own. - In mock mode,
localhost:3000/dashboard?layout=Marketsshows twenty tiles in seven sectors, Technology the largest, with a ticker and a signed change on every tile. - Click Apple's tile: the quote panel switches to Apple, and the address ends in
?symbol=and its ticker. - Ask the agent to delete
refund_1d_pfrom one row offixtures/screener.json, and reload: that tile is grey and reads "—". Put the row back. - The first lines of
src/lib/heatmap.tsname the repository, the file and the licence.
Compare with ours
// src/lib/heatmap.ts: unknown is null, and null is grey with a dash
export function changeOf(row: { refund_1d_p?: number | null }): number | null {
const p = row.refund_1d_p;
return typeof p === "number" && Number.isFinite(p) ? p : null; // never 0
}
export function changeColor(change: number | null): string {
const c = shown(change); // rounded to one decimal, as the tile prints it; -0 is 0
if (c === null) return "#475569";
if (c >= 3) return "#128651";
if (c >= 1) return "#177a44";
if (c > 0) return "#35684f";
if (c === 0) return "#475569";
if (c > -1) return "#a44148";
if (c > -3) return "#b93e3e";
return "#cb4242";
}
The full state at the end of this unit is on the starter's branch ref/build-a-dashboard/u2.
Checkpoint
npm test && npm run typecheck
git status # the files you expect, and no .env file
git add -A
git commit -m "Heatmap panel ported from eodhd-heatmap: squarified tiles, seven colour steps, a dash for unknown"
Then answer this lesson's questions.