JFLib

Charts#

Source: lib/ui/chart.rs

chart -- small charts for dashboards: sparkline, bar chart (grouped or stacked), donut,
legend, and line charts of time series (line_chart over SeriesBuf rings).  Colours: 0 =
the style's chart palette (StyleData::chart_palette) by index.

  sparkline(id, values, color)          area + line, natural 120 x 32 DIP (next_size)
  bar_chart(id, labels, series, stacked) -> hovered column or -1
                                        value grid with labels, one column per label,
                                        natural 360 x 180 DIP; hovering a column shows a
                                        tooltip with its values.  Labels never overlap:
                                        where they do not fit side by side every k-th one
                                        is shown (the first and the last kept; a text
                                        record each), a low plot gets fewer value steps
  donut(id, slices, center, sub)        ring of slices with a centre text, natural 160 DIP
                                        (texts kept inside the hole)
  legend(items)                         colour dot + label per item, wrapping row (an
                                        item wider than the row: its label cut with "…")
  line_chart(id, &LineChart) -> Option<(series, x)>
       series of (x, value) points (LcSeries over a SeriesBuf: label, colour or palette
       index, line or area, LcUnit) over time -- LX_SECS seconds or LX_NANOS nanoseconds,
       ticks 0:05 / 1:00 / 1:30 (h:mm:ss from an hour on, decimals of the second below
       one) -- or plain numbers (LX_NUM).  Natural 480 x 200 DIP for the plot with its
       axes, the legend (legend(), the series' labels) below it; next_size sizes both.
       The y axis: "nice" ticks (1 / 2 / 2.5 / 5 x 10^k) labelled in the first series'
       unit; y_range fixed or automatic per end (NaN: from the visible values and the
       thresholds, out to the next tick).  The x range: x_range, else the data's --
       follow live with x_span (the newest x_span; LineChart::view() is what it shows).
       Grid lines at the ticks; markers (LcMarker: a line or a shaded range with a short
       label in a tag at the top: fault injections) and thresholds (LcThreshold: a dashed
       line with its label in a tag at its right end).  Tags never overlap: a marker's tag
       takes the first of up to three rows with room, else it is left out (the hover card
       still names the markers under the pointer).  Hovering the plot: a crosshair at the
       nearest point, a dot on every series and a card beside it at once (the style's
       tooltip surface) with the x and every series' value there; the return value is the
       series nearest to the pointer and that x.  Keys (the plot takes the focus: Tab, a
       click puts the cursor on the point under the pointer): Left / Right the previous /
       next point of the cursor's series, Up / Down another series, Home / End, Esc ends
       it; crosshair and card show the cursor, the record's label starts with its point.
       Up to two points per pixel column the points themselves are drawn, more as one
       band per series (per column the range of its values, SeriesBuf::columns), so
       100 000 points draw like 500.  No points at all: LineChart::empty in the middle
       ("Keine Daten" in the UI language).  High
       contrast: the scheme's highlight, text and link colours with a dash pattern per
       series (the legend shows line samples), no translucent fills.  The plot keeps its
       left-to-right order in mirrored windows (ltr_begin); the legend is mirrored.
       Records: K_CANVAS id, its label a summary (the name, then per series its last
       value, min. and max. in the view; value: the number of series); K_TEXT per axis
       label, tag and hover card row ("p95: 48 ms").  Flags LC_NO_LEGEND, LC_NO_GRID,
       LC_NO_HOVER.
  SeriesBuf::new(capacity)              the ring a series draws from: push(t, v) (times
                                        never decrease; a value that is not finite is a
                                        gap), clear, len / get / first / last / iter,
                                        lower_bound(t), nearest(t), min_max /
                                        min_max_in(t0, t1), columns(t0, t1, n) (per pixel
                                        column min / max / first / last), ver()
  LcUnit::Num(sfx) / Si(unit) / Secs    how values read: text(v, sep) ("48,2 ms", "1,25k
                                        req/s", "1,5 MB"), ticks(first, step, n, sep)
  lc_nice(lo, hi, steps), lc_time_step(min), lc_time_text(t, dec, hours, sep)
                                        the axis maths: tick steps, clock labels

Area, line and ring are vector paths filled at render time (cmd_custom); their drawing
object is kept per id and replaced only when the data or the size change, so an unchanged
chart does not redraw (dirty.rs hashes the object's identity and version).  A line chart
keeps its statistics, summary and plot per id, keyed by the series' versions and the
view; its crosshair, dots and card are ordinary commands over the plot.
  • struct BarSeries<'a> one series of a bar chart

    • label: &'a str
    • values: &'a [f32]
    • color: u32 -- 0xAARRGGBB, 0 = palette colour of the series index
  • struct DonutSlice<'a> one slice of a donut

    • label: &'a str
    • value: f32
    • color: u32 -- 0xAARRGGBB, 0 = palette colour of the slice index
  • struct SeriesBuf a time series for line_chart: (t, value) points in a ring that keeps the newest capacity (a full buffer drops its oldest point on push). Times never decrease (push refuses an earlier one, so every lookup is a binary search); a value that is not finite (NaN) is a gap where the line breaks. ver() changes with every change: a chart redraws only then.

  • struct SeriesCol one pixel column of a downsampled series (SeriesBuf::columns): the points with a finite value whose time falls into it

    • col: u32 -- the column (0 .. n)
    • n: u32 -- its points with a value
    • min: f64
    • max: f64
    • first: f64 -- the values of its first and its last point
    • last: f64
    • gap: bool -- a gap came before it (after the previous point with a value inside t0 .. t1): the line breaks there; a gap inside a column starts a second entry for it

impl SeriesBuf

  • fn new(capacity: usize) -> SeriesBuf an empty series that keeps the newest capacity points (at least one)

  • fn push(&mut self, t: f64, v: f64) -> bool append a point (t: the time, or any x that never decreases; v NaN: a gap); false (nothing added) when t is not finite or earlier than the last point's

  • fn clear(&mut self) remove every point

  • fn len(&self) -> usize the number of points

  • fn is_empty(&self) -> bool no points

  • fn capacity(&self) -> usize the most points it keeps

  • fn ver(&self) -> u32 changes with every push and clear (equal versions: equal points)

  • fn get(&self, i: usize) -> Option<(f64, f64)> the i-th point, the oldest first

  • fn first(&self) -> Option<(f64, f64)> the oldest point

  • fn last(&self) -> Option<(f64, f64)> the newest point

  • fn iter(&self) -> impl DoubleEndedIterator<Item = (f64, f64)> + ExactSizeIterator + '_ the points, the oldest first

  • fn lower_bound(&self, t: f64) -> usize the index of the first point at t or later (len() when there is none)

  • fn nearest(&self, t: f64) -> Option<usize> the index of the point with a finite value nearest to t (of two as near the earlier)

  • fn min_max(&self) -> Option<(f64, f64)> the smallest and the largest finite value (None without one); looks at every point

  • fn min_max_in(&self, t0: f64, t1: f64) -> Option<(f64, f64)> the smallest and the largest finite value of the points with t0 <= t <= t1

  • fn columns(&self, t0: f64, t1: f64, n: usize) -> Vec<SeriesCol> the points with t0 <= t <= t1 in n columns of equal width (t1 belongs to the last): per column the range of its values, its first and its last value. Columns without a point with a value are left out; the work is one pass over the points, the result at most n entries (plus one per gap)

  • enum LcUnit<'a> how a line chart labels values (axis ticks, the hover card, the summary)

    • Num(&'a str) -- numbers, large ones with k / M / G / T (1,5k), then the suffix after a space ("req/s", "%"; "" none)
    • Si(&'a str) -- SI prefixes on a unit: Si("B") 950 B / 1,5 kB / 20 MB, Si("bit/s") 100 Mbit/s
    • Secs -- seconds shown as ns / µs / ms / s (latencies)

impl LcUnit<'_>

  • fn text(&self, v: f64, sep: u32) -> String v as one value: three significant digits with the scale that fits ("48,2 ms", "1,25k req/s", "12 %", "1,5 MB"); sep = the decimal separator (ui.decimal_sep()); "–" for a value that is not finite

  • fn ticks(&self, first: f64, step: f64, n: u32, sep: u32) -> Vec<String> the labels of the axis ticks first + k * step (k = 0 ..= n): one scale for all (the largest tick's; plain numbers get k / M only from 10 000 on), the decimals the step needs; a zero tick reads "0" with the unit ("0 ms", "0 req/s")

  • fn lc_nice(lo: f64, hi: f64, steps: u32) -> (f64, f64, u32) "nice" ticks over lo ..= hi: (first, step, n) -- the step 1, 2, 2.5 or 5 x 10^k, first and first + n * step the multiples of it at or around lo and hi, n <= max(steps, 1) (at least 1; lo == hi spans |lo|, or 1)

  • fn lc_time_step(min: f64) -> f64 the step of a time axis (s): the smallest of 1 / 2 / 5 x 10^k below a second (down to a nanosecond), 1 2 5 10 15 30 s, 1 2 5 10 15 30 min, 1 2 3 6 12 h, then days (1 / 2 / 5 x 10^k) that is at least min

  • fn lc_time_text(t: f64, dec: u32, hours: bool, sep: u32) -> String a time t (s) as a clock: "0:05", "1:30", "12:00", with hours "1:00:00"; dec decimals of the second (up to 9) after sep (the decimal separator): "0:01,5"; negative with a minus

line chart

/// x axes of a line chart (LineChart::x)
LX_SECS: u8 = 0  // time in seconds: 0:05, 1:00, 1:30
LX_NANOS: u8 = 1  // time in nanoseconds (a simulation clock), labelled the same
LX_NUM: u8 = 2  // plain numbers
/// LineChart::flags
LC_NO_LEGEND: u32 = 1  // no legend below the plot
LC_NO_GRID: u32 = 2  // no grid lines
LC_NO_HOVER: u32 = 4  // no crosshair, card and keyboard cursor (no focus stop)
  • struct LcSeries<'a> one series of a line chart: a label, its points, colour, line or area, unit
    • label: &'a str
    • data: &'a SeriesBuf
    • color: u32 -- 0xAARRGGBB; 0 = the chart palette's colour pal, without one the series' index
    • pal: Option<u8>
    • area: bool -- a translucent fill under the line, down to zero (or to the edge of the plot)
    • unit: LcUnit<'a> -- how its values read (the y axis takes the first series')

impl<'a> LcSeries<'a>

  • const fn new(label: &'a str, data: &'a SeriesBuf) -> LcSeries<'a> a line in the palette colour of its index, plain numbers

  • const fn color(mut self, c: u32) -> LcSeries<'a> the same series in a colour (0xAARRGGBB)

  • const fn pal(mut self, k: u8) -> LcSeries<'a> the same series in the chart palette's colour k (0 ..= 7)

  • const fn area(mut self) -> LcSeries<'a> the same series as an area

  • const fn unit(mut self, u: LcUnit<'a>) -> LcSeries<'a> the same series with a unit

  • struct LcMarker<'a> a marker on the x axis: a line at t0, or the range t0 .. t1 shaded (fault injections)

    • t0: f64
    • t1: f64 -- <= t0: a line
    • label: &'a str -- a short label in a tag at the top of the plot ("" none)
    • color: u32 -- 0xAARRGGBB, 0 = T_TEXT2

impl<'a> LcMarker<'a>

  • const fn line(t: f64, label: &'a str) -> LcMarker<'a> a line at t

  • const fn range(t0: f64, t1: f64, label: &'a str) -> LcMarker<'a> the range t0 .. t1

  • const fn color(mut self, c: u32) -> LcMarker<'a> the same marker in a colour

  • struct LcThreshold<'a> a threshold: a dashed horizontal line at a value with a label at its right end

    • value: f64
    • label: &'a str
    • color: u32 -- 0xAARRGGBB, 0 = T_TEXT2

impl<'a> LcThreshold<'a>

  • const fn new(value: f64, label: &'a str) -> LcThreshold<'a> a threshold at value with its label, in T_TEXT2

  • const fn color(mut self, c: u32) -> LcThreshold<'a> the same threshold in a colour

  • struct LineChart<'a> what ui.line_chart shows: the series and how (see the module header of chart.rs); build it with LineChart::new(&series) and the setters, or change the fields

    • series: &'a [LcSeries<'a>]
    • x: u8 -- the x axis: LX_SECS (default), LX_NANOS or LX_NUM
    • x_range: (f64, f64) -- the visible x range (the points' units); empty (the default) = the data's
    • x_span: f64 -- follow live: without x_range the newest x_span of the data (0 = all of it)
    • y_range: (f64, f64) -- the y range; a NaN end (the default) is automatic: the visible values and the thresholds, out to the next tick. (0.0, f64::NAN): from zero (metrics)
    • markers: &'a [LcMarker<'a>]
    • thresholds: &'a [LcThreshold<'a>]
    • flags: u32 -- LC_*
    • name: &'a str -- the chart's name: the start of its summary (screen readers)
    • empty: &'a str -- the text without points ("" = "Keine Daten" in the UI language)

impl<'a> LineChart<'a>

  • const fn new(series: &'a [LcSeries<'a>]) -> LineChart<'a> the series over time in seconds, both ranges automatic
  • const fn x_axis(mut self, x: u8) -> LineChart<'a> the x axis: LX_SECS, LX_NANOS or LX_NUM
  • const fn x_range(mut self, lo: f64, hi: f64) -> LineChart<'a> a fixed x range (a paused view)
  • const fn follow(mut self, span: f64) -> LineChart<'a> follow live: the newest span of the data
  • const fn y_range(mut self, lo: f64, hi: f64) -> LineChart<'a> the y range (a NaN end: automatic)
  • const fn markers(mut self, m: &'a [LcMarker<'a>]) -> LineChart<'a> markers on the x axis (lines and shaded ranges with tags)
  • const fn thresholds(mut self, t: &'a [LcThreshold<'a>]) -> LineChart<'a> thresholds (dashed lines with tags)
  • const fn flags(mut self, f: u32) -> LineChart<'a> LC_* flags
  • const fn name(mut self, n: &'a str) -> LineChart<'a> the name its summary starts with
  • const fn empty(mut self, s: &'a str) -> LineChart<'a> the text of the empty chart
  • fn view(&self) -> (f64, f64) the x range the chart shows (the points' units): x_range when it is set, else the data's -- with x_span the newest x_span of it (from the first point on while the data is shorter: it grows from the left); without points 0 .. x_span (or one second)

impl Ui

  • fn sparkline(&mut self, id: u32, values: &[f32], color: u32) ui_sparkline(id, values, color): area + line over the values (min .. max scaled)
  • fn bar_chart(&mut self, id: u32, labels: &[&str], series: &[BarSeries], stacked: bool) -> i32 ui_bar_chart(id, labels, series, stacked) -> the hovered column (-1 = none)
  • fn donut(&mut self, id: u32, slices: &[DonutSlice], center: &str, sub: &str) ui_donut(id, slices, center, sub): ring of slices (fractions of their sum) with a centre text (TS_SUBTITLE; TS_BODY_STRONG where that is wider than the hole) and a caption below it (left out where the hole is too low for both)
  • fn legend(&mut self, items: &[(&str, u32)]) ui_legend(items): a colour dot and a label per item (colour 0 = palette by index), laid out in a wrapping row; an item wider than the row has its label cut with "…"
  • fn line_chart(&mut self, id: u32, c: &LineChart) -> Option<(usize, f64)> ui_line_chart(id, chart) -> the hovered series and the x it snapped to: series over time (or plain numbers) with axes, grid, legend, markers, thresholds and a hover card (see the module header of chart.rs)

Source: docs/API.md in the JFLib repository.