Skip to main content

Sparkline

Struct Sparkline 

Source
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>

Source

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.

Source

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.

Source

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.

Source

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§

Source§

impl<'a> Clone for Sparkline<'a>

Source§

fn clone(&self) -> Sparkline<'a>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'a> Copy for Sparkline<'a>

Source§

impl<'a> Debug for Sparkline<'a>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Widget for Sparkline<'_>

Source§

fn render(&self, surface: &mut Surface<'_>)

Draw this widget into surface, filling surface.area(). Read more

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<C> WithAlpha for C
where C: Copy,

§

fn with_alpha_first<A>(self, alpha: A) -> AlphaFirst<A, Self>

Wraps self with alpha, storing alpha before the color in memory (see [AlphaFirst]).
§

fn with_alpha_last<A>(self, alpha: A) -> AlphaLast<A, Self>

Wraps self with alpha, storing alpha after the color in memory (see [AlphaLast]).