lumen-dioxus 6.0.0

Lumen UI components for Dioxus: web, desktop, mobile, and Dioxus Native (Blitz).
Documentation
// cspell:words feff
//! Charts that present summaries rather than series: calendar heatmaps, funnels, box plots,
//! bullet charts, lollipop and dumbbell comparisons, histograms, waterfalls, and the
//! [`ChartMotion`] wrapper.

mod box_plot;
mod bullet;
mod calendar;
mod comparison;
mod funnel;
mod interval;
mod motion;

pub use box_plot::{BoxPlot, BoxPlotDatum, BoxPlotProps, BoxPlotStatisticLabels};
pub use bullet::{BulletChart, BulletChartProps, BulletChartRange};
pub use calendar::{
    CalendarHeatmap, CalendarHeatmapDatum, CalendarHeatmapProps, CalendarHeatmapWeekStart,
};
pub use comparison::{
    ChartComparisonDatum, DumbbellChart, DumbbellChartProps, LollipopChart, LollipopChartProps,
};
pub use funnel::{FunnelChart, FunnelChartDatum, FunnelChartProps};
pub use interval::{
    Histogram, HistogramBin, HistogramFrequency, HistogramProps, WaterfallChart,
    WaterfallChartDatum, WaterfallChartKind, WaterfallChartProps,
};
pub use motion::{ChartMotion, ChartMotionDuration, ChartMotionProps};

use dioxus::prelude::*;

use super::format::{css_percent, js_equals};
use super::model::ChartLabels;

/// Whether `text` is empty after JavaScript's `String.prototype.trim`.
///
/// JavaScript trims ECMAScript white space and line terminators, which includes U+FEFF but not
/// U+0085, so Rust's `str::trim` is not an exact match.
pub(crate) fn is_blank(text: &str) -> bool {
    const SPACES: [char; 13] = [
        '\t', '\n', '\u{b}', '\u{c}', '\r', ' ', '\u{a0}', '\u{1680}', '\u{2028}', '\u{2029}',
        '\u{202f}', '\u{205f}', '\u{3000}',
    ];

    text.chars().all(|character| {
        SPACES.contains(&character)
            || ('\u{2000}'..='\u{200a}').contains(&character)
            || character == '\u{feff}'
    })
}

/// Whether an optional measurement is missing or a finite number.
pub(crate) fn is_measurement(value: Option<f64>) -> bool {
    value.is_none_or(f64::is_finite)
}

/// The `ui-chart__empty` status line.
pub(crate) fn empty_status(text: &str) -> Element {
    rsx! {
        p { class: "ui-chart__empty", role: "status", "{text}" }
    }
}

/// The collapsible data table shared by the summary charts: one header row and caller rows.
pub(crate) fn summary_table(
    labels: &ChartLabels,
    headings: &[(&str, String)],
    rows: Element,
) -> Element {
    rsx! {
        details { class: "ui-chart__data",
            summary { "{labels.view_data}" }
            div { role: "group", tabindex: "0", aria_label: "{labels.chart_data}",
                table {
                    thead {
                        tr {
                            for (key, heading) in headings.iter() {
                                th { key: "{key}", scope: "col", "{heading}" }
                            }
                        }
                    }
                    tbody { {rows} }
                }
            }
        }
    }
}

/// Position a label on the scale; endpoint labels remain inside the chart.
pub(crate) fn tick_style(position: f64) -> String {
    let shift = if js_equals(position, 0.0) {
        0
    } else if js_equals(position, 1.0) {
        -100
    } else {
        -50
    };
    format!(
        "left:{};transform:translateX({shift}%)",
        css_percent(position)
    )
}

#[cfg(test)]
mod tests {
    use super::{is_blank, is_measurement, tick_style};

    #[test]
    fn blank_text_follows_javascript_trim() {
        assert!(is_blank(""));
        assert!(is_blank(" \t\n\u{feff}\u{3000}"));
        assert!(!is_blank("\u{85}"));
        assert!(!is_blank(" a "));
    }

    #[test]
    fn measurements_are_missing_or_finite() {
        assert!(is_measurement(None));
        assert!(is_measurement(Some(-3.0)));
        assert!(!is_measurement(Some(f64::NAN)));
        assert!(!is_measurement(Some(f64::INFINITY)));
    }

    #[test]
    fn labels_stay_on_the_scale_and_inside_endpoints() {
        assert_eq!(tick_style(0.0), "left:0%;transform:translateX(0%)");
        assert_eq!(tick_style(0.5), "left:50%;transform:translateX(-50%)");
        assert_eq!(tick_style(1.0), "left:100%;transform:translateX(-100%)");
    }
}