lichtung-plot 0.3.0

Strongly typed scientific plotting with deterministic SVG and PNG output
Documentation
//! Actual cumulative endpoints and original contribution domains.

use crate::{
    builder::PlotDescription,
    data::{DataPayload, DataRole, PlotData, SourceMembers},
    elements::ElementRef,
    stats::{
        results::{numeric_column, result_keys, StatDomainInput},
        StatResult, WaterfallField,
    },
    Result,
};

impl super::PreparedWaterfall {
    pub(crate) fn scientific_result(
        &self,
        source: &PlotDescription,
        owner: &ElementRef,
    ) -> Result<StatResult> {
        let originals = source.primary_layer_data(&owner.declaration());
        let category = crate::data::select_value(&originals, DataRole::Category.into())?;
        let delta = crate::data::select_value(&originals, DataRole::Delta.into())?;
        let source_categories = source
            .automatic_axis_category_values(source.kind(), false)
            .expect("actual Waterfall category");
        let slots =
            crate::data::source::collect_source(self.spans.iter().map(|span| Ok(span.alpha.slot)))?;
        let keys = crate::data::source::collect_source(slots.iter().map(|&slot| {
            source_categories
                .key_for_level(
                    source_categories
                        .get(slot)
                        .flatten()
                        .expect("actual category"),
                )
                .ok_or_else(|| {
                    crate::Error::Render("waterfall category has no original key".into())
                })
        }))?;
        let direct = crate::data::source::collect_source(slots.iter().map(|&slot| {
            Ok(Some(SourceMembers::from_slots(
                &[&category, &delta],
                [slot],
            )?))
        }))?;
        let prefixes = SourceMembers::prefixes(&delta, &slots)?;
        let starts = crate::data::source::collect_source(
            prefixes[..slots.len()].iter().cloned().map(Some).map(Ok),
        )?;
        let ends =
            crate::data::source::collect_source(prefixes[1..].iter().cloned().map(Some).map(Ok))?;
        let columns = vec![
            (
                WaterfallField::Category.into(),
                crate::data::group_key::key_column(
                    source_categories.original_name(),
                    keys.into_iter(),
                )?,
            ),
            (
                WaterfallField::Delta.into(),
                DataPayload::Numeric(numeric_column(
                    delta.info().name().unwrap_or("delta"),
                    self.spans.iter().map(|span| span.delta),
                )?),
            ),
        ];
        let keys = result_keys(&columns, &[WaterfallField::Category.into()])?;
        StatResult::from_domains(
            owner,
            [
                StatDomainInput {
                    columns,
                    members: direct,
                    group: None,
                    keys: Some(keys.clone()),
                    rows: None,
                },
                StatDomainInput {
                    columns: vec![(
                        WaterfallField::Start.into(),
                        DataPayload::Numeric(numeric_column(
                            "start",
                            self.spans.iter().map(|span| span.start),
                        )?),
                    )],
                    members: starts,
                    group: None,
                    keys: Some(keys.clone()),
                    rows: None,
                },
                StatDomainInput {
                    columns: vec![(
                        WaterfallField::End.into(),
                        DataPayload::Numeric(numeric_column(
                            "end",
                            self.spans.iter().map(|span| span.end),
                        )?),
                    )],
                    members: ends,
                    group: None,
                    keys: Some(keys),
                    rows: None,
                },
            ],
        )
    }
}
fn prepare_result(
    chart: &PlotDescription,
    data: PlotData<'_, '_>,
    owner: &ElementRef,
) -> Result<StatResult> {
    super::prepare(chart, data)?
        .0
        .scientific_result(chart, owner)
}
crate::orchestrator::science::stat_result_api!(Waterfall, StatResult, |source, data, layer| {
    prepare_result(source, data, layer)
});