Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Charts

A chart draws series of values over a period on a screen: as lines, bars or stacked bars, with a period bar above and a legend that hides and shows series.

Draw a chart

use chrono::Utc;
use laterite_admin::axum::extract::Query;
use laterite_admin::axum::response::Response;
use laterite_admin::axum::Extension;
use laterite_admin::chart::{Chart, ChartQuery, Kind, Period, Series, CHART_ASSETS};
use laterite_admin::{html::Markup, PageHead, Shell};
use laterite_core::t;

async fn orders(
    Query(query): Query<ChartQuery>,
    Extension(shell): Extension<Shell>,
) -> Response {
    let period = Period::from_query(&query, Utc::now().date_naive());
    // (day, value) rows for period.from()..=period.to().
    let web = acme::orders::by_day("web", period.from(), period.to()).await;
    let shop = acme::orders::by_day("shop", period.from(), period.to()).await;

    let chart = Chart::new("orders", period.clone())
        .title(t!("Orders"))
        .kind(Kind::StackedBar)
        .series(Series::daily("web", t!("Website"), &period, web))
        .series(Series::daily("shop", t!("Shop"), &period, shop));

    let mut body = period.bar(&shell, &query).into_string();
    body.push_str(chart.render(&shell, &query).as_str());
    shell.page_with(
        PageHead::new(t!("Orders")).assets(CHART_ASSETS),
        Markup::raw_unchecked(body),
    )
}

Period

Period::from_query(&query, today) reads these query parameters.

ParameterValuesResult
range7d, 30d, 90dThe last 7, 30 or 90 days. 30d when absent.
rangemonth, last_monthThis calendar month to today; the whole of last month.
range12mThe last 12 calendar months, a point a month.
rangecustomfrom to to, both YYYY-MM-DD, at most 1830 days.
byday, week, monthThe interval. day up to 370 days.
MethodReturns
from(), to()The first and last day.
interval()Interval::Day up to 92 days, Week up to 370, else Month, unless by set it.
points()The first day of each point.
previous()The period of the same length ending the day before.
query()The period as query parameters, for a link.
bar(&shell, &query)The period bar: the presets and a from and to date.

Series

// Dated rows: summed into the period's points, a missing point is zero.
Series::daily("web", t!("Website"), &period, vec![(day, 12.0)]);
// One value a point, in order.
Series::new("web", t!("Website"), vec![12.0, 9.0, 14.0]);

Chart

MethodDefaultEffect
Chart::new(id, period)id names the chart on the page: letters, digits, -, _.
.kind(Kind)Kind::LineLine, Bar or StackedBar.
.format(Format)Format::NumberNumber, or Money("EUR".into()) for EUR 1,234.50.
.title(text)noneThe heading above the legend.
.series(series)Adds a series; its colour follows its place.
.render(&shell, &query)The chart, showing the series show_<id> names.

Figures

use laterite_admin::chart::{stats, Format, Stat};

let row = stats(&shell, &[
    Stat::new(t!("Orders"), Format::Number.text(1284.0)).note(t!("In this period")),
    Stat::new(t!("Revenue"), Format::Money("EUR".into()).text(9120.5)),
]);

Colours

A skin sets --chart-1 to --chart-6.

:root { --chart-1: #0b6e4f; --chart-2: #c8553d; }