use dioxus::prelude::*;
use super::activation::{ChartDatumActivation, DatumAction};
use super::format::js_number;
use super::frame::{
ChartFrame, ChartPresentation, activate_on_click, datum_actions, plot_label, render_frame,
serialized,
};
use super::geometry::{HeatmapColorScale, HeatmapDatum, heatmap_color, heatmap_model};
use super::model::{ChartBounds, ChartLabels, format_with};
use super::scale::CategoryTick;
use crate::class::{Glass, compose};
#[derive(Props, Clone, PartialEq)]
pub struct HeatmapProps {
#[props(default)]
pub data: Vec<HeatmapDatum>,
#[props(default)]
pub color_scale: HeatmapColorScale,
#[props(default)]
pub midpoint: f64,
pub domain: Option<ChartBounds>,
pub format_value: Option<Callback<f64, String>>,
pub labels: Option<ChartLabels>,
#[props(default = true)]
pub show_legend: bool,
#[props(default = true)]
pub show_table: bool,
pub on_datum_activate: Option<EventHandler<ChartDatumActivation>>,
#[props(into)]
pub heading: Option<String>,
#[props(into)]
pub description: Option<String>,
#[props(into)]
pub value: Option<String>,
#[props(into)]
pub summary: Option<String>,
#[props(into)]
pub caption: Option<String>,
#[props(default)]
pub presentation: ChartPresentation,
#[props(default, into)]
pub glass: Glass,
#[props(into)]
pub class: Option<String>,
#[props(extends = GlobalAttributes, extends = figure)]
pub attributes: Vec<Attribute>,
}
struct CellView {
key: String,
width: String,
height: String,
x: String,
y: String,
style: String,
title: String,
missing: Option<(String, String)>,
column: String,
row: String,
value: String,
action: Option<DatumAction>,
}
#[component]
#[allow(clippy::too_many_lines)]
pub fn Heatmap(props: HeatmapProps) -> Element {
let labels = props.labels.clone().unwrap_or_default();
let format_value = props.format_value.as_ref();
let model = heatmap_model(&props.data, props.color_scale, props.midpoint, props.domain);
let on_activate = props.on_datum_activate;
let available = model
.cells
.iter()
.filter(|cell| cell.datum.finite_value().is_some())
.count();
let has_cells = !model.cells.is_empty();
let cells: Vec<CellView> = model
.cells
.iter()
.map(|cell| {
let datum = &cell.datum;
let column = datum.x_label.clone().unwrap_or_else(|| datum.x.display());
let row = datum.y_label.clone().unwrap_or_else(|| datum.y.display());
let value_text = datum
.label
.clone()
.unwrap_or_else(|| format_with(format_value, datum.value.unwrap_or(0.0)));
let missing = datum.finite_value().is_none();
let action = on_activate.and_then(|_| {
(!missing)
.then(|| {
DatumAction::new(
ChartDatumActivation::heatmap(
&datum.x,
&datum.y,
datum.value,
datum.id.as_ref(),
),
&format!("{column} · {row}: {value_text}"),
&labels,
)
})
.flatten()
});
CellView {
key: format!("[{},{}]", datum.x.json(), datum.y.json()),
width: js_number((cell.width - 2.0).max(0.0)),
height: js_number((cell.height - 2.0).max(0.0)),
x: js_number(cell.x + 1.0),
y: js_number(cell.y + 1.0),
style: format!(
"fill:{}",
heatmap_color(datum.value, model.domain, props.color_scale, model.midpoint)
),
title: format!(
"{column} · {row}: {}",
if missing {
labels.not_available.clone()
} else {
value_text.clone()
}
),
missing: missing.then(|| {
(
js_number(cell.x + cell.width / 2.0),
js_number(cell.y + cell.height / 2.0),
)
}),
value: datum.label.clone().unwrap_or_else(|| {
datum.finite_value().map_or_else(
|| labels.not_available.clone(),
|value| format_with(format_value, value),
)
}),
column,
row,
action,
}
})
.collect();
let actions: Vec<Option<DatumAction>> = cells
.iter()
.filter(|cell| cell.missing.is_none())
.map(|cell| cell.action.clone())
.collect();
let x_ticks: Vec<(String, &'static str, String, String)> = model
.x_ticks
.iter()
.map(|tick| {
(
tick.index.to_string(),
CategoryTick::ANCHOR,
js_number(tick.position),
tick.label.clone(),
)
})
.collect();
let y_ticks: Vec<(String, String, String)> = model
.y_ticks
.iter()
.map(|tick| {
(
tick.value.key(),
js_number(tick.position),
tick.label.clone(),
)
})
.collect();
let plot_name = plot_label(
&props.attributes,
props.heading.as_ref(),
&labels.chart_data,
);
let legend_style = format!("background:{}", model.legend_background);
let midpoint_style = format!("left:{}%", js_number(model.midpoint_percent));
let diverging = props.color_scale == HeatmapColorScale::Diverging;
let frame = ChartFrame {
chart_class: Some(compose([Some("ui-heatmap"), props.class.as_deref()])),
heading: props.heading.clone(),
description: props.description.clone(),
value: props.value.clone(),
summary: Some(
props
.summary
.clone()
.unwrap_or_else(|| labels.heatmap_summary(available)),
),
caption: props.caption.clone(),
presentation: props.presentation,
glass: props.glass,
activation: on_activate.is_some(),
attributes: props.attributes.clone(),
on_mounted: None,
};
let body = rsx! {
if available == 0 {
p { class: "ui-chart__empty", role: "status", "{labels.empty}" }
}
div {
aria_label: "{plot_name}",
class: "ui-chart__plot",
role: "region",
tabindex: "0",
hidden: !has_cells,
svg { "aria-hidden": "true", "style": "--ui-chart-viewbox-width: 640", "viewBox": "0 0 640 320",
g { class: "ui-chart__axis-labels",
for (key, anchor, x, label) in x_ticks {
text { key: "{key}", "text-anchor": anchor, x: "{x}", y: "298", "{label}" }
}
for (key, y, label) in y_ticks {
text {
key: "row-{key}",
class: "ui-heatmap__row-label",
"text-anchor": "end",
"dominant-baseline": "middle",
x: "108",
y: "{y}",
"{label}"
}
}
}
g { class: "ui-heatmap__cells",
for cell in cells.iter() {
g { key: "{cell.key}",
rect {
"data-ui-chart-datum": serialized(cell.action.as_ref()),
height: "{cell.height}",
width: "{cell.width}",
x: "{cell.x}",
y: "{cell.y}",
style: "{cell.style}",
onclick: activate_on_click(cell.action.as_ref(), on_activate),
title { "{cell.title}" }
}
if let Some((x, y)) = cell.missing.clone() {
text {
class: "ui-heatmap__missing",
"text-anchor": "middle",
"dominant-baseline": "middle",
x: "{x}",
y: "{y}",
"×"
}
}
}
}
}
}
}
if props.show_legend && has_cells {
div { class: "ui-heatmap__legend", aria_label: "{labels.chart_legend}", role: "group",
span { "{format_with(format_value, model.domain.min)}" }
span { style: "{legend_style}", class: "ui-heatmap__scale",
if diverging {
span { style: "{midpoint_style}", "{format_with(format_value, model.midpoint)}" }
}
}
span { "{format_with(format_value, model.domain.max)}" }
span {
span { aria_hidden: "true", "×" }
" {labels.not_available}"
}
}
}
if props.show_table {
details { class: "ui-chart__data",
summary { "{labels.view_data}" }
div { aria_label: "{labels.chart_data}", role: "group", tabindex: "0",
table {
thead {
tr {
th { scope: "col", "{labels.column}" }
th { scope: "col", "{labels.row}" }
th { scope: "col", "{labels.value}" }
}
}
tbody {
for cell in cells.iter() {
tr { key: "row-{cell.key}",
th { scope: "row", "{cell.column}" }
td { "{cell.row}" }
td { "{cell.value}" }
}
}
}
}
}
}
}
if on_activate.is_some() {
{datum_actions(&actions, &labels.explore_data, on_activate)}
}
};
render_frame(frame, body)
}