librarylibs/Plot/src/Plot.xtl
Plot: text charts -- bars, sparklines, histograms, scatter plots on a character grid -- and line charts as pictures, with axes, labels and several lines in one chart. Import it with an alias of your choice: "p:" u_se< "Plot". Put libs/Plot/src on XETAL_PATH ("just path"); the reference is libs/Plot/docs. Names with l: are exported; those under h: are private to this file.
A text chart is a character matrix: it prints as lines in a terminal, a notebook or the live demo. A picture (l_ine!) is an SVG document shown with []S_HOW: numbered files from the command line, a pane in the browser.
Text charts
ʰs̲cale : (Num a, Num b) => a -> b -> Int
v scaled to 0..m, rounded (all 0 when v is all 0).
ʰs̲cale ← { m v → top ← 'm̲ax r̲/ a̲bs f̲loat v top = 0.0 ? (s̲hape v) r̲eshape 0 f̲loor 0.5 + (f̲loat m) × (f̲loat v) ÷ top }
ˡb̲ars : Num a => Box Char -> a -> Char
labels b_ars values: a horizontal bar per value, labeled, the longest 40 wide, each followed by its value (values >= 0).
ˡb̲ars ← { labels v → w ← 'm̲ax r̲/ '{ t̲ally d̲isclose ⍵ } e̲ach labels n ← width ʰs̲cale v ᵗm̲ix '{ i → label ← w ᵗp̲adRight d̲isclose i s̲elect labels bar ← (f̲irst i s̲elect n) r̲eshape "#" label c̲at " |" c̲at bar c̲at " " c̲at f̲ormat f̲irst i s̲elect v } m̲ap r̲ange t̲ally v }
ˡs̲park : Num a => a -> Char
s_park v: v as a sparkline, one block character per value, its height from the least (lowest) to the greatest (highest).
ˡs̲park ← { v → lo ← 'm̲in r̲/ f̲loat v hi ← 'm̲ax r̲/ f̲loat v hi = lo ? (t̲ally v) r̲eshape 4 s̲elect blocks (1 + f̲loor 0.5 + 7.0 × ((f̲loat v) − lo) ÷ hi − lo) s̲elect blocks }
ˡh̲istogram : Num a => Int -> a -> Char
n h_istogram v: v counted into n bins of equal width from its least to its greatest, drawn as bars labeled by each bin's range.
ˡh̲istogram ← { n v → lo ← 'm̲in r̲/ f̲loat v hi ← 'm̲ax r̲/ f̲loat v step ← (hi − lo) ÷ f̲loat n bin ← 1 + (n − 1) m̲in f̲loor ((f̲loat v) − lo) ÷ step m̲ax 0.000000000001 counts ← '{ t̲ally w̲here bin = ⍵ } e̲ach r̲ange n edges ← lo + step × f̲loat o̲ffsets n + 1 labels ← '{ i → (d̲isclose i s̲elect 2 ᶠf̲ixed edges) c̲at "-" c̲at d̲isclose (i + 1) s̲elect 2 ᶠf̲ixed edges } m̲ap r̲ange n labels ˡb̲ars counts }
ˡs̲catter : Num a => Int -> a -> Char
(w c_at h) s_catter xy: the points xy (2 rows, x over y, as []P_ATH takes them) marked * on a character grid w wide and h high, y up.
ˡs̲catter ← { size xy → w ← f̲irst size h ← f̲irst 1 d̲rop size x ← f̲loat f̲irst xy y ← f̲loat f̲irst 1 d̲rop xy col ← 1 + f̲loor 0.5 + (f̲loat w − 1) × (x − 'm̲in r̲/ x) ÷ 1.0 m̲ax ('m̲ax r̲/ x) − 'm̲in r̲/ x row ← h − f̲loor 0.5 + (f̲loat h − 1) × (y − 'm̲in r̲/ y) ÷ 1.0 m̲ax ('m̲ax r̲/ y) − 'm̲in r̲/ y at ← (col − 1) + w × row − 1 ⍝ offsets into the grid marks ← (o̲ffsets h × w) m̲ember? at (h c̲at w) r̲eshape (1 + marks) s̲elect " *" }
Line charts
ˡl̲ine! : Num a => a -> Char
l_ine! v: v as a line chart with axes, a picture shown with []S_HOW; gives the picture's SVG text. The vertical axis spans the values (however small their range), the horizontal one the positions 1 to t_ally v; a single value is drawn as a point.
ˡl̲ine! ← { v → ⎕S̲HOW ("" "" "") ʰc̲hart 1 r̲eshape e̲nclose f̲loat r̲avel v }
ˡl̲ines! : Num a => Box Char -> Box a -> Char
names l_ines! series: several lines in one chart, one per boxed vector of series (each its own length), colored in turn and named in a legend by names (a boxed text per series).
ˡl̲ines! ← { names series → ⎕S̲HOW ("" "" "" c̲at names) ʰc̲hart '{ x → f̲loat r̲avel d̲isclose x } m̲ap series }
ˡc̲hart! : Num a => Box Char -> Box a -> Char
labels c_hart! series: as l_ines!, with a title and the names of the axes: labels is the title, the horizontal axis's name, the vertical axis's name, then a name per series ("" for none).
ˡc̲hart! ← { labels series → ⎕S̲HOW labels ʰc̲hart '{ x → f̲loat r̲avel d̲isclose x } m̲ap series }
Chart drawing (private)
ʰcolors : Box Char
The line colors, in turn (blue, red, green, amber, violet, teal).
ʰcolors ← "#2563eb" "#dc2626" "#16a34a" "#d97706" "#7c3aed" "#0d9488"
ʰj̲oined : Box Float -> Float
Boxed numbers joined into one vector.
ʰj̲oined ← { b → d̲isclose '{ x y → e̲nclose (d̲isclose x) c̲at d̲isclose y } r̲/ b }
ʰc̲hart : Box Char -> Box Float -> Char
labels c_hart series: the picture's SVG text.
ʰc̲hart ← { labels series → all ← ʰj̲oined series lo ← 'm̲in r̲/ all hi ← 'm̲ax r̲/ all pad ← lo ʰp̲ad hi box ← (lo − pad) c̲at hi + pad n ← 'm̲ax r̲/ '{ x → t̲ally d̲isclose x } e̲ach series lines ← ʰj̲oinText '{ i → (i ʰc̲olor ʰcolors) ʰs̲eries (e̲nclose f̲loat n) c̲at (e̲nclose box) c̲at i s̲elect series } m̲ap r̲ange t̲ally series body ← (n ʰf̲rame box) c̲at lines c̲at labels ʰt̲itles series (ʰW c̲at ʰH) ᵛp̲icture body }
ʰp̲ad : Float -> Float -> Float
How far past its least and greatest values the vertical axis goes: 0, or for a flat series a tenth of its size (1 when it is 0).
ʰp̲ad ← { lo hi → hi > lo ? 0.0 lo = 0.0 ? 1.0 0.1 × a̲bs lo }
ʰj̲oinText : Box Char -> Char
Boxed texts joined into one text (none is the empty text).
ʰj̲oinText ← { b → 0 = t̲ally b ? "" d̲isclose '{ x y → e̲nclose (d̲isclose x) c̲at d̲isclose y } r̲/ b }
ʰc̲olor : Int -> Box Char -> Char
The color of series i (1, 2, ...), the colors in turn.
ʰc̲olor ← { i c → d̲isclose (1 + (i − 1) m̲od t̲ally c) s̲elect c }
ʰx̲at : Int -> Int -> Float
The x of positions p (0, 1, ...) of n: spread across the plot area, one position in its middle.
ʰx̲at ← { n p → n = 1 ? (s̲hape p) r̲eshape (ʰL + ʰR) ÷ 2.0 ʰr̲ound ʰL + (ʰR − ʰL) × (f̲loat p) ÷ f̲loat n − 1 }
ʰr̲ound : Float -> Float
x rounded to a whole number, still a Float.
ʰr̲ound ← { x → f̲loat f̲loor 0.5 + x }
ʰy̲at : Float -> Float -> Float
The y of values v in the vertical range box (least, greatest).
ʰy̲at ← { box v → lo ← f̲irst box hi ← f̲irst 1 d̲rop box ʰr̲ound ʰB − (ʰB − ʰT) × ((f̲loat v) − lo) ÷ hi − lo }
ʰs̲eries : Char -> Box Float -> Char
One series as a polyline (and a point when it has one value), in its color: color s_eries (n; box; values), boxed (n a Float).
ʰs̲eries ← { color rest → n ← f̲loor f̲irst d̲isclose f̲irst rest box ← d̲isclose f̲irst 1 d̲rop rest v ← d̲isclose f̲irst 2 d̲rop rest xs ← n ʰx̲at o̲ffsets t̲ally v ys ← box ʰy̲at v line ← ((ᵛf̲ill "none") c̲at color ᵛs̲troke 2) ᵛp̲olyline (2 c̲at t̲ally v) r̲eshape xs c̲at ys 1 < t̲ally v ? line line c̲at "<circle" c̲at ("cx" ᵛa̲ttr f̲ormat f̲irst xs) c̲at ("cy" ᵛa̲ttr f̲ormat f̲irst ys) c̲at " r=\"3\"" c̲at (ᵛf̲ill color) c̲at "/>" }
ʰsmall : Char
The text attributes of tick labels and names.
ʰsmall ← " font-family=\"sans-serif\" font-size=\"11\" fill=\"#475569\""
ʰf̲rame : Int -> Float -> Char
The axes, the grid lines and the tick labels: three values on the vertical axis (least, middle, greatest), the first and last positions on the horizontal one.
ʰf̲rame ← { n box → lo ← f̲irst box hi ← f̲irst 1 d̲rop box ticks ← lo c̲at ((lo + hi) ÷ 2.0) c̲at hi ys ← box ʰy̲at ticks d ← 0 m̲ax 8 m̲in 1 − f̲loor (l̲og (hi − lo) ÷ 2.0) ÷ l̲og 10 ⍝ decimals enough to tell the ticks apart texts ← d ᶠf̲ixed ticks grid ← ʰj̲oinText '{ y → ((ᵛf̲ill "none") c̲at "#e2e8f0" ᵛs̲troke 1) ᵛp̲olyline 2 2 r̲eshape ʰL c̲at ʰR c̲at y c̲at y } m̲ap ys left ← ʰj̲oinText '{ k → ((ᵛa̲t (ʰL − 8.0) c̲at 4.0 + k s̲elect ys) c̲at ʰsmall c̲at "text-anchor" ᵛa̲ttr "end") ᵛt̲ext d̲isclose k s̲elect texts } m̲ap r̲ange 3 last ← 1 m̲ax n at ← n ʰx̲at (1 + n > 1) r̲eshape 0 c̲at n − 1 marks ← (1 + n > 1) r̲eshape 1 c̲at last bottom ← ʰj̲oinText '{ k → ((ᵛa̲t (k s̲elect at) c̲at ʰB + 18.0) c̲at ʰsmall c̲at "text-anchor" ᵛa̲ttr "middle") ᵛt̲ext f̲ormat k s̲elect marks } m̲ap r̲ange t̲ally at axes ← ((ᵛf̲ill "none") c̲at "#64748b" ᵛs̲troke 1) ᵛp̲olyline 2 3 r̲eshape ʰL c̲at ʰL c̲at ʰR c̲at ʰT c̲at ʰB c̲at ʰB back ← "<rect" c̲at ("width" ᵛa̲ttr f̲ormat ʰW) c̲at ("height" ᵛa̲ttr f̲ormat ʰH) c̲at (ᵛf̲ill "#ffffff") c̲at "/>" back c̲at grid c̲at axes c̲at left c̲at bottom }
ʰt̲itles : Box Char -> Box Float -> Char
The title, the axes' names and the legend from labels (title, horizontal axis, vertical axis, then a name per series); empty ones are left out.
ʰt̲itles ← { labels series → n̲ame ← { k → 0 = t̲ally d̲isclose k s̲elect labels ? ""◆ d̲isclose k s̲elect labels } title ← ((ᵛa̲t ʰL c̲at 24.0) c̲at " font-family=\"sans-serif\" font-size=\"14\" font-weight=\"bold\" fill=\"#0f172a\"") ᵛt̲ext n̲ame 1 across ← ((ᵛa̲t ((ʰL + ʰR) ÷ 2.0) c̲at ʰB + 38.0) c̲at ʰsmall c̲at "text-anchor" ᵛa̲ttr "middle") ᵛt̲ext n̲ame 2 up ← (ʰsmall c̲at ("text-anchor" ᵛa̲ttr "middle") c̲at "transform" ᵛa̲ttr "translate(18 " c̲at (f̲ormat (ʰT + ʰB) ÷ 2.0) c̲at ") rotate(-90)") ᵛt̲ext n̲ame 3 names ← 3 d̲rop labels keys ← ʰj̲oinText '{ i → ((i ʰc̲olor ʰcolors) ᵛs̲pan "■ ") c̲at ("#334155" ᵛs̲pan d̲isclose i s̲elect names) c̲at " " } m̲ap r̲ange t̲ally names legend ← ((ᵛa̲t ʰR c̲at 24.0) c̲at ʰsmall c̲at "text-anchor" ᵛa̲ttr "end") ᵛm̲arkup keys ((n̲ame 1) ʰk̲ept title) c̲at ((n̲ame 2) ʰk̲ept across) c̲at ((n̲ame 3) ʰk̲ept up) c̲at keys ʰk̲ept legend }