code wiki / _hdl_build / nx_viz_axis.nx
nx_viz_axis.nx source
↩ module page · 64 lines · 3993 B
1// nx_viz_axis.nx -- the AXIS layer of the sovereign Nishi viz library (the d3-axis equivalent, bits-up).
2// Composes scales (vs_ticks for tick values + vs_linear for value->pixel) + shapes' serializer (vsh_n/vsh_lit)
3// to emit a full SVG axis: baseline + tick marks + numeric labels. va_ticks is the KAT-able numeric core
4// (tick values + their pixel positions); va_axis_bottom is the render. Closes axis-ticks. license_tier: ORIGINAL
5import "nx_viz_scale.nx"
6import "nx_viz_shape.nx"
7import "nx_syscalls.nx"
8
9// numeric core: tick VALUES (vs_ticks) + their PIXEL positions (vs_linear). returns count.
10func va_ticks(d0: i64, d1: i64, target: i64, px0: i64, px1: i64, vals_out: *i64, px_out: *i64) -> i64 {
11 let cnt: i64 = vs_ticks(d0, d1, target, vals_out)
12 var i: i64 = 0
13 while i < cnt {
14 px_out[i] = vs_linear(d0, d1, px0, px1, vals_out[i])
15 i = i + 1
16 }
17 return cnt
18}
19// render: bottom axis (baseline + downward tick marks + labels) -> out (null-terminated). returns length.
20func va_axis_bottom(d0: i64, d1: i64, px0: i64, px1: i64, target: i64, axis_y: i64, out: *u8) -> i64 {
21 let vals: *i64 = sys_mmap(256) as *i64
22 let pxs: *i64 = sys_mmap(256) as *i64
23 let cnt: i64 = va_ticks(d0, d1, target, px0, px1, vals, pxs)
24 var o: i64 = vsh_lit(out, 0, "<line x1=\"" as *u8)
25 o = vsh_n(out, o, px0); o = vsh_lit(out, o, "\" y1=\"" as *u8); o = vsh_n(out, o, axis_y)
26 o = vsh_lit(out, o, "\" x2=\"" as *u8); o = vsh_n(out, o, px1); o = vsh_lit(out, o, "\" y2=\"" as *u8); o = vsh_n(out, o, axis_y)
27 o = vsh_lit(out, o, "\" stroke=\"#888\"/>" as *u8)
28 var i: i64 = 0
29 while i < cnt {
30 o = vsh_lit(out, o, "<line x1=\"" as *u8); o = vsh_n(out, o, pxs[i]); o = vsh_lit(out, o, "\" y1=\"" as *u8); o = vsh_n(out, o, axis_y)
31 o = vsh_lit(out, o, "\" x2=\"" as *u8); o = vsh_n(out, o, pxs[i]); o = vsh_lit(out, o, "\" y2=\"" as *u8); o = vsh_n(out, o, axis_y + 6)
32 o = vsh_lit(out, o, "\" stroke=\"#888\"/>" as *u8)
33 o = vsh_lit(out, o, "<text x=\"" as *u8); o = vsh_n(out, o, pxs[i]); o = vsh_lit(out, o, "\" y=\"" as *u8); o = vsh_n(out, o, axis_y + 20)
34 o = vsh_lit(out, o, "\" text-anchor=\"middle\" font-size=\"11\" fill=\"#aaa\">" as *u8); o = vsh_n(out, o, vals[i]); o = vsh_lit(out, o, "</text>" as *u8)
35 i = i + 1
36 }
37 out[o] = 0 as u8
38 return o
39}
40// render: left axis (vertical line + leftward tick marks + right-aligned labels) -> out. returns length.
41// py0 maps d0 (bottom, larger y in SVG), py1 maps d1 (top). (d3.axisLeft)
42func va_axis_left(d0: i64, d1: i64, py0: i64, py1: i64, target: i64, axis_x: i64, out: *u8) -> i64 {
43 let vals: *i64 = sys_mmap(256) as *i64
44 let pys: *i64 = sys_mmap(256) as *i64
45 let cnt: i64 = vs_ticks(d0, d1, target, vals)
46 var i: i64 = 0
47 while i < cnt { pys[i] = vs_linear(d0, d1, py0, py1, vals[i]); i = i + 1 }
48 var o: i64 = vsh_lit(out, 0, "<line x1=\"" as *u8)
49 o = vsh_n(out, o, axis_x); o = vsh_lit(out, o, "\" y1=\"" as *u8); o = vsh_n(out, o, py0)
50 o = vsh_lit(out, o, "\" x2=\"" as *u8); o = vsh_n(out, o, axis_x); o = vsh_lit(out, o, "\" y2=\"" as *u8); o = vsh_n(out, o, py1)
51 o = vsh_lit(out, o, "\" stroke=\"#888\"/>" as *u8)
52 i = 0
53 while i < cnt {
54 o = vsh_lit(out, o, "<line x1=\"" as *u8); o = vsh_n(out, o, axis_x - 6); o = vsh_lit(out, o, "\" y1=\"" as *u8); o = vsh_n(out, o, pys[i])
55 o = vsh_lit(out, o, "\" x2=\"" as *u8); o = vsh_n(out, o, axis_x); o = vsh_lit(out, o, "\" y2=\"" as *u8); o = vsh_n(out, o, pys[i])
56 o = vsh_lit(out, o, "\" stroke=\"#888\"/>" as *u8)
57 o = vsh_lit(out, o, "<text x=\"" as *u8); o = vsh_n(out, o, axis_x - 10); o = vsh_lit(out, o, "\" y=\"" as *u8); o = vsh_n(out, o, pys[i] + 4)
58 o = vsh_lit(out, o, "\" text-anchor=\"end\" font-size=\"11\" fill=\"#aaa\">" as *u8); o = vsh_n(out, o, vals[i]); o = vsh_lit(out, o, "</text>" as *u8)
59 i = i + 1
60 }
61 out[o] = 0 as u8
62 return o
63}
64func main() -> i64 { return 0 }