[ Widgets ]
Sparkline
koto::Sparkline renders a sequence of uint64_t values as a compact single-row vertical bar chart using 8 Unicode block characters (Header: koto/sparkline.hpp).
Quick example
Section titled “Quick example”std::vector<uint64_t> values = {1, 3, 2, 5, 8, 6, 7, 4, 9, 10};Sparkline() .data(values) .fg(Color::Cyan) .draw(frame, spark_area); ▃▂▅▇▅▆▄██Behavior
Section titled “Behavior”- Bottom-row drawing and upper-row clearing: In a multi-row
area,Sparkline::draw()renders bars on the bottom rowtarget_row = area.bottom() - 1and clears all rows abovetarget_row(area.row .. target_row - 1) withCell{U' ', m_fg, m_bg}(src/sparkline.cpp:60-69). - Auto-scaling vs. fixed ceiling: If
m_max == 0(the default),effective_maxis computed as the maximum element inm_data(clamped to at least1if all elements are0). Otherwiseeffective_max = m_max(src/sparkline.cpp:80-88). - Windowing and right alignment: If
m_data.size() > area.width, only the lastarea.widthelements are displayed (start_idx = num_points - available_cols). Ifm_data.size() < area.widthandm_align_right == true(the default), leading columns on the left are filled with spaces so the data hugs the right edge (src/sparkline.cpp:93-106). - 8-level quantization: For each point
val:- If
val == 0, the cell is a spaceU' '. - If
val > 0,bar_level = std::min<uint64_t>(7, (val * 8) / effective_max)indexes intoSPARKLINE_BARS = {U'▁', U'▂', U'▃', U'▄', U'▅', U'▆', U'▇', U'█'}(src/sparkline.cpp:8-10,src/sparkline.cpp:121-130).
- If
Methods
Section titled “Methods”| Method | Description | Default |
|---|---|---|
Sparkline() | Constructs an empty Sparkline. | m_max = 0, m_align_right = true, m_fg = Color::Cyan, m_bg = COLOR_DEFAULT |
data(std::span<const uint64_t> values) | Binds a non-owning view over values and clears m_owned_data. | Empty span |
data(const std::vector<uint64_t>& values) | Copies values into m_owned_data and points m_data at the copy. | Empty vector |
max(uint64_t max_value) | Sets a fixed scaling ceiling (0 enables auto-scaling to the maximum element). | 0 |
align_right(bool right = true) | When true, right-aligns shorter data series; when false, left-aligns them. | true |
fg(Color fg) | Sets the foreground color of the sparkline bars. | Color::Cyan |
bg(Color bg) | Sets the background color of the sparkline cells. | COLOR_DEFAULT |
draw(Frame& frame, Area area) | Draws the sparkline into frame within area. | None |
Gotchas
Section titled “Gotchas”data(std::span<const uint64_t>)vs.data(const std::vector<uint64_t>&): Passing astd::vector<uint64_t>directly matches theconst std::vector<uint64_t>&overload, which copies the vector intom_owned_data(src/sparkline.cpp:21-26). To avoid copying astd::vector<uint64_t> vecon every frame, passstd::span<const uint64_t>(vec).- Single-row bar height: Even if
area.height > 1,Sparklineonly draws 1-row-tall bars onarea.bottom() - 1and fills the rows above with blank spaces.