use super::*;
use crate::alpha::{AlphaEncoding, AlphaValues};
use crate::{BarInput, CategoryValues, NumericValues};
#[test]
#[expect(
clippy::too_many_lines,
reason = "one aggregate uncertainty contract follows actual facet/category/series units through both orientations and optional intervals"
)]
fn aggregate_uncertainty_alpha_uses_actual_category_series_and_facet_keys_before_fonts() {
for horizontal in [false, true] {
let category =
CategoryValues::new("category", ["B", "B", "A", "A", "B", "B", "A", "A"]).unwrap();
let values = NumericValues::new("value", [1.0, 3.0, 2.0, 4.0, 5.0, 7.0, 6.0, 8.0]);
let input = BarInput::samples(values).error_bar(crate::ErrorBar::standard_deviation());
let series =
CategoryValues::new("cohort", ["u", "u", "v", "v", "u", "u", "v", "v"]).unwrap();
let facet = crate::FacetSpec::column(
CategoryValues::new("panel", ["L", "L", "L", "L", "R", "R", "R", "R"]).unwrap(),
);
let source = if horizontal {
Plot::horizontal_bar(category, input)
.series(series)
.facet(facet)
.erase()
} else {
Plot::bar(category, input)
.series(series)
.facet(facet)
.erase()
}
.with_alpha(
AlphaTarget::Line,
AlphaEncoding::keyed(
[
CategoryValues::new("panel", ["R", "L", "R", "L", "unused"]).unwrap(),
CategoryValues::new("category", ["A", "A", "B", "B", "\u{10ffff}"]).unwrap(),
CategoryValues::new("cohort", ["v", "v", "u", "u", "unused"]).unwrap(),
],
AlphaValues::new("uncertainty confidence", [230, 64, 0, 128, 99]).unwrap(),
)
.unwrap(),
)
.with_alpha(
AlphaTarget::Fill,
AlphaEncoding::keyed(
[
CategoryValues::new("category", ["A", "A", "B", "B", "\u{10ffff}"]).unwrap(),
CategoryValues::new("cohort", ["v", "v", "u", "u", "unused"]).unwrap(),
CategoryValues::new("panel", ["R", "L", "R", "L", "unused"]).unwrap(),
],
AlphaValues::new("body confidence", [0, 230, 64, 0, 99]).unwrap(),
)
.unwrap(),
);
let mut theme = crate::Theme::publication().resolve().unwrap();
let interval = theme
.marks
.get_strokes()
.get_summaries()
.get_uncertainty()
.clone()
.color(crate::Color::rgba(17, 41, 67, 0))
.unwrap()
.width(40.0)
.unwrap();
let summaries = theme
.marks
.get_strokes()
.get_summaries()
.clone()
.uncertainty(interval)
.unwrap();
let strokes = theme
.marks
.get_strokes()
.clone()
.summaries(summaries)
.unwrap();
theme.marks = theme.marks.clone().strokes(strokes).unwrap();
let body = theme
.marks
.get_fills()
.get_bodies()
.get_bar_body()
.clone()
.fill_alpha(0)
.unwrap()
.outline(Some(crate::DataOutlineTheme::mapped(2.375, 61).unwrap()))
.unwrap();
let bodies = theme
.marks
.get_fills()
.get_bodies()
.clone()
.bar_body(body)
.unwrap();
let fills = theme.marks.get_fills().clone().bodies(bodies).unwrap();
theme.marks = theme.marks.clone().fills(fills).unwrap();
let text = crate::text::TextService::default();
let theme = crate::text::TextRequestContext::new(&theme, &text);
crate::orchestrator::with_prepared_plot_for_test(
&source,
&theme,
|_| Ok(()),
|chart, inputs, theme, _operation, round| {
let panels = &inputs.plots;
assert_eq!(panels.len(), 2);
for (registered, (expected, ends, fills)) in panels.iter().zip([
([128, 64], [2.0_f64, 3.0], [0, 230]),
([0, 230], [6.0, 7.0], [64, 0]),
]) {
let crate::orchestrator::registration::PreparedFamily::Bars {
marks: prepared,
..
} = ®istered.family
else {
panic!("prepared Bars")
};
assert_eq!(
prepared
.bars
.iter()
.map(|bar| bar.line_alpha.unwrap().alpha)
.collect::<Vec<_>>(),
expected
);
assert_eq!(
prepared
.bars
.iter()
.map(|bar| bar.fill_alpha.unwrap().alpha)
.collect::<Vec<_>>(),
fills
);
assert_eq!(
prepared
.bars
.iter()
.map(|bar| bar.end.to_bits())
.collect::<Vec<_>>(),
ends.map(f64::to_bits)
);
assert!(prepared.bars.iter().all(|bar| bar.interval.is_some()));
let minimum = intrinsic_body_minimum(chart, Some(registered), theme).unwrap();
assert_eq!(
(if horizontal { minimum.0 } else { minimum.1 }).to_bits(),
40.0_f64.to_bits()
);
let reads = crate::data::SOURCE_COLUMN_READS.with(std::cell::Cell::get);
let measurement = inputs.assignment_factory().assign(
chart
.sizing_request()
.assigned_size()
.unwrap_or(crate::layout::RootSize::new(1, 1)),
);
let factory = inputs
.plot_assignment_factory(&measurement, ®istered.scope.id)
.unwrap();
let selected = crate::layout::interior::measure_for_test(
&factory,
crate::layout::PixelRect::new(20.0, 1000.0, 20.0, 800.0),
round,
)
.unwrap();
assert_eq!(
crate::data::SOURCE_COLUMN_READS.with(std::cell::Cell::get),
reads
);
let crate::layout::interior::MarkPayload::Bars(payload) =
&selected.marks.as_ref().unwrap().payload
else {
panic!("Bar payload")
};
assert_eq!(
payload
.bars
.iter()
.map(|bar| bar.interval_style.color)
.collect::<Vec<_>>(),
expected.map(|alpha| crate::Color::rgba(17, 41, 67, alpha))
);
assert_eq!(
payload
.bars
.iter()
.map(|bar| bar.fill.color.get_alpha())
.collect::<Vec<_>>(),
fills
);
assert!(payload
.bars
.iter()
.all(|bar| bar.fill.stroke.get_alpha() == 61));
}
let calls = crate::font::canonical::FONT_DISCOVERY_CALLS.with(std::cell::Cell::get);
let incomplete = source.clone().with_alpha(
AlphaTarget::Line,
AlphaEncoding::keyed(
[
CategoryValues::new("category", ["B"]).unwrap(),
CategoryValues::new("cohort", ["u"]).unwrap(),
CategoryValues::new("panel", ["L"]).unwrap(),
],
AlphaValues::new("uncertainty confidence", [0]).unwrap(),
)
.unwrap(),
);
assert!(matches!(
incomplete.preflight().unwrap_err(),
Error::Data(_)
));
assert_eq!(
crate::font::canonical::FONT_DISCOVERY_CALLS.with(std::cell::Cell::get),
calls
);
},
)
.unwrap();
}
let source = Plot::bar(
CategoryValues::new("category", ["one"]).unwrap(),
BarInput::estimates(NumericValues::new("value", [2.0])),
)
.erase();
let absent = source.clone().with_alpha(
AlphaTarget::Line,
AlphaEncoding::keyed(
[CategoryValues::new("category", std::iter::empty::<&str>()).unwrap()],
AlphaValues::new("uncertainty confidence", []).unwrap(),
)
.unwrap(),
);
let operation = crate::font::canonical::OperationContext::new();
let (prepared, _, _) = crate::data::with_source_data_for_test(
&absent,
&operation,
crate::orchestrator::registration::RowSelection::Full,
|data| prepare(absent.description(), data, BarMode::Single).unwrap(),
)
.unwrap();
assert!(prepared.bars[0].interval.is_none());
assert!(prepared.bars[0].line_alpha.is_none());
let invalid = source.with_alpha(
AlphaTarget::Line,
AlphaEncoding::observations(AlphaValues::new("uncertainty confidence", [0]).unwrap()),
);
assert!(matches!(
crate::data::with_source_data_for_test(
&invalid,
&operation,
crate::orchestrator::registration::RowSelection::Full,
|data| prepare(invalid.description(), data, BarMode::Single).unwrap_err()
)
.unwrap(),
Error::Data(_)
));
}
#[test]
fn stacked_body_alpha_preserves_signed_accumulation_zero_segments_and_independent_outlines() {
for horizontal in [false, true] {
let category = CategoryValues::new("category", ["B", "A", "B", "A", "B", "A"]).unwrap();
let values = NumericValues::new("value", [1.0, 2.0, -3.0, 4.0, 3.0, -2.0]);
let series = CategoryValues::new("cohort", ["u", "u", "v", "v", "v", "w"]).unwrap();
let source = if horizontal {
Plot::stacked_horizontal_bar(category, values, series).erase()
} else {
Plot::stacked_bar(category, values, series).erase()
}
.with_alpha(
AlphaTarget::Fill,
AlphaEncoding::keyed(
[
CategoryValues::new("cohort", ["w", "v", "v", "u", "u"]).unwrap(),
CategoryValues::new("category", ["A", "A", "B", "A", "B"]).unwrap(),
],
AlphaValues::new("body confidence", [17, 64, 0, 230, 128]).unwrap(),
)
.unwrap(),
);
let theme = crate::Theme::publication().resolve().unwrap();
let text = crate::text::TextService::default();
let theme = crate::text::TextRequestContext::new(&theme, &text);
crate::orchestrator::with_prepared_plot_for_test(
&source,
&theme,
|_| Ok(()),
|chart, inputs, _theme, _operation, round| {
let registered = &inputs.plots[0];
let prepared = retained(Some(registered.family.as_ref())).unwrap();
assert_eq!(prepared.primary_mark_count(), 5);
assert_eq!(
prepared
.bars
.iter()
.map(|bar| (bar.start.to_bits(), bar.end.to_bits(), bar.nonzero))
.collect::<Vec<_>>(),
[
(0.0_f64.to_bits(), 1.0_f64.to_bits(), true),
(0.0_f64.to_bits(), 2.0_f64.to_bits(), true),
(1.0_f64.to_bits(), 1.0_f64.to_bits(), false),
(2.0_f64.to_bits(), 6.0_f64.to_bits(), true),
(0.0_f64.to_bits(), (-2.0_f64).to_bits(), true),
]
);
let measurement = inputs.assignment_factory().assign(
chart
.sizing_request()
.assigned_size()
.unwrap_or(crate::layout::RootSize::new(1, 1)),
);
let factory = inputs
.plot_assignment_factory(&measurement, ®istered.scope.id)
.unwrap();
let selected = crate::layout::interior::measure_for_test(
&factory,
crate::layout::PixelRect::new(20.0, 1000.0, 20.0, 800.0),
round,
)
.unwrap();
let crate::layout::interior::MarkPayload::Bars(payload) =
&selected.marks.as_ref().unwrap().payload
else {
panic!("stacked Bar payload")
};
assert_eq!(
payload
.bars
.iter()
.map(|bar| bar.fill.color.get_alpha())
.collect::<Vec<_>>(),
[128, 230, 0, 64, 17]
);
assert!(payload.bars.iter().all(|bar| bar.interval.is_none()));
assert!(payload.bars.iter().all(|bar| bar.fill.stroke.get_alpha()
== theme
.marks
.get_fills()
.get_bodies()
.get_bar_body()
.backend_style((0, 0, 0))
.stroke
.get_alpha()));
},
)
.unwrap();
}
}