1use dioxus::prelude::*;
4
5use super::activation::{ChartDatumActivation, DatumAction};
6use super::format::js_number;
7use super::frame::{
8 ChartFrame, ChartPresentation, activate_on_click, datum_actions, plot_label, render_frame,
9 serialized, table_value,
10};
11use super::geometry::{
12 COMPACT_AXIS_LABEL_SIZE, Margin, RangeChartDatum, chart_size, range_geometry,
13};
14use super::model::{ChartLabels, ChartTone, format_with};
15use super::scale::{CategoryTick, CategoryTickOptions, category_ticks, format_ticks, scale_value};
16use crate::class::{Glass, compose};
17
18#[derive(Props, Clone, PartialEq)]
20pub struct RangeChartProps {
21 pub width: Option<f64>,
23 pub height: Option<f64>,
25 #[props(default)]
27 pub data: Vec<RangeChartDatum>,
28 #[props(default)]
30 pub tone: ChartTone,
31 pub format_value: Option<Callback<f64, String>>,
35 pub labels: Option<ChartLabels>,
37 #[props(default = true)]
39 pub show_table: bool,
40 pub on_datum_activate: Option<EventHandler<ChartDatumActivation>>,
42 #[props(into)]
44 pub heading: Option<String>,
45 #[props(into)]
47 pub description: Option<String>,
48 #[props(into)]
50 pub value: Option<String>,
51 #[props(into)]
53 pub summary: Option<String>,
54 #[props(into)]
56 pub caption: Option<String>,
57 #[props(default)]
59 pub presentation: ChartPresentation,
60 #[props(default, into)]
62 pub glass: Glass,
63 #[props(into)]
65 pub class: Option<String>,
66 #[props(extends = GlobalAttributes, extends = figure)]
68 pub attributes: Vec<Attribute>,
69}
70
71struct IntervalView {
72 key: String,
73 x: String,
74 high: String,
75 low: String,
76 title: String,
77 hit: [String; 3],
78 action: Option<DatumAction>,
79}
80
81#[component]
84#[allow(clippy::too_many_lines)]
85pub fn RangeChart(props: RangeChartProps) -> Element {
86 let labels = props.labels.clone().unwrap_or_default();
87 let format_value = props.format_value.as_ref();
88 let width = chart_size(props.width, 640.0);
89 let height = chart_size(props.height, 320.0);
90 let geometry = range_geometry(
91 &props.data,
92 Margin {
93 bottom: 44.0,
94 left: 44.0,
95 right: 24.0,
96 top: 16.0,
97 },
98 width,
99 height,
100 Some(COMPACT_AXIS_LABEL_SIZE),
101 format_value,
102 );
103 let plot = geometry.plot;
104 let on_activate = props.on_datum_activate;
105 let tick_labels: Vec<String> = geometry
106 .points
107 .iter()
108 .map(|point| {
109 point
110 .datum
111 .x_label
112 .clone()
113 .unwrap_or_else(|| point.datum.x.display())
114 })
115 .collect();
116 let tick_positions: Vec<f64> = geometry.points.iter().map(|point| point.x).collect();
117 let category_ticks: Vec<(String, &'static str, String, String)> = category_ticks(
118 &tick_labels,
119 CategoryTickOptions::new(plot.left, plot.right)
120 .bounds(0.0, width)
121 .maximum_font_size(COMPACT_AXIS_LABEL_SIZE)
122 .positions(&tick_positions),
123 )
124 .into_iter()
125 .map(|tick| {
126 (
127 tick.index.to_string(),
128 CategoryTick::ANCHOR,
129 js_number(tick.position),
130 tick.label,
131 )
132 })
133 .collect();
134 let grid: Vec<(String, String, String)> = geometry
135 .ticks
136 .iter()
137 .zip(format_ticks(&geometry.ticks, format_value))
138 .map(|(tick, label)| {
139 (
140 js_number(*tick),
141 js_number(scale_value(*tick, geometry.domain, plot.bottom, plot.top)),
142 label,
143 )
144 })
145 .collect();
146 let intervals: Vec<IntervalView> = geometry
147 .points
148 .iter()
149 .map(|point| {
150 let datum = &point.datum;
151 let text = datum.label.clone().unwrap_or_else(|| {
152 format!(
153 "{}–{}",
154 format_with(format_value, datum.low.unwrap_or(0.0)),
155 format_with(format_value, datum.high.unwrap_or(0.0))
156 )
157 });
158 let title = format!(
159 "{}: {text}",
160 datum.x_label.clone().unwrap_or_else(|| datum.x.display())
161 );
162 let action = on_activate.and_then(|_| {
163 DatumAction::new(
164 ChartDatumActivation::range(&datum.x, datum.low, datum.high, datum.id.as_ref()),
165 &title,
166 &labels,
167 )
168 });
169 let span = (point.high - point.low).abs();
170
171 IntervalView {
172 key: datum.id.clone().unwrap_or_else(|| datum.x.key()),
173 x: js_number(point.x),
174 high: js_number(point.high),
175 low: js_number(point.low),
176 hit: [
177 js_number(point.x - 10.0),
178 js_number(point.high.min(point.low) - (20.0 - span).max(0.0) / 2.0),
179 js_number(span.max(20.0)),
180 ],
181 title,
182 action,
183 }
184 })
185 .collect();
186 let actions: Vec<Option<DatumAction>> = intervals
187 .iter()
188 .map(|interval| interval.action.clone())
189 .collect();
190 let rows: Vec<(String, String, String, String)> = props
191 .data
192 .iter()
193 .map(|datum| {
194 (
195 datum.id.clone().unwrap_or_else(|| datum.x.key()),
196 datum.x_label.clone().unwrap_or_else(|| datum.x.display()),
197 table_value(datum.low, format_value, &labels.not_available),
198 table_value(datum.high, format_value, &labels.not_available),
199 )
200 })
201 .collect();
202 let empty = geometry.points.is_empty();
203 let plot_name = plot_label(
204 &props.attributes,
205 props.heading.as_ref(),
206 &labels.chart_data,
207 );
208 let frame = ChartFrame {
209 chart_class: Some(compose([
210 Some("ui-range-chart"),
211 Some(props.tone.class_name()),
212 props.class.as_deref(),
213 ])),
214 heading: props.heading.clone(),
215 description: props.description.clone(),
216 value: props.value.clone(),
217 summary: Some(
218 props
219 .summary
220 .clone()
221 .unwrap_or_else(|| labels.range_summary(geometry.points.len())),
222 ),
223 caption: props.caption.clone(),
224 presentation: props.presentation,
225 glass: props.glass,
226 activation: on_activate.is_some(),
227 attributes: props.attributes.clone(),
228 on_mounted: None,
229 };
230 let left = js_number(plot.left);
231 let right = js_number(plot.right);
232 let label_x = js_number(plot.left - 8.0);
233 let view_box = format!("0 0 {} {}", js_number(width), js_number(height));
234 let viewbox_width = js_number(width);
235
236 let axis_y = js_number(height - 12.0);
237 let body = rsx! {
238 if empty {
239 p { class: "ui-chart__empty", role: "status", "{labels.empty}" }
240 }
241 div {
242 aria_label: "{plot_name}",
243 class: "ui-chart__plot",
244 role: "region",
245 tabindex: "0",
246 hidden: empty,
247 svg { "aria-hidden": "true", "style": "--ui-chart-viewbox-width: {viewbox_width}", "viewBox": "{view_box}",
248 g { class: "ui-chart__grid",
249 for (key, y, text) in grid {
250 g { key: "{key}",
251 line { x1: "{left}", x2: "{right}", y1: "{y}", y2: "{y}" }
252 text { x: "{label_x}", y: "{y}", "{text}" }
253 }
254 }
255 }
256 g { class: "ui-chart__axis-labels",
257 for (key, anchor, x, label) in category_ticks {
258 text { key: "{key}", x: "{x}", y: "{axis_y}", "text-anchor": anchor, "{label}" }
259 }
260 }
261 path { class: "ui-range-chart__area", d: "{geometry.area_path}" }
262 for interval in intervals.iter() {
263 line {
264 key: "{interval.key}",
265 "data-ui-chart-datum": serialized(interval.action.as_ref()),
266 class: "ui-range-chart__interval",
267 x1: "{interval.x}",
268 x2: "{interval.x}",
269 y1: "{interval.high}",
270 y2: "{interval.low}",
271 onclick: activate_on_click(interval.action.as_ref(), on_activate),
272 title { "{interval.title}" }
273 }
274 }
275 if on_activate.is_some() {
276 for interval in intervals.iter() {
277 rect {
278 key: "hit-{interval.key}",
279 class: "ui-chart__datum-hit",
280 x: "{interval.hit[0]}",
281 y: "{interval.hit[1]}",
282 width: "20",
283 height: "{interval.hit[2]}",
284 "data-ui-chart-datum": serialized(interval.action.as_ref()),
285 onclick: activate_on_click(interval.action.as_ref(), on_activate),
286 }
287 }
288 }
289 }
290 }
291 if props.show_table {
292 details { class: "ui-chart__data",
293 summary { "{labels.view_data}" }
294 div { aria_label: "{labels.chart_data}", role: "group", tabindex: "0",
295 table {
296 thead {
297 tr {
298 th { scope: "col", "{labels.category}" }
299 th { scope: "col", "{labels.low}" }
300 th { scope: "col", "{labels.high}" }
301 }
302 }
303 tbody {
304 for (key, label, low, high) in rows {
305 tr { key: "{key}",
306 th { scope: "row", "{label}" }
307 td { "{low}" }
308 td { "{high}" }
309 }
310 }
311 }
312 }
313 }
314 }
315 }
316 if on_activate.is_some() {
317 {datum_actions(&actions, &labels.explore_data, on_activate)}
318 }
319 };
320
321 render_frame(frame, body)
322}