#![allow(
clippy::cast_possible_truncation,
clippy::cast_precision_loss,
clippy::cast_sign_loss,
clippy::too_many_lines
)]
use crate::alpha::{marks::ObservationAlpha, AlphaEncoding, AlphaTarget, AlphaUnitKind};
use crate::Error;
use crate::builder::PlotDescription;
use crate::layout::{AxisTick, Layout, PhysicalTextAlignment, PixelRect, TextBlock};
use crate::render::backend::{
with_clip, Backend, FillStyle, GradientStop, LineStyle, PlacedText, Point2D, TextAnchor,
};
use crate::spec::{
CategoryOrder, HeatmapAnnotations, HeatmapLayout, PlotMeasureContext, PlotRenderResult,
};
use crate::theme::{ContinuousColorScale, OpaqueColor};
#[cfg(test)]
use crate::Plot;
#[cfg(test)]
mod tests;
const MAX_CELLS: usize = 1_000_000;
#[derive(Debug, PartialEq)]
pub(crate) struct HeatmapData {
cells: Vec<Option<f64>>,
fills: Vec<Option<OpaqueColor>>,
fill_binding: AlphaEncoding,
fill_alphas: std::sync::Arc<Vec<Option<ObservationAlpha>>>,
highlighted: Vec<bool>,
null_row_annotations: Vec<Option<crate::Text>>,
has_missing: bool,
color: PreparedHeatmapColor,
colorbar_stops: Vec<GradientStop>,
layout: HeatmapLayout,
value_labels: Vec<Option<String>>,
value_owners: Vec<Option<String>>,
value_presentation: Option<std::sync::Arc<crate::numeric::set::NumericPresentation>>,
colorbar_values: Option<crate::numeric::set::FormattedSet>,
color_format: Option<std::sync::Arc<crate::numeric::format::NumericFormat>>,
pub(crate) color_label: crate::Text,
}
impl HeatmapData {
pub(crate) fn numeric_explanation(&self) -> Option<&crate::Text> {
self.value_presentation
.as_ref()
.and_then(|numeric| numeric.set.explanation.as_ref())
}
pub(crate) fn alpha_guide(
&self,
_chart: &crate::builder::PlotDescription,
theme: &crate::theme::ResolvedTheme,
guide: &mut crate::decorations::legend::alpha::AlphaGuideBuilder<'_>,
) -> crate::Result<()> {
use crate::decorations::legend::alpha::{AlphaGlyphRole, AlphaUnit};
for (index, alpha) in self.fill_alphas.iter().enumerate() {
let Some(alpha) = alpha else { continue };
let highlighted = self.highlighted[index];
let outline = if highlighted {
Some(
theme
.annotations
.get_cells()
.get_highlight_outline()
.clone(),
)
} else {
theme.marks.get_cells().get_cell_outline().cloned()
};
let fill = crate::theme::FixedFillTheme::new(
crate::Color::rgba(0, 0, 0, theme.marks.get_cells().get_fill_alpha()),
outline,
)?
.backend_style();
guide.fill(
if highlighted {
AlphaGlyphRole::Highlight
} else {
AlphaGlyphRole::Fill
},
AlphaUnitKind::Observation,
AlphaUnit::observation(*alpha),
alpha.value,
fill,
false,
)?;
}
Ok(())
}
fn resolved_cell_fill(&self, index: usize, role_alpha: u8) -> Option<crate::Color> {
self.fills[index].map(|fill| {
let member = self.fill_alphas[index]
.expect("finite Heatmap cell retains original alpha membership");
crate::Color::from(fill).alpha(member.value.map_or(role_alpha, |value| value.alpha))
})
}
pub(crate) fn guide_semantics(
&self,
theme: &crate::theme::ResolvedTheme,
) -> Result<crate::decorations::legend::descriptor::ContinuousSemantics, Error> {
let domain = self.color.domain();
let missing = self
.has_missing
.then(|| -> Result<_, Error> {
Ok(crate::decorations::legend::descriptor::MissingKey {
fill: theme.marks.get_cells().get_missing_fill(),
outline: theme.marks.get_cells().get_missing_key_outline().cloned(),
label: crate::data::source::copy_source_string("Missing")?,
})
})
.transpose()?;
Ok(
crate::decorations::legend::descriptor::ContinuousSemantics {
domain: (domain.minimum, domain.maximum),
selection: self.color.selection.clone(),
missing,
format: self.color_format.clone(),
},
)
}
pub(crate) fn guide_samples(&self) -> Result<Vec<f64>, Error> {
let mut samples = Vec::new();
crate::data::source::reserve_source(&mut samples, self.cells.len())?;
for &value in &self.cells {
if let Some(value) = value {
samples.push(value);
}
}
Ok(samples)
}
pub(crate) fn install_shared_domain(
&mut self,
descriptor: &crate::decorations::legend::descriptor::ContinuousDescriptor,
) -> Result<(), Error> {
self.replace_domain(
ResolvedColorDomain {
minimum: descriptor.domain.0,
maximum: descriptor.domain.1,
center: descriptor.center,
},
descriptor.selection.clone(),
&descriptor.numeric,
)?;
self.colorbar_stops =
crate::data::source::collect_source(descriptor.stops.iter().copied().map(Ok))?;
Ok(())
}
fn replace_domain(
&mut self,
domain: ResolvedColorDomain,
selection: ContinuousColorScale,
numeric: &crate::numeric::set::FormattedSet,
) -> Result<(), Error> {
let color = PreparedHeatmapColor::resolve(domain, selection)?;
let fills = crate::data::source::collect_source(
self.cells
.iter()
.map(|value| value.map(|value| color.scale.color(value)).transpose()),
)?;
let colorbar_stops = color.guide_stops(&self.guide_samples()?)?;
let colorbar_values = Some(numeric.try_copy()?);
self.color = color;
self.fills = fills;
self.colorbar_stops = colorbar_stops;
self.colorbar_values = colorbar_values;
Ok(())
}
pub(crate) fn guide_descriptor(
&self,
field: &str,
theme: &crate::theme::ResolvedTheme,
) -> Result<Option<crate::decorations::legend::descriptor::ContinuousDescriptor>, Error> {
let Some(values) = &self.colorbar_values else {
return Ok(None);
};
let semantics = self.guide_semantics(theme)?;
Ok(Some(
crate::decorations::legend::descriptor::ContinuousDescriptor {
field: crate::data::source::copy_source_string(field)?,
title: crate::data::source::source_arc(self.color_label.try_copy()?)?,
stops: crate::data::source::collect_source(
self.colorbar_stops.iter().copied().map(Ok),
)?,
center: semantics.selection.get_center(),
selection: semantics.selection,
missing: semantics.missing,
domain: semantics.domain,
exact: self.color.selection.get_domain().is_some(),
format: semantics.format,
numeric: values.try_copy()?,
},
))
}
}
#[expect(
clippy::cast_precision_loss,
reason = "selected CSS-pixel tracks are bounded by the exact u32 root dimensions"
)]
fn track_pixels(quanta: i64) -> f64 {
quanta as f64 / 1024.0
}
#[expect(
clippy::too_many_arguments,
reason = "the original column fit borrows its category topology from the owning axes"
)]
fn assigned_columns<'a>(
prepared: &HeatmapData,
levels: [&[String]; 2],
label_count: usize,
label: impl Fn(usize) -> &'a str,
selection: &crate::CategoryLabelSelection,
panel: PixelRect,
theme: &crate::text::TextRequestContext<'_, '_>,
scope: &crate::layout::fit::FitScope,
) -> Result<Vec<f64>, crate::layout::fit::PlanError> {
use crate::layout::fit::FitOutcome;
use crate::layout::kernel::{GridRequirement, TrackSpec};
let count = levels[0].len();
let cell = intrinsic_cell_ink(prepared, levels, theme)?;
let label_pitch = if selection.is_all() {
0.0
} else {
crate::layout::category_label_span(selection, count, |index| {
column_label_hard(prepared, label(index), theme)
})? / count as f64
};
let mut hard = crate::data::source::collect_source((0..label_count).map(|index| {
let text = if selection.is_all() {
column_label_hard(prepared, label(index), theme)?
} else {
label_pitch
};
Ok((cell.0 / count as f64).max(text))
}))?;
for (index, (source, fill)) in prepared
.value_labels
.iter()
.zip(&prepared.fills)
.enumerate()
{
let (Some(source), Some(_)) = (source, fill) else {
continue;
};
let fill = prepared
.resolved_cell_fill(index, theme.marks.get_cells().get_fill_alpha())
.expect("prepared finite cell");
let appearance = theme.contrasting_text(fill)?;
let widths = crate::layout::fit::intrinsic_widths(
theme,
source,
appearance,
"Heatmap column value label",
)?;
let padding = theme
.annotations
.get_cells()
.value_padding(appearance.get_font_size())?;
hard[index % count] = hard[index % count].max(padding.width(widths.0)?);
}
let preferred = crate::layout::kernel::ceil_spatial_quanta(
2.0 * theme.typography.axes.tick_label.font_size,
)?;
let base = crate::data::source::collect_source(hard.into_iter().map(|width| {
let min = crate::layout::kernel::ceil_spatial_quanta(width)?;
Ok(TrackSpec {
min,
preferred: min.max(preferred),
max: None,
flexible: true,
})
}))?;
let start = crate::layout::kernel::nearest_spatial_quanta(panel.x0)?;
let end = crate::layout::kernel::nearest_spatial_quanta(panel.x1)?;
let total = end
.checked_sub(start)
.ok_or_else(|| Error::Render("Heatmap assigned column track span overflowed".into()))?;
let children: [GridRequirement<'_>; 0] = [];
let allocation = match crate::layout::kernel::allocate_grid_axis(
&base,
&children,
total,
[0, 0],
0,
None,
true,
scope,
)? {
FitOutcome::Measured(allocation) => allocation,
FitOutcome::Miss(failure) => return Err(failure.into()),
};
let mut edges = Vec::new();
crate::data::source::reserve_source(&mut edges, count + 1)?;
let mut edge = start;
edges.push(track_pixels(edge));
for width in allocation.sizes {
edge = edge
.checked_add(width)
.ok_or_else(|| Error::Render("Heatmap column edge overflowed spatial quanta".into()))?;
edges.push(track_pixels(edge));
}
Ok(edges)
}
#[expect(
clippy::too_many_arguments,
reason = "full cell annotations, independent label eligibility and assigned columns constrain row tracks together"
)]
fn assigned_rows(
prepared: &HeatmapData,
levels: [&[String]; 2],
annotations: &AnnotationPlans,
columns: &[f64],
labels: &[String],
selection: &crate::CategoryLabelSelection,
labels_visible: bool,
tick_width: f64,
context: &PlotMeasureContext<'_, '_>,
theme: &crate::text::TextRequestContext<'_, '_>,
panel: PixelRect,
) -> Result<Vec<f64>, crate::layout::fit::PlanError> {
use crate::layout::fit::FitOutcome;
use crate::layout::kernel::{GridRequirement, TrackSpec};
let count = levels[1].len();
let width = levels[0].len();
if labels.len() != count {
return Err(
Error::Render("Heatmap prepared row label count differs from tracks".into()).into(),
);
}
let ink = intrinsic_cell_ink(prepared, levels, theme)?;
let mut hard =
crate::data::source::collect_source((0..count).map(|_| Ok(ink.1 / count as f64)))?;
let mut label_heights = crate::data::source::collect_source((0..count).map(|_| Ok(0.0)))?;
for (index, label) in labels.iter().enumerate().filter(|(index, _)| {
labels_visible
&& selection.includes(*index, count)
&& !theme
.tick_text_appearance(crate::layout::AxisSide::Left)
.hidden()
&& !theme.text_layouts.ticks[1].as_ref().is_some_and(|request| {
request
.instructions()
.visibility
.as_ref()
.is_some_and(crate::TextVisibility::reduces)
})
}) {
let text = crate::layout::fit::measured(crate::layout::fit::text(
theme,
label,
theme.tick_text_appearance(crate::layout::AxisSide::Left),
tick_width,
PhysicalTextAlignment::End,
"y-axis tick label",
&context
.fit_scope()
.child(crate::layout::AddressRole::AxisLeft, 0)?,
))?;
if !text.lines.is_empty() {
label_heights[index] = crate::layout::text::font_fit::hard_height(theme, &text)?.0
+ 2.0 * crate::layout::LABEL_CLEARANCE;
}
}
let pitch = if selection.is_all() {
0.0
} else {
crate::layout::category_label_span(selection, count, |index| Ok(label_heights[index]))?
/ count as f64
};
for (index, hard) in hard.iter_mut().enumerate() {
*hard = hard.max(if selection.is_all() {
label_heights[index]
} else {
pitch
});
}
for (index, plan) in annotations.values.iter().enumerate() {
if let Some(plan) = plan {
let (height, size) = crate::layout::text::font_fit::hard_height(theme, plan)?;
let padding = theme.annotations.get_cells().value_padding(size)?;
if !plan.lines.is_empty() {
hard[index / width] = hard[index / width].max(if height == 0.0 {
0.0
} else {
padding.height(height)?
});
}
}
}
for (index, plan) in annotations.rows.iter().enumerate() {
if let Some(plan) = plan {
let (height, size) = crate::layout::text::font_fit::hard_height(theme, plan)?;
let padding = theme.annotations.get_cells().null_row_padding(size)?;
if !plan.lines.is_empty() {
hard[index] = hard[index].max(if height == 0.0 {
0.0
} else {
padding.height(height)?
});
}
}
}
let square = columns
.windows(2)
.map(|pair| pair[1] - pair[0])
.fold(0.0_f64, f64::max);
let preferred = crate::layout::kernel::ceil_spatial_quanta(
square.max(2.0 * theme.typography.axes.tick_label.font_size),
)?;
let base = crate::data::source::collect_source(hard.into_iter().map(|height| {
let min = crate::layout::kernel::ceil_spatial_quanta(height)?;
Ok(TrackSpec {
min,
preferred: min.max(preferred),
max: None,
flexible: true,
})
}))?;
let bottom = crate::layout::kernel::nearest_spatial_quanta(panel.y0)?;
let top = crate::layout::kernel::nearest_spatial_quanta(panel.y1)?;
let total = top
.checked_sub(bottom)
.ok_or_else(|| Error::Render("Heatmap assigned row track span overflowed".into()))?;
let children: [GridRequirement<'_>; 0] = [];
let allocation = match crate::layout::kernel::allocate_grid_axis(
&base,
&children,
total,
[0, 0],
0,
None,
true,
context.fit_scope(),
)? {
FitOutcome::Measured(allocation) => allocation,
FitOutcome::Miss(failure) => return Err(failure.into()),
};
let mut edges = Vec::new();
crate::data::source::reserve_source(&mut edges, count + 1)?;
let mut edge = top;
edges.push(track_pixels(edge));
for height in allocation.sizes {
edge = edge
.checked_sub(height)
.ok_or_else(|| Error::Render("Heatmap row edge overflowed spatial quanta".into()))?;
edges.push(track_pixels(edge));
}
Ok(edges)
}
fn retained_in<'v>(
registered: Option<&'v crate::orchestrator::registration::PlotRegistration<'_>>,
) -> Result<(&'v HeatmapData, [&'v [String]; 2]), Error> {
if registered.is_none() {
return Err(Error::Render(
"heatmap requires root-prepared semantics".into(),
));
}
match crate::layout::interior::preference::panel(registered, "Heatmap")?.as_ref() {
crate::orchestrator::registration::FamilyRef::Heatmap(marks) => Ok((
marks,
[0, 1].map(|side| {
registered
.expect("prepared Heatmap occurrence")
.axes
.categories(side)
.expect("prepared Heatmap categories")
.raw
.as_slice()
}),
)),
_ => Err(Error::Render(
"heatmap requires root-prepared semantics".into(),
)),
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
struct ResolvedColorDomain {
minimum: f64,
maximum: f64,
center: Option<f64>,
}
#[derive(Debug, PartialEq)]
struct PreparedHeatmapColor {
selection: ContinuousColorScale,
scale: crate::theme::ContinuousColors,
}
impl PreparedHeatmapColor {
fn resolve(
domain: ResolvedColorDomain,
selection: ContinuousColorScale,
) -> Result<Self, Error> {
if selection.get_center().map(f64::to_bits) != domain.center.map(f64::to_bits) {
return Err(Error::Render(
"resolved Heatmap domain disagrees with its retained scale selection".into(),
));
}
Ok(Self {
scale: crate::theme::ContinuousColors::selected(
(domain.minimum, domain.maximum),
&selection,
)?,
selection,
})
}
fn domain(&self) -> ResolvedColorDomain {
let (minimum, maximum) = self.scale.domain();
ResolvedColorDomain {
minimum,
maximum,
center: self.selection.get_center(),
}
}
fn guide_stops(&self, observed: &[f64]) -> Result<Vec<GradientStop>, Error> {
self.scale.guide_stops(observed)
}
}
#[derive(Debug)]
pub(crate) struct HeatmapIntrinsicCache {
layouts: crate::text::roles::TextRoles,
minimum: (f64, f64),
without_y_labels: (f64, f64),
color_scale: crate::theme::ContinuousColors,
contrast_light: crate::theme::TextTheme,
contrast_dark: crate::theme::TextTheme,
tick_label: crate::theme::TextTheme,
category_labels: [crate::CategoryLabelSelection; 2],
axis_title: crate::theme::TextTheme,
titles: [Option<crate::Text>; 2],
null_rows: Vec<Option<crate::Text>>,
plot_panel: crate::theme::Color,
canvas_root: crate::theme::Color,
canvas_override: Option<crate::theme::Color>,
backdrop: OpaqueColor,
cell_marks: crate::theme::mark::CellMarkRoles,
cell_annotations: crate::theme::annotation::CellAnnotationGeometry,
value_presentation: Option<std::sync::Arc<crate::numeric::set::NumericPresentation>>,
fill_binding: AlphaEncoding,
fill_alphas: std::sync::Arc<Vec<Option<ObservationAlpha>>>,
}
impl HeatmapIntrinsicCache {
fn matches_inputs(
&self,
prepared: &HeatmapData,
axes: &crate::orchestrator::registration::PreparedAxesOwner,
registered: &crate::orchestrator::registration::PlotRegistration<'_>,
theme: &crate::text::TextRequestContext<'_, '_>,
) -> bool {
let source = registered.source;
self.color_scale.same(&prepared.color.scale)
&& self.layouts == theme.text_layouts
&& self.category_labels
== [axes.axis(source, 0), axes.axis(source, 1)].map(|axis| {
axis.authored()
.expect("coordinate authored intent")
.category_labels
.clone()
})
&& self.titles.each_ref().map(Option::as_ref)
== [axes.label(registered, 0), axes.label(registered, 1)]
&& self.null_rows == prepared.null_row_annotations
&& self
.contrast_light
.same(&theme.typography.annotations.contrast.light)
&& self
.contrast_dark
.same(&theme.typography.annotations.contrast.dark)
&& self.tick_label.same(&theme.typography.axes.tick_label)
&& self.axis_title.same(&theme.typography.axes.axis_title)
&& self.plot_panel == theme.surfaces.plot_panel
&& self.canvas_root == theme.surfaces.canvas_root
&& self.canvas_override == theme.canvas_surface_override
&& self.backdrop == theme.annotations.get_planning_backdrop()
&& self.cell_marks == *theme.marks.get_cells()
&& self.cell_annotations == *theme.annotations.get_cells()
&& self.value_presentation == prepared.value_presentation
&& self.fill_binding == prepared.fill_binding
&& self.fill_alphas == prepared.fill_alphas
}
fn new(
minimum: (f64, f64),
without_y_labels: (f64, f64),
prepared: &HeatmapData,
axes: &crate::orchestrator::registration::PreparedAxesOwner,
registered: &crate::orchestrator::registration::PlotRegistration<'_>,
theme: &crate::text::TextRequestContext<'_, '_>,
) -> Result<Self, Error> {
let source = registered.source;
let color_scale = prepared.color.scale.clone();
let text = |role: &crate::theme::TextTheme| -> Result<_, Error> {
crate::theme::TextTheme::new(
crate::data::source::copy_font_stack(role.get_stack())?,
role.get_color(),
role.get_font_size(),
role.get_weight(),
role.get_line_height(),
)
};
Ok(Self {
minimum,
layouts: theme.text_layouts.clone(),
without_y_labels,
color_scale,
contrast_light: text(&theme.typography.annotations.contrast.light)?,
contrast_dark: text(&theme.typography.annotations.contrast.dark)?,
tick_label: text(&theme.typography.axes.tick_label)?,
category_labels: [axes.axis(source, 0), axes.axis(source, 1)].map(|axis| {
axis.authored()
.expect("coordinate authored intent")
.category_labels
.clone()
}),
axis_title: text(&theme.typography.axes.axis_title)?,
titles: [
axes.copy_label(registered, 0)?,
axes.copy_label(registered, 1)?,
],
null_rows: crate::data::source::collect_source(
prepared
.null_row_annotations
.iter()
.map(|text| text.as_ref().map(crate::Text::try_copy).transpose()),
)?,
plot_panel: theme.surfaces.plot_panel,
canvas_root: theme.surfaces.canvas_root,
canvas_override: theme.canvas_surface_override,
backdrop: theme.annotations.get_planning_backdrop(),
cell_marks: theme.marks.get_cells().clone(),
cell_annotations: theme.annotations.get_cells().clone(),
value_presentation: prepared.value_presentation.clone(),
fill_binding: prepared.fill_binding.clone(),
fill_alphas: prepared.fill_alphas.clone(),
})
}
}
pub(crate) fn intrinsic_body_minimum(
registered: Option<&crate::orchestrator::registration::PlotRegistration<'_>>,
intrinsic: &crate::orchestrator::root::PlotIntrinsics,
operation: &crate::font::canonical::OperationContext,
theme: &crate::text::TextRequestContext<'_, '_>,
) -> Result<(f64, f64), Error> {
registered
.and_then(|registered| registered.axes.coordinate())
.ok_or_else(|| Error::Render("heatmap intrinsics require root-prepared axes".into()))?;
retained_in(registered)?;
let (minimum, without_y_labels) = heatmap_minima(
intrinsic,
&crate::orchestrator::root::intrinsic_inputs(registered, theme, operation)?,
)?;
if crate::orchestrator::registration::axis_tick_labels_visible(registered, 1) {
Ok(minimum)
} else {
Ok(without_y_labels)
}
}
pub(crate) fn natural_body_preference(
registered: Option<&crate::orchestrator::registration::PlotRegistration<'_>>,
intrinsic: &crate::orchestrator::root::PlotIntrinsics,
operation: &crate::font::canonical::OperationContext,
theme: &crate::text::TextRequestContext<'_, '_>,
) -> Result<(f64, f64), Error> {
let (_, levels) = retained_in(registered)?;
let hard = intrinsic_body_minimum(registered, intrinsic, operation, theme)?;
let pitch = 2.0 * theme.typography.axes.tick_label.font_size;
Ok((
hard.0.max(pitch * levels[0].len() as f64),
hard.1.max(pitch * levels[1].len() as f64),
))
}
pub(crate) fn natural_body_height(
chart: &PlotDescription,
registered: Option<&crate::orchestrator::registration::PlotRegistration<'_>>,
theme: &crate::text::TextRequestContext<'_, '_>,
width: f64,
) -> Result<f64, Error> {
let (marks, levels) = retained_in(registered)?;
let axes = registered
.and_then(|registered| registered.axes.coordinate())
.ok_or_else(|| Error::Render("natural Heatmap axes missing".into()))?;
theme.text.prepared_font_environment()?;
let labels = axes
.categories(0)
.ok_or_else(|| Error::Render("natural Heatmap columns missing".into()))?;
let columns = assigned_columns(
marks,
levels,
labels.ticks.len(),
|index| &labels.ticks[index].label,
&axes
.axis(registered.expect("prepared Heatmap occurrence").source, 0)
.authored()
.expect("coordinate authored intent")
.category_labels,
PixelRect::new(0.0, width, 0.0, 1.0),
theme,
&crate::layout::fit::FitScope::root(),
)
.map_err(crate::layout::fit::PlanError::into_error)?;
let annotations = measure_annotations(
chart,
marks,
levels,
&columns,
theme,
&crate::layout::fit::FitScope::root(),
)
.map_err(crate::layout::fit::PlanError::into_error)?;
let preferred = columns
.windows(2)
.map(|edge| edge[1] - edge[0])
.fold(2.0 * theme.typography.axes.tick_label.font_size, f64::max);
let mut heights = crate::data::source::collect_source(levels[1].iter().map(|_| Ok(preferred)))?;
for (index, plan) in annotations.values.iter().enumerate() {
if let Some(plan) = plan {
let padding = theme
.annotations
.get_cells()
.value_padding(plan.style.font_size)?;
heights[index / levels[0].len()] =
heights[index / levels[0].len()].max(if plan.lines.is_empty() {
0.0
} else {
padding.height(plan.occupied_size().1)?
});
}
}
for (index, plan) in annotations.rows.iter().enumerate() {
if let Some(plan) = plan {
let padding = theme
.annotations
.get_cells()
.null_row_padding(plan.style.font_size)?;
if !plan.lines.is_empty() {
heights[index] = heights[index].max(padding.height(plan.occupied_size().1)?);
}
}
}
Ok(heights.into_iter().sum())
}
fn intrinsic_cell_ink(
prepared: &HeatmapData,
levels: [&[String]; 2],
theme: &crate::text::TextRequestContext<'_, '_>,
) -> Result<(f64, f64), Error> {
let (columns, rows) = (levels[0].len(), levels[1].len());
let slots = columns.checked_mul(rows).ok_or_else(|| {
Error::Render("heatmap declared cell topology exceeds spatial capacity".into())
})?;
if columns == 0
|| rows == 0
|| prepared.highlighted.len() != prepared.cells.len()
|| prepared.null_row_annotations.len() != rows
|| prepared.cells.len() != slots
{
return Err(Error::Render(
"heatmap cell topology differs from prepared category tracks".into(),
));
}
let quantum = 1.0 / 1024.0;
let cell = |outline: Option<&crate::theme::OutlineTheme>| -> Result<_, Error> {
let fill =
crate::theme::FixedFillTheme::new(crate::theme::Color::rgb(0, 0, 0), outline.cloned())?
.backend_style();
let (x, y) = crate::render::geometry::rectangular_fill_ink_outset(&fill)?;
Ok((x + quantum, y + quantum))
};
let mut normal = None;
let mut highlight = None;
let mut missing_row = None;
let mut cross = None;
let mut pitch = (quantum, quantum);
for y in 0..rows {
if prepared.null_row_annotations[y].is_some() {
let ink = if let Some(ink) = missing_row {
ink
} else {
let ink = cell(theme.marks.get_cells().get_missing_row_outline())?;
missing_row = Some(ink);
ink
};
pitch.0 = pitch.0.max(ink.0);
pitch.1 = pitch.1.max(ink.1);
}
for x in 0..columns {
if !active_cell(prepared.layout, x, y) {
continue;
}
if prepared.null_row_annotations[y].is_some() {
continue;
}
let index = y * columns + x;
let ink = if prepared.highlighted[index] {
if let Some(ink) = highlight {
ink
} else {
let ink = cell(Some(theme.annotations.get_cells().get_highlight_outline()))?;
highlight = Some(ink);
ink
}
} else if let Some(ink) = normal {
ink
} else {
let ink = cell(theme.marks.get_cells().get_cell_outline())?;
normal = Some(ink);
ink
};
pitch.0 = pitch.0.max(ink.0);
pitch.1 = pitch.1.max(ink.1);
if prepared.cells[index].is_none() {
let cross = if let Some(stroke) = cross {
stroke
} else {
let stroke = crate::render::geometry::line_ink_width(
&theme.marks.get_cells().get_missing_cross().backend_style(),
)?;
cross = Some(stroke);
stroke
};
let span = (2.0_f64).max(cross + 2.0 * quantum) + quantum;
pitch.0 = pitch.0.max(span);
pitch.1 = pitch.1.max(span);
}
}
}
let columns = f64::from(
u32::try_from(columns)
.map_err(|_| Error::Render("heatmap column count exceeds spatial range".into()))?,
);
let rows = f64::from(
u32::try_from(rows)
.map_err(|_| Error::Render("heatmap row count exceeds spatial range".into()))?,
);
let result = (pitch.0 * columns, pitch.1 * rows);
if !result.0.is_finite() || !result.1.is_finite() {
return Err(Error::Render(
"heatmap painted cell hard span overflowed".into(),
));
}
Ok(result)
}
fn column_label_hard(
_prepared: &HeatmapData,
label: &str,
theme: &crate::text::TextRequestContext<'_, '_>,
) -> Result<f64, Error> {
if let Some(layout) = &theme.text_layouts.ticks[0] {
if layout.hidden()
|| layout
.instructions()
.visibility
.as_ref()
.is_some_and(crate::TextVisibility::reduces)
|| layout.instructions().overlap == Some(crate::TextOverlap::Allow)
{
return Ok(0.0);
}
return Ok(crate::layout::fit::intrinsic_widths(
theme,
label,
theme.tick_text_appearance(crate::layout::AxisSide::Bottom),
"Heatmap category intrinsic",
)?
.0 + 2.0 * crate::layout::LABEL_CLEARANCE);
}
let hard = crate::layout::fit::intrinsic_widths(
theme,
label,
theme.tick_text_appearance(crate::layout::AxisSide::Bottom),
"Heatmap category intrinsic",
)?
.0;
Ok(hard + 2.0 * crate::layout::LABEL_CLEARANCE)
}
#[expect(
clippy::type_complexity,
reason = "visible and hidden-y summaries share one appearance-keyed analysis"
)]
pub(crate) fn heatmap_minima(
intrinsic: &crate::orchestrator::root::PlotIntrinsics,
input: &crate::orchestrator::root::PlotIntrinsicInputs<'_, '_>,
) -> Result<((f64, f64), (f64, f64)), Error> {
let theme = &input.request;
let axes = input
.registered
.axes
.coordinate()
.expect("prepared Heatmap axes");
let source = input.registered.source;
let crate::orchestrator::registration::PreparedFamily::Heatmap(prepared) =
&input.registered.family
else {
unreachable!("prepared Heatmap intrinsic family")
};
let diagnostic_scope = input.diagnostic_scope;
theme.text.prepared_font_environment()?;
let crate::orchestrator::root::PlotIntrinsics::Heatmap { value } = intrinsic else {
unreachable!("Heatmap intrinsic slot")
};
let _ = diagnostic_scope;
if let Some(cached) = value.get() {
if cached.matches_inputs(prepared, axes, input.registered, theme) {
return Ok((cached.minimum, cached.without_y_labels));
}
}
let mut minimum = (1.0_f64 / 1024.0, 1.0_f64 / 1024.0);
let levels = [0, 1].map(|side| {
axes.categories(side)
.expect("prepared Heatmap categories")
.raw
.as_slice()
});
let cell = intrinsic_cell_ink(prepared, levels, theme)?;
let columns = levels[0].len();
let base_column = (cell.0 / columns as f64).max(1.0 / 1024.0);
let mut column_hard =
crate::data::source::collect_source((0..columns).map(|_| Ok(base_column)))?;
let mut row_hard = crate::data::source::collect_source(
(0..levels[1].len()).map(|_| Ok(cell.1 / levels[1].len() as f64)),
)?;
let x_labels = &axes
.categories(0)
.ok_or_else(|| Error::Render("Heatmap x intrinsic labels missing".into()))?;
let y_labels = &axes
.categories(1)
.ok_or_else(|| Error::Render("Heatmap y intrinsic labels missing".into()))?;
let (x_labels, y_labels) = (&x_labels.ticks, &y_labels.ticks);
for (index, label) in x_labels.iter().enumerate().filter(|(index, _)| {
axes.axis(source, 0)
.authored()
.expect("coordinate authored intent")
.category_labels
.includes(*index, x_labels.len())
}) {
column_hard[index] =
column_hard[index].max(column_label_hard(prepared, &label.label, theme)?);
}
let mut y_label_hard =
crate::data::source::collect_source((0..y_labels.len()).map(|_| Ok(0.0_f64)))?;
for (index, label) in y_labels.iter().enumerate().filter(|(index, _)| {
axes.axis(source, 1)
.authored()
.expect("coordinate authored intent")
.category_labels
.includes(*index, y_labels.len())
}) {
if theme.text_layouts.ticks[1].as_ref().is_some_and(|layout| {
layout
.instructions()
.visibility
.as_ref()
.is_some_and(crate::TextVisibility::reduces)
}) {
continue;
}
let plan = theme.measure_appearance_text(
&label.label,
theme.tick_text_appearance(crate::layout::AxisSide::Left),
PhysicalTextAlignment::End,
"Heatmap row intrinsic",
)?;
if !plan.lines.is_empty() {
y_label_hard[index] = plan.occupied_size().1 + 2.0 * crate::layout::LABEL_CLEARANCE;
}
}
let eligible = crate::data::source::collect_source(
prepared
.value_labels
.iter()
.enumerate()
.filter(|(_, label)| label.is_some())
.map(|(index, _)| Ok(index)),
)?;
for (index, (source, _fill)) in prepared
.value_labels
.iter()
.zip(&prepared.fills)
.enumerate()
.filter_map(|(index, (source, fill))| Some((index, (source.as_deref()?, (*fill)?))))
{
let fill = prepared
.resolved_cell_fill(index, theme.marks.get_cells().get_fill_alpha())
.expect("prepared finite cell");
let appearance = theme.contrasting_text(fill)?;
let appearance =
crate::text::TextAppearance::from(appearance).layout(theme.text_layouts.value.as_ref());
if !appearance.includes(index, &eligible) {
continue;
}
let plan = theme.measure_appearance_text(
source,
appearance,
PhysicalTextAlignment::Center,
"heatmap value intrinsic",
)?;
let padding = theme
.annotations
.get_cells()
.value_padding(plan.style.font_size)?;
let hard_width = crate::layout::fit::intrinsic_widths(
theme,
source,
appearance,
"heatmap value intrinsic",
)?
.0;
if !plan.lines.is_empty() {
column_hard[index % columns] =
column_hard[index % columns].max(padding.width(hard_width)?);
row_hard[index / columns] =
row_hard[index / columns].max(padding.height(plan.occupied_size().1)?);
}
}
let total_columns = column_hard.into_iter().try_fold(0.0_f64, |total, width| {
let next = total + width;
if !next.is_finite() {
return Err(Error::Render(
"heatmap column hard widths overflowed".into(),
));
}
Ok(next)
})?;
minimum.0 = minimum.0.max(total_columns);
let default = theme.contrasting_text(theme.marks.get_cells().get_missing_fill())?;
let eligible = crate::data::source::collect_source(
prepared
.null_row_annotations
.iter()
.enumerate()
.filter(|(_, label)| label.is_some())
.map(|(index, _)| Ok(index)),
)?;
for (row, source) in prepared
.null_row_annotations
.iter()
.enumerate()
.filter_map(|(row, text)| Some((row, text.as_ref()?)))
{
let appearance = source.appearance(
crate::text::TextAppearance::from(default).layout(theme.text_layouts.null_row.as_ref()),
);
if !appearance.includes(row, &eligible) {
continue;
}
let hard = crate::layout::fit::intrinsic_widths(
theme,
source.as_str(),
appearance,
"heatmap fully-null row annotation",
)?
.0;
let unwrapped = theme.measure_appearance_text(
source.as_str(),
appearance,
source
.resolved_alignment(crate::TextAlignment::Center)
.into(),
"heatmap fully-null row annotation",
)?;
let padding = theme
.annotations
.get_cells()
.null_row_padding(appearance.theme.get_font_size())?;
minimum.0 = minimum.0.max(padding.width(hard)?);
if !unwrapped.lines.is_empty() {
row_hard[row] = row_hard[row].max(padding.height(unwrapped.occupied_size().1)?);
}
}
minimum.0 = minimum.0.max(cell.0);
let without_y_height = row_hard.iter().sum::<f64>().max(cell.1);
minimum.1 = row_hard
.iter()
.zip(&y_label_hard)
.map(|(row, label)| row.max(*label))
.sum::<f64>()
.max(cell.1);
let mut without_y_labels = (minimum.0, without_y_height);
for (side, title) in [
axes.label(input.registered, 0),
axes.label(input.registered, 1),
]
.into_iter()
.enumerate()
{
let Some(title) = title else { continue };
let hard = crate::layout::fit::intrinsic_widths(
theme,
title.as_str(),
title.appearance(&theme.typography.axes.axis_title),
"Heatmap axis title intrinsic",
)?
.0;
if side == 0 {
minimum.0 = minimum.0.max(hard);
without_y_labels.0 = without_y_labels.0.max(hard);
} else {
minimum.1 = minimum.1.max(hard);
without_y_labels.1 = without_y_labels.1.max(hard);
}
}
if value.get().is_none() {
let cache = HeatmapIntrinsicCache::new(
minimum,
without_y_labels,
prepared,
axes,
input.registered,
theme,
)?;
let _ = value.set(cache);
}
Ok((minimum, without_y_labels))
}
impl HeatmapData {
pub(crate) fn null_row_sources(&self) -> impl Iterator<Item = (usize, &crate::Text)> {
self.null_row_annotations
.iter()
.enumerate()
.filter_map(|(index, source)| source.as_ref().map(|source| (index, source)))
}
pub(crate) fn annotation_labels(
&self,
alpha: u8,
) -> impl Iterator<Item = (usize, &str, crate::theme::Color)> {
self.value_labels
.iter()
.enumerate()
.filter_map(move |(index, label)| {
Some((
index,
label.as_deref()?,
self.resolved_cell_fill(index, alpha)?,
))
})
}
}
pub(crate) fn measure(
chart: &PlotDescription,
context: &PlotMeasureContext<'_, '_>,
request: &crate::text::TextRequestContext<'_, '_>,
) -> Result<PlotRenderResult, crate::layout::fit::PlanError> {
let (prepared, levels) = retained_in(Some(context.registered))?;
let theme = request;
let minimum = intrinsic_body_minimum(
Some(context.registered),
context.intrinsic(),
context.operation,
theme,
)?;
let (body, colorbar) =
if crate::orchestrator::registration::continuous_guide_suppressed(Some(context.registered))
|| crate::orchestrator::registration::continuous_guide_use(
chart,
Some(context.registered),
) != crate::decorations::legend::discrete::scoped::GuideUse::Local
|| context.guides.use_kind
!= crate::decorations::legend::discrete::scoped::GuideUse::Local
{
(context.assignment.local.body, None)
} else {
let (body, guide) = guide::measure(
Some(context.registered),
context.assignment.local.body,
minimum.0,
theme,
&context
.fit_scope()
.child(crate::layout::AddressRole::OutsideGuideRight, 0)?,
context.assignment.local.content_height.then_some(context),
)?;
(body, Some(guide))
};
let mut annotations = None;
let mut columns = None;
let mut rows = None;
let layout = (|| -> Result<_, crate::layout::fit::PlanError> {
let x_labels = crate::data::source::collect_source(
context
.prepared_category_labels(0)
.ok_or_else(|| Error::Render("heatmap x labels require prepared axes".into()))?
.iter()
.map(|tick| crate::data::source::copy_source_string(&tick.label)),
)?;
let y_labels = crate::data::source::collect_source(
context
.prepared_category_labels(1)
.ok_or_else(|| Error::Render("heatmap y labels require prepared axes".into()))?
.iter()
.map(|tick| crate::data::source::copy_source_string(&tick.label)),
)?;
let wrap_labels = theme.text_layouts.ticks[1].as_ref().is_none_or(|layout| {
layout.instructions().wrapping != Some(crate::TextWrapping::NoSoftWrap)
});
let available = context.assignment.local.axis_label_width.max(1.0 / 1024.0);
if available <= 0.0 {
return Err(context
.fit_scope()
.failure(
crate::layout::fit::FitKind::TrackOrResidual,
"Heatmap y-axis labels have no assigned perpendicular capacity",
)
.into());
}
let mut natural = 1.0 / 1024.0;
let eligible = crate::data::source::collect_source(
chart
.y_axis_spec()
.category_labels
.indices(y_labels.len())
.map(Ok),
)?;
for (_, label) in y_labels.iter().enumerate().filter(|(index, _)| {
crate::orchestrator::registration::axis_tick_labels_visible(Some(context.registered), 1)
&& theme
.tick_text_appearance(crate::layout::AxisSide::Left)
.includes(*index, &eligible)
&& chart
.y_axis_spec()
.category_labels
.includes(*index, y_labels.len())
}) {
let plan = theme.measure_appearance_text(
label,
theme.tick_text_appearance(crate::layout::AxisSide::Left),
PhysicalTextAlignment::End,
"y-axis category label",
)?;
natural = f64::max(natural, plan.occupied_size().0.max(plan.fit_width()));
}
let tick_width = if natural <= available || !wrap_labels {
natural.min(available)
} else {
theme
.intrinsic_tick_label_width(
chart
.y_axis_spec()
.category_labels
.indices(y_labels.len())
.map(|index| y_labels[index].as_str()),
body.height(),
)?
.min(available)
};
let panel = body;
if panel.width() < minimum.0 || panel.height() < minimum.1 {
return Err(context.fit_scope().failure(crate::layout::fit::FitKind::SpecializedGeometry,
"heatmap: canvas is too small after reserving categorical labels and the colorbar; increase chart size"
).into());
}
let x_count = levels[0].len();
let y_count = levels[1].len();
let assigned = assigned_columns(
prepared,
levels,
x_labels.len(),
|index| &x_labels[index],
&chart.x_axis_spec().category_labels,
panel,
theme,
context.fit_scope(),
)?;
columns = Some(assigned);
let column_edges = columns.as_ref().expect("assigned Heatmap columns");
let mut measured = measure_annotations(
chart,
prepared,
levels,
column_edges,
theme,
context.fit_scope(),
)?;
let assigned = assigned_rows(
prepared,
levels,
&measured,
column_edges,
&y_labels,
&chart.y_axis_spec().category_labels,
crate::orchestrator::registration::axis_tick_labels_visible(
Some(context.registered),
1,
),
tick_width,
context,
request,
panel,
)?;
for (index, plan) in measured.values.iter_mut().enumerate() {
if let Some(text) = plan {
let row = index / x_count;
*text = crate::layout::text::font_fit::assign_height(
theme,
text,
assigned[row] - assigned[row + 1],
&context
.fit_scope()
.child(crate::layout::AddressRole::HeatmapValueLabel, index)?,
)?;
}
}
for (row, plan) in measured.rows.iter_mut().enumerate() {
if let Some(text) = plan {
*text = crate::layout::text::font_fit::assign_height(
theme,
text,
assigned[row] - assigned[row + 1],
&context
.fit_scope()
.child(crate::layout::AddressRole::HeatmapMergedRow, row)?,
)?;
}
}
crate::layout::text::font_fit::uniform_options(
theme,
&mut measured.values,
context.fit_scope(),
)?;
crate::layout::text::font_fit::uniform_options(
theme,
&mut measured.rows,
context.fit_scope(),
)?;
check_annotation_tracks(
prepared,
levels,
&measured,
column_edges,
&assigned,
theme,
context.fit_scope(),
)?;
annotations = Some(measured);
rows = Some(assigned);
let row_edges = rows.as_ref().expect("assigned Heatmap rows");
let x_ticks =
crate::data::source::collect_source(levels[0].iter().enumerate().map(|(index, _)| {
let middle = f64::midpoint(column_edges[index], column_edges[index + 1]);
let x = (middle - panel.x0) / panel.width() * x_count as f64;
Ok(AxisTick::new(x, ""))
}))?;
let y_ticks = crate::data::source::collect_source(y_labels.into_iter().enumerate().map(
|(index, label)| {
let middle = f64::midpoint(row_edges[index], row_edges[index + 1]);
let y = (middle - panel.y0) / panel.height() * y_count as f64;
Ok(AxisTick::new(y, label))
},
))?;
let axes = context
.registered
.axes
.coordinate()
.ok_or_else(|| Error::Render("Heatmap requires prepared native axes".into()))?;
let mut layout = Layout {
projection: crate::layout::Projection::new(
(0.0, x_count as f64),
(0.0, y_count as f64),
panel.as_tuple(),
),
x_axis: crate::layout::Axis {
label: axes.copy_label(context.registered, 0)?,
ticks: x_ticks,
tick_labels_visible: true,
category_labels: axes
.axis(context.registered.source, 0)
.authored()
.expect("coordinate authored intent")
.category_labels
.clone(),
explicit_ticks: false,
assigned_tick_width: None,
assigned_cells: None,
source: crate::layout::TickSource::SUPPLIED,
numeric: None,
},
y_axis: crate::layout::Axis {
label: axes.copy_label(context.registered, 1)?,
ticks: y_ticks,
tick_labels_visible: crate::orchestrator::registration::axis_tick_labels_visible(
Some(context.registered),
1,
),
category_labels: axes
.axis(context.registered.source, 1)
.authored()
.expect("coordinate authored intent")
.category_labels
.clone(),
explicit_ticks: false,
assigned_tick_width: None,
assigned_cells: None,
source: crate::layout::TickSource::SUPPLIED,
numeric: None,
},
};
layout.y_axis.assigned_tick_width = Some(tick_width);
layout.x_axis.assigned_cells = Some(crate::data::source::collect_source(
column_edges.windows(2).map(|edge| Ok((edge[0], edge[1]))),
)?);
layout.y_axis.assigned_cells = Some(crate::data::source::collect_source(
row_edges.windows(2).map(|edge| Ok((edge[1], edge[0]))),
)?);
for (tick, label) in layout.x_axis.ticks.iter_mut().zip(x_labels) {
tick.label = label;
}
Ok(layout)
})()?;
let panel = layout.projection.panel_box();
let columns = columns
.as_ref()
.ok_or_else(|| Error::Render("selected Heatmap has no assigned column tracks".into()))?;
let rows = rows
.as_ref()
.ok_or_else(|| Error::Render("selected Heatmap has no assigned row tracks".into()))?;
let annotations = annotations
.ok_or_else(|| Error::Render("selected Heatmap has no measured cell text".into()))?;
let mut payload = HeatmapPayload {
clip: panel,
cross_style: theme.marks.get_cells().get_missing_cross().backend_style(),
cells: select_cells(prepared, levels, columns, rows, theme, &annotations)?,
colorbar: colorbar
.as_ref()
.map(|guide| guide::select(guide, panel, theme))
.transpose()?,
};
payload.validate_annotation_ink(context.fit_scope())?;
let geometry = payload.geometry()?;
let mut result = PlotRenderResult::new(
layout,
crate::orchestrator::registration::guide_descriptor(Some(context.registered))?,
)
.with_resolved_axis_keys(
crate::data::source::collect_source(
levels[0]
.iter()
.map(|s| crate::data::source::copy_source_string(s)),
)?,
crate::data::source::collect_source(
levels[1]
.iter()
.map(|s| crate::data::source::copy_source_string(s)),
)?,
);
result.x_domain = crate::spec::AxisDomain::Categorical;
result
.numeric_presentation
.clone_from(&prepared.value_presentation);
result.y_domain = crate::spec::AxisDomain::Categorical;
result.axes = Some(result.layout.measure_axes_with_inputs(
theme,
context.fit_scope(),
context.semantic_inputs(crate::orchestrator::assignment::AxisPairScope::Pair)?,
|| crate::layout::CoordinateLayoutInputs::new(theme),
)?);
let texts = payload
.cells
.iter()
.filter_map(|cell| cell.text.as_ref())
.chain(payload.colorbar.iter().flat_map(|bar| {
bar.calls.iter().filter_map(|call| match call {
crate::decorations::legend::LegendCall::Text(text) => Some(text),
_ => None,
})
}));
for text in texts {
crate::data::source::reserve_source(&mut result.producer_text, 1)?;
result.producer_text.push(text.clone());
}
result.marks = Some(crate::data::source::source_arc(
crate::layout::interior::SelectedMarkPlan {
payload: crate::layout::interior::MarkPayload::Heatmap(payload),
geometry,
},
)?);
Ok(result)
}
fn category_levels(
chart: &crate::builder::PlotDescription,
data: crate::data::PlotData<'_, '_>,
x: &[String],
y: &[String],
) -> Result<[Vec<String>; 2], Error> {
match chart.heatmap_layout_spec() {
HeatmapLayout::Cartesian => Ok([
resolve_axis_order(
&crate::data::source::source_levels(x)?,
chart.x_category_order_spec(),
"x",
!data.is_full_plot(),
)?,
resolve_axis_order(
&crate::data::source::source_levels(y)?,
chart.y_category_order_spec(),
"y",
!data.is_full_plot(),
)?,
]),
layout => {
let observed = triangular_family_order(x, y, layout)?;
let family = resolve_triangular_order(chart, &observed, !data.is_full_plot())?;
let copy = crate::data::source::collect_source(
family
.iter()
.map(|level| crate::data::source::copy_source_string(level)),
)?;
Ok([copy, family])
}
}
}
pub(crate) fn prepare(
chart: &crate::builder::PlotDescription,
data: crate::data::PlotData<'_, '_>,
selection: ContinuousColorScale,
numeric: crate::numeric::source::NumericBinding<'_>,
materialization: crate::orchestrator::registration::ContinuousMaterialization,
) -> Result<(HeatmapData, [Vec<String>; 2]), Error> {
validate_options(chart)?;
let alpha = chart.prepare_alpha(AlphaTarget::Fill, AlphaUnitKind::Observation, &[])?;
let x_col = chart
.x_col()
.ok_or_else(|| Error::Data("heatmap: x category column not set".into()))?;
let y_col = chart
.y_col()
.ok_or_else(|| Error::Data("heatmap: y category column not set".into()))?;
let color_col = chart
.color_col()
.ok_or_else(|| Error::Data("heatmap: numeric color column not set".into()))?;
let x_values = normalized_categories(data, x_col, "x")?;
let y_values = normalized_categories(data, y_col, "y")?;
let fills = crate::data::nullable_finite_f64(data, color_col, "heatmap")?;
let highlights = chart
.heatmap_highlight_col()
.map(|column| crate::data::strict_bool(data, column, "heatmap"))
.transpose()?;
if x_values.is_empty() || x_values.len() != y_values.len() || x_values.len() != fills.len() {
return Err(Error::Data(
"heatmap: x/y/color columns must have equal non-zero lengths".into(),
));
}
if highlights
.as_ref()
.is_some_and(|values| values.len() != x_values.len())
{
return Err(Error::Data(
"heatmap: highlight column must align with x/y/color columns".into(),
));
}
let heatmap_layout = chart.heatmap_layout_spec();
let [x_levels, y_levels] = category_levels(chart, data, &x_values, &y_values)?;
let cell_count = x_levels
.len()
.checked_mul(y_levels.len())
.ok_or_else(|| Error::Data("heatmap: observed grid cell count overflowed".into()))?;
if cell_count > MAX_CELLS {
return Err(Error::Data(format!(
"heatmap: observed grid has {cell_count} cells, exceeding the safety limit of {MAX_CELLS}"
)));
}
let explicit_triangular_order = heatmap_layout != HeatmapLayout::Cartesian
&& (!matches!(chart.x_category_order_spec(), CategoryOrder::Observed)
|| !matches!(chart.y_category_order_spec(), CategoryOrder::Observed));
let mut x_indices = crate::data::source::collect_source(
x_levels
.iter()
.enumerate()
.map(|(index, level)| Ok((level.as_str(), index))),
)?;
let mut y_indices = crate::data::source::collect_source(
y_levels
.iter()
.enumerate()
.map(|(index, level)| Ok((level.as_str(), index))),
)?;
crate::data::source::sort_source_by(&mut x_indices, |left, right| left.0.cmp(right.0));
crate::data::source::sort_source_by(&mut y_indices, |left, right| left.0.cmp(right.0));
data.validate_row_source()?;
let mut seen = crate::data::source::collect_source((0..cell_count).map(|_| Ok(false)))?;
let mut cells = crate::data::source::collect_source((0..cell_count).map(|_| Ok(None)))?;
let mut fill_alphas = crate::data::source::collect_source((0..cell_count).map(|_| Ok(None)))?;
let mut highlighted = crate::data::source::collect_source((0..cell_count).map(|_| Ok(false)))?;
for (row, ((x, y), fill)) in x_values.iter().zip(&y_values).zip(&fills).enumerate() {
let mut x_index = x_indices[x_indices
.binary_search_by_key(&x.as_str(), |entry| entry.0)
.expect("observed x level")]
.1;
let mut y_index = y_indices[y_indices
.binary_search_by_key(&y.as_str(), |entry| entry.0)
.expect("observed y level")]
.1;
if explicit_triangular_order && !active_cell(heatmap_layout, x_index, y_index) {
std::mem::swap(&mut x_index, &mut y_index);
}
let index = y_index * x_levels.len() + x_index;
if std::mem::replace(&mut seen[index], true) {
let source_row = data.source_row(row);
return Err(Error::Data(format!(
"heatmap: duplicate normalized (x, y) pair ({x:?}, {y:?}) at source row {source_row}"
)));
}
if !active_cell(heatmap_layout, x_index, y_index) {
let source_row = data.source_row(row);
return Err(Error::Data(format!(
"heatmap: normalized (x, y) pair ({x:?}, {y:?}) at source row {source_row} is outside the active {heatmap_layout:?} layout"
)));
}
cells[y_index * x_levels.len() + x_index] = *fill;
if fill.is_some() {
fill_alphas[index] = Some(ObservationAlpha::resolve(data, &alpha, row)?);
}
highlighted[y_index * x_levels.len() + x_index] =
highlights.as_ref().is_some_and(|values| values[row]);
}
let null_row_annotations = resolve_null_row_annotations(
chart,
heatmap_layout,
&y_levels,
&cells,
&highlighted,
x_levels.len(),
)?;
let mut finite = Vec::new();
crate::data::source::reserve_source(&mut finite, fills.len())?;
finite.extend(fills.iter().flatten().copied());
if finite.is_empty() && selection.get_domain().is_none() {
return Err(Error::Data(
"heatmap: color column must contain at least one finite value unless explicit color limits are set"
.into(),
));
}
let domain = color_domain(&finite, &selection)?;
let color = PreparedHeatmapColor::resolve(domain, selection)?;
let resolved_fills = crate::data::source::collect_source(
cells
.iter()
.map(|value| value.map(|value| color.scale.color(value)).transpose()),
)?;
let has_missing = cells.iter().enumerate().any(|(index, value)| {
let x_index = index % x_levels.len();
let y_index = index / x_levels.len();
active_cell(heatmap_layout, x_index, y_index) && value.is_none()
});
let value_presentation = if let HeatmapAnnotations::Values(format) =
chart.heatmap_annotations_spec()
{
let format = crate::data::source::source_arc(format.try_copy()?)?;
numeric.check_data(data)?;
let values = crate::numeric::source::collect_values(
cells.iter().flatten().count(),
cells
.iter()
.enumerate()
.filter_map(|(index, value)| value.map(|value| (index, value)))
.map(|(index, value)| {
Ok(crate::numeric::context::ContextValue {
operand: crate::numeric::operand::Operand::number(value, false),
identity: crate::numeric::context::Identity::Heatmap(
crate::data::source::copy_source_string(
&y_levels[index / x_levels.len()],
)?,
crate::data::source::copy_source_string(
&x_levels[index % x_levels.len()],
)?,
),
})
}),
)?;
let info = numeric.context(
crate::NumericRole::HeatmapValues,
crate::NumericPurpose::ActualSet,
values,
None,
)?;
let set = format
.materialize(numeric.operation, info, false)?
.ok_or_else(|| {
Error::Data("Heatmap value labels are not unique at configured precision".into())
})?;
Some(crate::data::source::source_arc(
crate::numeric::set::NumericPresentation {
format: Some(format),
set,
},
)?)
} else {
None
};
let mut selected = value_presentation
.as_ref()
.map(|presentation| presentation.set.labels.iter());
let value_labels = crate::data::source::collect_source(cells.iter().map(|value| {
if value.is_some() && value_presentation.is_some() {
let label = selected
.as_mut()
.and_then(Iterator::next)
.ok_or_else(|| Error::Render("prepared Heatmap numeric source missing".into()))?;
crate::data::source::copy_source_string(label).map(Some)
} else {
Ok(None)
}
}))?;
let value_owners = crate::data::source::collect_source(value_labels.iter().enumerate().map(
|(index, label)| {
use std::fmt::Write;
if label.is_none() {
return Ok(None);
}
let mut owner = String::new();
crate::data::source::reserve_source_string(&mut owner, 44)?;
write!(owner, "heatmap value annotation {index}")
.expect("reserved annotation identity");
Ok(Some(owner))
},
))?;
let color_format = chart.color_label_format_spec().cloned();
numeric.check_data(data)?;
let colorbar_values =
if materialization == crate::orchestrator::registration::ContinuousMaterialization::Local {
Some(crate::decorations::legend::descriptor::domain_presentation(
(domain.minimum, domain.maximum),
domain.center,
color_format.as_deref().map(|format| {
(
format,
numeric,
if color.selection.get_domain().is_some() {
crate::NumericRangeOrigin::Explicit
} else {
crate::NumericRangeOrigin::Automatic
},
)
}),
)?)
} else {
None
};
let mut prepared = HeatmapData {
color_label: chart.prepare_color_label(None)?,
layout: heatmap_layout,
value_labels,
value_owners,
value_presentation,
colorbar_values,
color_format,
cells,
fills: resolved_fills,
fill_binding: chart.alpha_inputs().get(AlphaTarget::Fill).clone(),
fill_alphas: crate::data::source::source_arc(fill_alphas)?,
highlighted,
null_row_annotations,
has_missing,
color,
colorbar_stops: Vec::new(),
};
prepared.colorbar_stops = prepared.color.guide_stops(&prepared.guide_samples()?)?;
Ok((prepared, [x_levels, y_levels]))
}
fn validate_options(chart: &crate::builder::PlotDescription) -> Result<(), Error> {
if [chart.x_axis_spec(), chart.y_axis_spec()]
.iter()
.any(|spec| {
spec.window.is_some()
|| spec.exact_ticks.is_some()
|| spec.format.is_some()
|| !spec.generation.is_automatic()
})
{
return Err(Error::Data(
"heatmap: categorical axis limits are not supported".into(),
));
}
if let Some(hue_col) = chart.hue_col() {
return Err(Error::Data(format!(
"heatmap: hue column {hue_col:?} is not supported; use color for numeric fill"
)));
}
if !matches!(chart.category_order_spec(), CategoryOrder::Observed) {
return Err(Error::Data(
"heatmap: primary category order is ambiguous; use x_category_order(...) and y_category_order(...)"
.into(),
));
}
for (axis, order) in [
("x", chart.x_category_order_spec()),
("y", chart.y_category_order_spec()),
] {
if let CategoryOrder::Value(sort) = order {
return Err(Error::Data(format!(
"heatmap: {axis} categories cannot use ValueSort::{sort:?}; each category maps to multiple cells, so supply an explicit order"
)));
}
}
Ok(())
}
fn resolve_null_row_annotations(
chart: &crate::builder::PlotDescription,
layout: HeatmapLayout,
y_levels: &[String],
cells: &[Option<f64>],
highlighted: &[bool],
x_count: usize,
) -> Result<Vec<Option<crate::Text>>, Error> {
let annotations = chart.heatmap_null_row_annotations_spec();
let mut resolved = crate::data::source::collect_source(y_levels.iter().map(|_| Ok(None)))?;
if annotations.is_empty() {
return Ok(resolved);
}
if layout != HeatmapLayout::Cartesian {
return Err(Error::Data(
"heatmap: fully-null row annotations require HeatmapLayout::Cartesian".into(),
));
}
let mut seen = Vec::new();
crate::data::source::reserve_source(&mut seen, annotations.len())?;
for annotation in annotations {
let row_label = crate::data::source::normalize_source_string(annotation.row_label())?;
if seen.contains(&row_label) {
return Err(Error::Data(format!(
"heatmap: duplicate fully-null row annotation for normalized y category {row_label:?}"
)));
}
let y_index = y_levels
.iter()
.position(|level| level == &row_label)
.ok_or_else(|| {
Error::Data(format!(
"heatmap: fully-null row annotation targets unknown normalized y category {row_label:?}"
))
})?;
let row = &cells[y_index * x_count..(y_index + 1) * x_count];
if row.iter().any(Option::is_some) {
return Err(Error::Data(format!(
"heatmap: row {row_label:?} must be fully null before it can receive a merged row annotation"
)));
}
if highlighted[y_index * x_count..(y_index + 1) * x_count]
.iter()
.any(|value| *value)
{
return Err(Error::Data(format!(
"heatmap: fully-null row {row_label:?} cannot combine a merged annotation with true cell highlights"
)));
}
resolved[y_index] = Some(annotation.content().try_copy()?);
seen.push(row_label);
}
Ok(resolved)
}
fn normalized_categories(
data: crate::data::PlotData<'_, '_>,
column: &str,
axis: &str,
) -> Result<Vec<String>, Error> {
crate::data::source::collect_source(
crate::data::strict_strings(data, column, "heatmap")?
.into_iter()
.enumerate()
.map(|(row, value)| {
let normalized = crate::data::source::normalize_source_string(&value)?;
if normalized.is_empty() {
let source_row = data.source_row(row);
Err(Error::Data(format!(
"heatmap: {axis} category label is empty at source row {source_row}"
)))
} else {
Ok(normalized)
}
}),
)
}
fn resolve_axis_order(
observed: &[String],
order: &CategoryOrder,
axis: &str,
is_subset: bool,
) -> Result<Vec<String>, Error> {
match order {
CategoryOrder::Observed => crate::data::source::collect_source(observed.iter().map(|level| crate::data::source::copy_source_string(level))),
CategoryOrder::Explicit(requested) => {
let requested = crate::data::source::collect_source(requested.iter().map(|level| crate::data::source::normalize_source_string(level)))?;
if requested.iter().any(String::is_empty) {
return Err(Error::Data(format!(
"heatmap: explicit {axis} category order contains an empty normalized level"
)));
}
crate::data::explicit_category_order(
observed,
&requested,
&format!("heatmap {axis} category order"),
is_subset,
)
}
CategoryOrder::Value(sort) => Err(Error::Data(format!(
"heatmap: {axis} categories cannot use ValueSort::{sort:?}; each category maps to multiple cells, so supply an explicit order"
))),
}
}
fn resolve_triangular_order(
chart: &crate::builder::PlotDescription,
observed: &[String],
is_subset: bool,
) -> Result<Vec<String>, Error> {
match (chart.x_category_order_spec(), chart.y_category_order_spec()) {
(CategoryOrder::Observed, CategoryOrder::Observed) => crate::data::source::collect_source(
observed
.iter()
.map(|level| crate::data::source::copy_source_string(level)),
),
(CategoryOrder::Observed, y) => resolve_axis_order(observed, y, "y", is_subset),
(x, CategoryOrder::Observed) => resolve_axis_order(observed, x, "x", is_subset),
(x, y) => {
let x = resolve_axis_order(observed, x, "x", is_subset)?;
let y = resolve_axis_order(observed, y, "y", is_subset)?;
if x != y {
return Err(Error::Data(
"heatmap: triangular x and y category orders must resolve to the same family order"
.into(),
));
}
Ok(x)
}
}
}
fn triangular_family_order(
x_values: &[String],
y_values: &[String],
layout: HeatmapLayout,
) -> Result<Vec<String>, Error> {
let mut levels = Vec::new();
for (x, y) in x_values.iter().zip(y_values) {
let pair = match layout {
HeatmapLayout::UpperTriangle { .. } => [y, x],
HeatmapLayout::LowerTriangle { .. } | HeatmapLayout::Cartesian => [x, y],
};
for value in pair {
if !levels.contains(value) {
crate::data::source::reserve_source(&mut levels, 1)?;
levels.push(crate::data::source::copy_source_string(value)?);
}
}
}
Ok(levels)
}
const fn active_cell(layout: HeatmapLayout, x_index: usize, y_index: usize) -> bool {
match layout {
HeatmapLayout::Cartesian => true,
HeatmapLayout::LowerTriangle { diagonal } => {
y_index > x_index || (diagonal && y_index == x_index)
}
HeatmapLayout::UpperTriangle { diagonal } => {
x_index > y_index || (diagonal && x_index == y_index)
}
}
}
fn color_domain(
values: &[f64],
selection: &ContinuousColorScale,
) -> Result<ResolvedColorDomain, Error> {
let domain = selection.prepared_domain(values, "heatmap")?;
Ok(ResolvedColorDomain {
minimum: domain.minimum(),
maximum: domain.maximum(),
center: selection.get_center(),
})
}
#[derive(Debug)]
pub(crate) struct HeatmapPayload {
clip: PixelRect,
cross_style: LineStyle,
cells: Vec<CellPayload>,
colorbar: Option<crate::decorations::legend::LegendDrawPlan>,
}
#[derive(Debug)]
struct CellPayload {
corners: [Point2D; 4],
fill: FillStyle,
text: Option<PlacedText>,
text_owner: Option<(bool, usize)>,
cross: Option<[[Point2D; 2]; 2]>,
}
impl CellPayload {
fn draw(&self, backend: &mut dyn Backend, cross_style: &LineStyle) -> Result<(), Error> {
backend.draw_polygon(&self.corners, &self.fill)?;
if let Some(text) = &self.text {
text.draw(backend)?;
}
if let Some(cross) = &self.cross {
for line in cross {
backend.draw_line(line, cross_style)?;
}
}
Ok(())
}
fn geometry(
&self,
geometry: &mut crate::render::geometry::GeometrySnapshot,
clip: Option<PixelRect>,
cross_style: &LineStyle,
) -> Result<(), Error> {
geometry.polygon(&self.corners, &self.fill, clip)?;
if let Some(text) = &self.text {
geometry.text(text.placement, &text.plan, clip)?;
}
if let Some(cross) = &self.cross {
for line in cross {
geometry.line(line, cross_style, clip)?;
}
}
Ok(())
}
}
impl HeatmapPayload {
fn validate_annotation_ink(
&mut self,
scope: &crate::layout::fit::FitScope,
) -> Result<(), crate::layout::fit::PlanError> {
for (index, cell) in self.cells.iter_mut().enumerate() {
let Some(text) = &mut cell.text else { continue };
let containing = PixelRect::new(
cell.corners[0].x,
cell.corners[2].x,
cell.corners[0].y,
cell.corners[2].y,
);
text.plan = crate::layout::text::placement::contain(
text.plan.clone(),
text.placement,
containing,
&scope.child(crate::layout::AddressRole::HeatmapCell, index)?,
)?;
}
for merged in [false, true] {
let members = crate::data::source::collect_source(
self.cells
.iter()
.enumerate()
.filter(|(_, cell)| {
cell.text_owner.is_some_and(|(row, _)| row == merged) && cell.text.is_some()
})
.map(|(index, _)| Ok(index)),
)?;
let indices =
crate::data::source::collect_source(members.iter().map(|&index| {
Ok(self.cells[index].text_owner.expect("selected text owner").1)
}))?;
let mut text = crate::data::source::collect_source(members.iter().map(|&index| {
Ok(self.cells[index]
.text
.as_ref()
.expect("selected text")
.clone())
}))?;
crate::layout::text::visibility::apply(&mut text, &indices, self.clip, merged, scope)?;
for (&index, text) in members.iter().zip(text) {
self.cells[index].text = Some(text);
}
}
Ok(())
}
pub(crate) fn draw(&self, backend: &mut dyn Backend) -> Result<(), Error> {
with_clip(backend, self.clip, |backend| {
for cell in &self.cells {
cell.draw(backend, &self.cross_style)?;
}
Ok(())
})?;
if let Some(bar) = &self.colorbar {
bar.draw(backend)?;
}
Ok(())
}
fn geometry(&self) -> Result<crate::render::geometry::GeometrySnapshot, Error> {
let mut geometry = crate::render::geometry::GeometrySnapshot::new();
let clip = crate::render::backend::validate_clip_region(self.clip)?.panel();
for cell in &self.cells {
cell.geometry(&mut geometry, Some(clip), &self.cross_style)?;
}
if let Some(bar) = &self.colorbar {
bar.geometry(&mut geometry)?;
}
Ok(geometry)
}
}
fn select_cells(
prepared: &HeatmapData,
levels: [&[String]; 2],
columns: &[f64],
rows: &[f64],
theme: &crate::text::TextRequestContext<'_, '_>,
annotations: &AnnotationPlans,
) -> Result<Vec<CellPayload>, Error> {
let x_count = levels[0].len();
let y_count = levels[1].len();
let mut cross_width = None;
let mut cells = Vec::new();
crate::data::source::reserve_source(&mut cells, prepared.cells.len())?;
for y_index in 0..y_count {
let (bottom, top) = (rows[y_index + 1], rows[y_index]);
if let Some(annotation) = &annotations.rows[y_index] {
let (left, right) = (columns[0], columns[x_count]);
let center = f64::midpoint(left, right).round();
let half = track_pixels(
crate::render::backend::nearest_even_text_half(annotation.width_quanta)
.ok_or_else(|| Error::Render("heatmap row text half extent overflow".into()))?,
);
let (anchor, x) = match annotation.style.alignment {
PhysicalTextAlignment::Start => (TextAnchor::Start, center - half),
PhysicalTextAlignment::Center => (TextAnchor::Middle, center),
PhysicalTextAlignment::End => (TextAnchor::End, center + annotation.width() - half),
};
cells.push(CellPayload {
corners: [
Point2D::new(left, bottom),
Point2D::new(right, bottom),
Point2D::new(right, top),
Point2D::new(left, top),
],
fill: crate::theme::FixedFillTheme::new(
theme.marks.get_cells().get_missing_fill(),
theme.marks.get_cells().get_missing_row_outline().cloned(),
)?
.backend_style(),
cross: None,
text_owner: Some((true, y_index)),
text: Some(PlacedText {
plan: annotation.clone(),
placement: crate::layout::text::placement::resolve(
crate::render::backend::PlannedTextPlacement::middle(
Point2D::new(x, f64::midpoint(bottom, top).round()),
anchor,
0.0,
),
annotation,
Some(PixelRect::new(left, right, bottom, top)),
)?,
}),
});
continue;
}
for x_index in 0..x_count {
if !active_cell(prepared.layout, x_index, y_index) {
continue;
}
let (left, right) = (columns[x_index], columns[x_index + 1]);
let value = prepared.cells[y_index * x_count + x_index];
let highlighted = prepared.highlighted[y_index * x_count + x_index];
let color = prepared
.resolved_cell_fill(
y_index * x_count + x_index,
theme.marks.get_cells().get_fill_alpha(),
)
.unwrap_or(theme.marks.get_cells().get_missing_fill());
let mut cell = CellPayload {
corners: [
Point2D::new(left, bottom),
Point2D::new(right, bottom),
Point2D::new(right, top),
Point2D::new(left, top),
],
fill: crate::theme::FixedFillTheme::new(
color,
if highlighted {
Some(
theme
.annotations
.get_cells()
.get_highlight_outline()
.clone(),
)
} else {
theme.marks.get_cells().get_cell_outline().cloned()
},
)?
.backend_style(),
text: None,
text_owner: None,
cross: None,
};
if value.is_some() {
if let Some(plan) = &annotations.values[y_index * x_count + x_index] {
cell.text_owner = Some((false, y_index * x_count + x_index));
cell.text = Some(PlacedText::middle(
plan.clone(),
Point2D::new(
f64::midpoint(left, right).round(),
f64::midpoint(bottom, top).round(),
),
TextAnchor::Middle,
0.0,
)?);
}
} else {
let stroke = if let Some(stroke) = cross_width {
stroke
} else {
let stroke = crate::render::geometry::line_ink_width(
&theme.marks.get_cells().get_missing_cross().backend_style(),
)?;
cross_width = Some(stroke);
stroke
};
let metric = crate::theme::LocalMetric::scalar(
crate::layout::kernel::LayoutScalar::from_quanta(
crate::layout::kernel::nearest_spatial_quanta(
(right - left).min(top - bottom),
)?,
),
)?;
let inset = theme
.marks
.get_cells()
.get_missing_cross_inset()
.product(metric)
.pixels_rounded(crate::layout::kernel::SpatialRounding::NearestEven)?
.clamp(1.0, 4.0)
.max(stroke / 2.0 + 1.0 / 1024.0);
cell.cross = Some([
[
Point2D::new(left + inset, bottom + inset),
Point2D::new(right - inset, top - inset),
],
[
Point2D::new(left + inset, top - inset),
Point2D::new(right - inset, bottom + inset),
],
]);
}
cells.push(cell);
}
}
Ok(cells)
}
struct AnnotationPlans {
values: Vec<Option<crate::layout::ResolvedTextPlan>>,
rows: Vec<Option<crate::layout::ResolvedTextPlan>>,
}
fn measure_annotations(
chart: &PlotDescription,
prepared: &HeatmapData,
levels: [&[String]; 2],
columns: &[f64],
theme: &crate::text::TextRequestContext<'_, '_>,
scope: &crate::layout::fit::FitScope,
) -> Result<AnnotationPlans, crate::layout::fit::PlanError> {
use crate::layout::fit::{self, FitFailure, FitKind, FitOutcome};
use crate::layout::AddressRole;
let mut miss = None;
let mut plans = AnnotationPlans {
values: Vec::new(),
rows: Vec::new(),
};
plans
.values
.try_reserve_exact(prepared.cells.len())
.map_err(|_| Error::Render("heatmap value plans allocation failed".into()))?;
plans
.rows
.try_reserve_exact(prepared.null_row_annotations.len())
.map_err(|_| Error::Render("heatmap row plans allocation failed".into()))?;
plans.values.resize(prepared.cells.len(), None);
plans.rows.resize(prepared.null_row_annotations.len(), None);
if let HeatmapAnnotations::Values(_) = chart.heatmap_annotations_spec() {
let eligible = crate::data::source::collect_source(
prepared
.value_labels
.iter()
.enumerate()
.filter(|(_, label)| label.is_some())
.map(|(index, _)| Ok(index)),
)?;
for (index, value) in prepared.cells.iter().enumerate() {
let Some(_) = value else { continue };
let column = index % levels[0].len();
let cell_width = columns[column + 1] - columns[column];
let source = prepared.value_labels[index]
.as_deref()
.ok_or_else(|| Error::Render("heatmap value text identity missing".into()))?;
let fill = prepared
.resolved_cell_fill(index, theme.marks.get_cells().get_fill_alpha())
.ok_or_else(|| {
Error::Render("heatmap value has no prepared resolved fill".into())
})?;
let assigned_appearance = theme.theme.clone();
let mut assigned_theme = crate::text::TextRequestContext {
theme: &assigned_appearance,
..*theme
};
let geometry = theme.annotations.get_cells();
let padding = (
geometry.get_value_horizontal_padding().clone(),
geometry.get_value_vertical_padding().clone(),
);
assigned_theme.trial.padding = Some(&padding);
let appearance = assigned_theme.contrasting_text(fill)?;
let owner = prepared.value_owners[index]
.as_deref()
.ok_or_else(|| Error::Render("heatmap value owner missing".into()))?;
let cell_scope = scope.child(AddressRole::HeatmapValueLabel, index)?;
let measured = fit::measured(crate::layout::text::request::measure_occurrence(
&assigned_theme,
source,
crate::text::TextAppearance::from(appearance)
.layout(theme.text_layouts.value.as_ref()),
crate::layout::maximum_width_quanta(cell_width)
.map_err(|error| Error::Render(error.to_string()))?,
PhysicalTextAlignment::Center,
owner,
&cell_scope,
(index, &eligible),
));
match measured {
Ok(plan) => plans.values[index] = Some(plan),
Err(crate::layout::fit::PlanError::Fit(failure)) => {
FitFailure::retain_least(&mut miss, failure);
}
Err(error) => return Err(error),
}
}
}
let eligible = crate::data::source::collect_source(
prepared
.null_row_annotations
.iter()
.enumerate()
.filter(|(_, label)| label.is_some())
.map(|(index, _)| Ok(index)),
)?;
for (y_index, annotation) in prepared.null_row_annotations.iter().enumerate() {
let Some(annotation) = annotation else {
continue;
};
let appearance = annotation.appearance(
crate::text::TextAppearance::from(
theme.contrasting_text(theme.marks.get_cells().get_missing_fill())?,
)
.layout(theme.text_layouts.null_row.as_ref()),
);
let padding = theme
.annotations
.get_cells()
.null_row_padding(appearance.theme.get_font_size())?;
let available_width = padding.available_width(columns[columns.len() - 1] - columns[0])?;
let assigned_appearance = theme.theme.clone();
let mut assigned_theme = crate::text::TextRequestContext {
theme: &assigned_appearance,
..*theme
};
let geometry = theme.annotations.get_cells();
let padding = (
geometry.get_null_row_horizontal_padding().clone(),
geometry.get_null_row_vertical_padding().clone(),
);
assigned_theme.trial.padding = Some(&padding);
let row_scope = scope.child(AddressRole::HeatmapMergedRow, y_index)?;
let block = match fit::outcome(fit::measured(
crate::layout::text::request::measure_occurrence(
&assigned_theme,
annotation.as_str(),
appearance,
crate::layout::maximum_width_quanta(columns[columns.len() - 1] - columns[0])
.map_err(|error| Error::Render(error.to_string()))?,
annotation
.resolved_alignment(crate::TextAlignment::Center)
.into(),
"heatmap fully-null row annotation",
&row_scope,
(y_index, &eligible),
),
))? {
FitOutcome::Measured(plan) => TextBlock::from_resolved(plan),
FitOutcome::Miss(failure) => {
FitFailure::retain_least(&mut miss, failure);
continue;
}
};
if block.width() > available_width {
FitFailure::retain_least(
&mut miss,
row_scope.failure(
FitKind::Text,
"heatmap: wrapped fully-null row annotation exceeds its assigned Grid width",
),
);
}
plans.rows[y_index] = Some(block.plan().clone());
}
crate::layout::text::font_fit::uniform_options(theme, &mut plans.values, scope)?;
crate::layout::text::font_fit::uniform_options(theme, &mut plans.rows, scope)?;
match miss {
Some(failure) => Err(failure.into()),
None => Ok(plans),
}
}
fn check_annotation_tracks(
prepared: &HeatmapData,
levels: [&[String]; 2],
annotations: &AnnotationPlans,
columns: &[f64],
rows: &[f64],
theme: &crate::text::TextRequestContext<'_, '_>,
scope: &crate::layout::fit::FitScope,
) -> Result<(), crate::layout::fit::PlanError> {
use crate::layout::fit::{FitFailure, FitKind};
use crate::layout::AddressRole;
let mut miss = None;
let count = levels[0].len();
for (index, plan) in annotations.values.iter().enumerate() {
let Some(plan) = plan else { continue };
let col = index % count;
let row = index / count;
let width = columns[col + 1] - columns[col];
let height = rows[row] - rows[row + 1];
let padding = theme
.annotations
.get_cells()
.value_padding(plan.style.font_size)?;
let strict = plan
.request
.as_ref()
.and_then(|request| request.instructions().overflow.as_ref())
.is_none_or(|overflow| overflow.failure == crate::TextFailure::Error);
if strict
&& !plan.lines.is_empty()
&& (width < padding.width(plan.occupied_size().0)?
|| height < padding.height(plan.occupied_size().1)?)
{
let precision = prepared
.value_presentation
.as_ref()
.ok_or_else(|| Error::Render("heatmap value presentation missing".into()))?
.set
.precision;
FitFailure::retain_least(&mut miss, scope.child(AddressRole::HeatmapValueLabel, index)?.failure(FitKind::Text, format!(
"heatmap: cell {col},{row} is too small for required value annotation at precision {precision}; increase chart size or hide annotations"
)));
}
}
for (row, plan) in annotations.rows.iter().enumerate() {
let Some(plan) = plan else { continue };
let padding = theme
.annotations
.get_cells()
.null_row_padding(plan.style.font_size)?;
let strict = plan
.request
.as_ref()
.and_then(|request| request.instructions().overflow.as_ref())
.is_none_or(|overflow| overflow.failure == crate::TextFailure::Error);
if strict
&& !plan.lines.is_empty()
&& (rows[row] - rows[row + 1] < padding.height(plan.occupied_size().1)?
|| columns[columns.len() - 1] - columns[0]
< padding.width(plan.occupied_size().0)?)
{
FitFailure::retain_least(&mut miss, scope.child(AddressRole::HeatmapMergedRow, row)?.failure(FitKind::Text,
"heatmap: required merged row text exceeds its assigned row; increase chart width or height"));
}
}
miss.map_or(Ok(()), |failure| Err(failure.into()))
}
mod guide;