Lines
98.04 %
Functions
69.23 %
Branches
100 %
//! A chart spec in the format a client asks for.
use base64::Engine;
use base64::engine::general_purpose::STANDARD as BASE64;
use plotting::DEFAULT_TEXT_PX;
use plotting::kitty::{KittyOpts, render_png};
use plotting::svg::render_svg;
use plotting::text::render_text_default;
use plotting::{ChartKind, ChartSpec, Series, SeriesPoint};
use scripting::runtime::EvalValue;
use crate::forms::outcome::key;
pub const MAX_SIDE: u32 = 2048;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ChartFormat {
Spec,
Svg,
Png,
Text,
}
impl ChartFormat {
#[must_use]
pub fn name(self) -> &'static str {
match self {
Self::Spec => "spec",
Self::Svg => "svg",
Self::Png => "png",
Self::Text => "text",
pub fn parse(name: &str) -> Option<Self> {
match name.to_ascii_lowercase().as_str() {
"spec" => Some(Self::Spec),
"svg" => Some(Self::Svg),
"png" => Some(Self::Png),
"text" => Some(Self::Text),
_ => None,
fn text(value: &str) -> EvalValue {
EvalValue::String(value.to_string())
pub fn kind_name(kind: ChartKind) -> &'static str {
match kind {
ChartKind::Bar => "bar",
ChartKind::HBar => "hbar",
ChartKind::StackedBar => "stacked",
ChartKind::Line => "line",
fn point_value(point: &SeriesPoint) -> EvalValue {
EvalValue::List(vec![
key("X"),
text(&point.x),
key("Y"),
EvalValue::Ratio {
numer: point.y_num,
denom: point.y_denom,
},
])
fn series_value(series: &Series) -> EvalValue {
key("LABEL"),
text(&series.label),
key("COMMODITY"),
text(&series.commodity_symbol),
key("POINTS"),
EvalValue::List(series.points.iter().map(point_value).collect()),
/// The spec as a plist: `(:title … :kind … :x-label … :y-label … :series ((:label … :commodity …
/// :points ((:x … :y ratio) …)) …) :notes (…))`.
pub(super) fn spec_value(spec: &ChartSpec) -> EvalValue {
key("TITLE"),
text(&spec.title),
key("KIND"),
text(kind_name(spec.kind)),
key("X-LABEL"),
text(&spec.x_label),
key("Y-LABEL"),
text(&spec.y_label),
key("SERIES"),
EvalValue::List(spec.series.iter().map(series_value).collect()),
key("NOTES"),
EvalValue::List(spec.notes.iter().map(|note| text(note)).collect()),
/// How a chart's text is sized and what a PNG is drawn on.
pub struct ChartLook {
/// The text height in pixels every label size scales from.
pub text_px: u32,
/// The PNG's opaque background; `None` leaves it transparent.
pub background: Option<[u8; 3]>,
impl Default for ChartLook {
fn default() -> Self {
Self {
text_px: DEFAULT_TEXT_PX,
background: None,
/// The spec drawn as an SVG document.
pub fn render_svg_chart(spec: &ChartSpec, width: u32, height: u32) -> String {
render_svg(spec, width, height, DEFAULT_TEXT_PX)
/// The PNG as the `#"base64"` literal a bytes value prints as.
fn png_value(
spec: &ChartSpec,
(width, height): (u32, u32),
look: ChartLook,
) -> wasmtime::Result<EvalValue> {
let opts = KittyOpts {
width_px: width,
height_px: height,
text_px: look.text_px,
background: look.background,
};
let png = render_png(spec, opts)
.ok_or_else(|| wasmtime::Error::msg("the chart could not be drawn as a PNG"))?;
Ok(EvalValue::String(format!("#\"{}\"", BASE64.encode(png))))
/// `spec` in `format`; `size` is the SVG's and the PNG's width and height.
///
/// # Errors
/// A PNG that cannot be drawn.
pub fn render_chart(
format: ChartFormat,
size: (u32, u32),
match format {
ChartFormat::Spec => Ok(spec_value(spec)),
ChartFormat::Svg => Ok(EvalValue::String(render_svg(
spec,
size.0,
size.1,
look.text_px,
))),
ChartFormat::Png => png_value(spec, size, look),
ChartFormat::Text => Ok(EvalValue::String(render_text_default(spec))),