pub struct Sparkline<'a> { /* private fields */ }Expand description
A single-row sparkline of samples, scaled to the sample max, using the
eight vertical block glyphs ▁▂▃▄▅▆▇█ by default (see Sparkline::glyphs to pick a
different ramp).
The most recent samples are right-aligned so the graph scrolls left as
new data arrives. By default, bar height and color track each sample’s
fraction of the max via Meter (a green-to-red load ramp). Two ways to override the color:
Sparkline::style draws every bar in one fixed color instead, height only, the right choice
once the color channel would otherwise imply something the data doesn’t mean (e.g. a
frame-time graph, where “tallest bar in view” isn’t the same thing as “bad”:
super::PerfOverlay does this); Sparkline::fill_color instead swaps Meter’s ramp for
a caller-supplied one, e.g. for a bar where high isn’t bad (matching
super::Gauge::fill_color/super::StatBar::fill_color). If both are set, style wins,
since it also carries a background color that a color-only ramp can’t express. Only the first
row of area is drawn.
Unlike super::BoxBorder, super::Gauge, super::StatBar,
super::Table, and super::Button, Sparkline has no theme()/
theme_on() pair: Meter already gives its bars a semantic
green-to-red color, and a fixed Sparkline::style override has no
single Theme role to map onto either.
§Examples
use retroglyph_core::grid::{Grid, Rect};
use retroglyph_ui::widget::{Sparkline, Widget};
use retroglyph_ui::Surface;
let samples = [1.0, 3.0, 2.0, 4.0, 1.5];
let mut grid = Grid::new(10, 1);
let area = Rect::new(0, 0, 10, 1);
Sparkline::new(&samples).render(&mut Surface::new(&mut grid, area, 0));Implementations§
Source§impl<'a> Sparkline<'a>
impl<'a> Sparkline<'a>
Sourcepub const fn new(samples: &'a [f32]) -> Self
pub const fn new(samples: &'a [f32]) -> Self
A sparkline of samples, colored by Meter (green-to-red load ramp) unless overridden
with style or fill_color, drawn with the eighth-block
glyph ramp NINE_LEVELS unless overridden
with glyphs.
Sourcepub const fn style(self, style: Style) -> Self
pub const fn style(self, style: Style) -> Self
Draws every bar in style’s foreground color instead of the default Meter ramp.
Height still tracks each sample’s fraction of the max; only the color stops varying.
Takes priority over Sparkline::fill_color if both are set.
Sourcepub const fn fill_color(self, fill_color: fn(f32) -> Color) -> Self
pub const fn fill_color(self, fill_color: fn(f32) -> Color) -> Self
Overrides each bar’s color from Meter’s default green→yellow→red load ramp to
fill_color, called with the bar’s clamped 0.0..=1.0 fraction of the sample max. Height
still tracks that same fraction.
Matches super::Gauge::fill_color/super::StatBar::fill_color’s shape: for a bar
where high isn’t bad (e.g. a health-derived sparkline, where full should read safe, not
danger), supply a ramp that fits, e.g. |r| Color::lerp(Color::RED, Color::GREEN, r). A
plain fn pointer rather than a boxed closure, so Sparkline stays Copy; a
non-capturing closure like the one above coerces to it automatically.
Ignored if Sparkline::style is also set: style also carries a background color that
a color-only ramp can’t express, so it wins.
Sourcepub const fn glyphs(self, glyphs: &'a [char]) -> Self
pub const fn glyphs(self, glyphs: &'a [char]) -> Self
Overrides the glyph ramp from the default eighth blocks
(NINE_LEVELS, ' '▁▂▃▄▅▆▇█) to glyphs,
indexed from empty (glyphs[0]) to full (glyphs[glyphs.len() - 1]).
Six of the eight eighth-block glyphs are outside CP437: a pixel backend that resolves
glyphs through a CP437 table draws its fallback solid block for all of them, and a
tileset/sprite backend can’t tint an arbitrary glyph by Style::fg. On either backend,
pass SHADE_LEVELS (' '░▒▓█, all within
CP437) for a ramp that still renders five distinguishable levels instead of collapsing to
one block. glyphs must be non-empty; an empty slice falls back to a single space.
Trait Implementations§
impl<'a> Copy for Sparkline<'a>
Auto Trait Implementations§
impl<'a> Freeze for Sparkline<'a>
impl<'a> RefUnwindSafe for Sparkline<'a>
impl<'a> Send for Sparkline<'a>
impl<'a> Sync for Sparkline<'a>
impl<'a> Unpin for Sparkline<'a>
impl<'a> UnsafeUnpin for Sparkline<'a>
impl<'a> UnwindSafe for Sparkline<'a>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<C> WithAlpha for Cwhere
C: Copy,
impl<C> WithAlpha for Cwhere
C: Copy,
§fn with_alpha_first<A>(self, alpha: A) -> AlphaFirst<A, Self>
fn with_alpha_first<A>(self, alpha: A) -> AlphaFirst<A, Self>
self with alpha, storing alpha before the color in memory
(see [AlphaFirst]).§fn with_alpha_last<A>(self, alpha: A) -> AlphaLast<A, Self>
fn with_alpha_last<A>(self, alpha: A) -> AlphaLast<A, Self>
self with alpha, storing alpha after the color in memory
(see [AlphaLast]).