chartKit.core: the plain maths and text helpers every chart kind uses. Number
formatting, "nice" axis ticks, min/max decimation that keeps spikes, colour mixing,
SVG text escaping and folded-stack parsing. No UI and no state: safe to call anywhere.
local Core = require("chartKit.core")
local ys, idx = Core.decimate(frameTimes, 600) -- 2 points per bucket, min and max
local ticks = Core.niceTicks(0, 37.2, 5) -- { 0, 10, 20, 30, 40 }
esc(s: any) → string
Escape text for SVG markup (& < > " ').
n(x: number) → string
A coordinate as short text: one decimal, "-0" and ".0" dropped.
| arg | type | description |
|---|
| x | number | |
trim(s: string) → string
| arg | type | description |
|---|
| s | string | |
fmt(v: number?, unit: string?) → string
A value for people to read: 1234567 -> "1.2M", 12345 -> "12.3k", 16.667 -> "16.7",
0.256 -> "0.26". `unit` is appended with a space ("16.7 ms"); "%" is appended directly.
| arg | type | description |
|---|
| v | number? | |
| unit | string? | |
niceTicks(lo: number, hi: number, count: number?) → void
Rounded axis ticks covering lo..hi with about `count` steps of 1, 2, 2.5 or 5 x 10^n.
Returns (ticks, niceLo, niceHi, step).
| arg | type | description |
|---|
| lo | number | |
| hi | number | |
| count | number? | |
decimate(values: { number }, buckets: number) → void
Reduce `values` to at most 2 x `buckets` points that keep every bucket's minimum AND
maximum, in the order they occur, so a single-frame spike never disappears. Returns
(ys, idx): the kept values and each one's index in `values`. NaN values are skipped.
Input no longer than 2 x buckets comes back as a copy.
| arg | type | description |
|---|
| values | { number } | |
| buckets | number | |
range(values: { number }) → void
Min and max of a list, skipping NaN. Returns (min, max) or (nil, nil) when empty.
| arg | type | description |
|---|
| values | { number } | |
quickPercentile(values: { number }, p: number) → number
An approximate percentile (0..100) from at most ~4000 evenly spaced samples, so it
stays fast on 50,000+ values.
| arg | type | description |
|---|
| values | { number } | |
| p | number | |
hex2(s: string) → number
| arg | type | description |
|---|
| s | string | |
rgba(c: string) → void
Parse "#rgb", "#rrggbb" or "rgba(r,g,b,a)" to (r, g, b, a) with r,g,b 0..255, a 0..1.
| arg | type | description |
|---|
| c | string | |
clamp255(x: number) → number
| arg | type | description |
|---|
| x | number | |
hex(r: number, g: number, b: number) → string
"#rrggbb" for r,g,b 0..255.
| arg | type | description |
|---|
| r | number | |
| g | number | |
| b | number | |
mix(a: string, b: string, t: number) → string
Mix two colours: t = 0 gives `a`, 1 gives `b`. Alpha is dropped ("#rrggbb").
| arg | type | description |
|---|
| a | string | |
| b | string | |
| t | number | |
alpha(c: string, a: number) → string
A colour at opacity `a` as "rgba(r,g,b,a)".
| arg | type | description |
|---|
| c | string | |
| a | number | |
luma(c: string) → number
Perceived lightness 0..1 of a colour (to pick dark or light text on top of it).
| arg | type | description |
|---|
| c | string | |
hash01(s: string) → number
A stable 0..1 number from a string (flame-graph colours stay put between redraws).
| arg | type | description |
|---|
| s | string | |
textWidth(text: string, size: number, mono: boolean?) → number
Rough text width in pixels for `text` at `size` (average glyph 0.56 em; `mono` 0.6).
| arg | type | description |
|---|
| text | string | |
| size | number | |
| mono | boolean? | |
fit(text: string, width: number, size: number, mono: boolean?) → string
Cut `text` to fit `width` pixels at `size`, ending with "…" when cut. "" when not even
two characters fit.
| arg | type | description |
|---|
| text | string | |
| width | number | |
| size | number | |
| mono | boolean? | |
folded(text: string) → FlameNode
Parse folded stacks ("main;update;physics 123" per line, the format flamegraph tools
write) into a tree { name = "all", value, children = { ... } }. Children are sorted by
value, biggest first. Blank and malformed lines are skipped.
| arg | type | description |
|---|
| text | string | |
normalizeTree(node: any) → any
Fill in missing `value`s of a flame tree from its children (a node's value is at least
the sum of its children) and return the root.
| arg | type | description |
|---|
| node | any | |