Expand description
Charts over your own data, drawn onto a densa_draw::Canvas: lines, candlesticks and
bars, with gridlines and tick labels.
Domain-agnostic: an application implements Points (a line or bars) or Candles
over its data in ascending x, picks the visible part with an Axis for each
direction, and draws onto a Plot (an area of the canvas and its two axes). Time axes
count seconds since the epoch and label ticks as times of day or dates (UTC); category
axes place named things (a bar chart’s bars) at 0, 1, 2, ... and label them by name.
A chart widget asks its data through ChartModel: panes on one time axis, each giving
its vertical axis for what shows and drawing its series.
A line is drawn as one vertical span per pixel column, shaded at its ends, so a million
points cost about as much as the plot is wide and every high and low still shows (M4
aggregation). No GPU dependency: hand the canvas’s instances to densa-render (or any
other backend) to show them.
Painting allocates nothing once the label characters are cached.
use densa_chart::{paint_line, points_within, y_extent, Axis, LineStyle, Plot, Rect};
use densa_draw::{Canvas, InstanceBuffer};
use densa_text::{AtlasConfig, GlyphCache};
// A day of readings, one a second: (seconds since the epoch, value).
let readings: Vec<(f64, f64)> = (0..86_400)
.map(|s| (1_792_368_000.0 + f64::from(s), (f64::from(s) / 3_600.0).sin()))
.collect();
// The last hour, scaled to what it holds.
let x = Axis::time(1_792_450_800.0, 1_792_454_400.0);
let (low, high) = y_extent(&readings[..], points_within(&readings[..], &x)).unwrap();
let plot = Plot::new(Rect::new(0.0, 0.0, 800.0, 300.0), x, Axis::number(low, high).padded(0.05));
let mut cache = GlyphCache::new(AtlasConfig::DEFAULT);
let mut out = InstanceBuffer::with_capacity(4_096);
let mut canvas = Canvas::begin(&mut cache, &mut out);
paint_line(&mut canvas, &plot, &readings[..], LineStyle::new(densa_chart::Color::WHITE));
assert!(out.len() <= 3 * 800, "at most a few rectangles per pixel column");Structs§
- Axis
- The values one axis of a plot spans,
startbelowend. - BarStyle
- How bars are drawn:
BarStyle::new(),with_bodyfor wider or narrower ones. - Candle
- One candlestick: a period’s first, highest, lowest and last values.
- Candle
Style - How candlesticks are drawn:
CandleStyle::new(up, down). - Chart
Painter - Draws what is around a chart’s series: gridlines at the ticks, the axis edges and the
tick labels, right of the plot for
yand under it forx, and a crosshair over it. - Chart
Palette - Chart colors:
ChartPalette::newis the dark default; change its fields for others. - Color
- A color in linear RGB with straight (not premultiplied) alpha: the space densa blends in.
- Line
Style - How a line is drawn:
LineStyle::new(color),with_widthfor a thicker one. - Plot
- Where a chart draws and what it shows: an area of the canvas, with
xrunning left to right across it andybottom to top. - Point
- A position in pixels, y down.
- Rect
- An axis-aligned rectangle in pixels, y down, stored as its four edges.
- Ticks
- The ticks of an
Axis, smallest first: whole multiples ofstep.
Enums§
- Axis
Kind - What an axis’s values are, which decides where its ticks go and how they read.
Traits§
- Candles
- A series of candlesticks in ascending
x(each candle’s period, by its start or its middle: the chart centers a candle on itsx). - Chart
Model - The data of a chart widget (
densa_widgets::Chart): panes stacked top to bottom on one horizontal axis, each drawing its own series (candles over a line, volume bars below). - Points
- A series of points, in ascending
x(a chart finds the visible ones by binary search): a line, or the bars of a bar series. Aythat is not finite is a gap.
Functions§
- candle_
extent - The lowest low and the highest high among
rangeofcandles(the part of it within them; finite candles only), orNoneif there are none. - candles_
within - The indices of the candles whose
xlies withinx(ends included). - paint_
bars - Draws
barsacrossplotas columns from zero to eachy, clipped to its area;intervalis the distance between two bars inxunits, andcolorgives each bar’s color by index (to color volume by its candle’s direction, say). - paint_
candles - Draws
candlesacrossplot, clipped to its area;intervalis the distance between two candles inxunits (60 for one-minute candles on a time axis), which sets their width. - paint_
line - Draws
pointsas a line acrossplot, clipped to its area. - points_
within - The indices whose
xlies withinx(ends included), found by binary search. - y_
extent - The lowest and highest finite
yamongrangeofpoints(the part of it within them), orNoneif none is finite.