| 1 | use std::borrow::Cow; |
| 2 | |
| 3 | use ratatui::{ |
| 4 | Frame, |
| 5 | layout::{Constraint, Rect}, |
| 6 | }; |
| 7 | |
| 8 | use crate::{ |
| 9 | app::{App, AppConfigFields}, |
| 10 | canvas::{ |
| 11 | Painter, |
| 12 | components::time_series::{AxisBound, ChartScaling, GraphData, LegendConstraints}, |
| 13 | drawing_utils::should_hide_x_label, |
| 14 | }, |
| 15 | components::time_series::GraphDrawCtx, |
| 16 | }; |
| 17 | |
| 18 | impl Painter { |
| 19 | pub fn draw_temperature_graph( |
| 20 | &self, f: &mut Frame<'_>, app_state: &mut App, draw_loc: Rect, widget_id: u64, |
| 21 | ) { |
| 22 | if let Some(widget_state) = app_state |
| 23 | .states |
| 24 | .temp_graph_state |
| 25 | .get_mut_widget_state(widget_id) |
| 26 | { |
| 27 | let shared_data = app_state.data_store.get_data(); |
| 28 | let points = &(shared_data.time_series_data.temperature); |
| 29 | let times = &(shared_data.time_series_data.time); |
| 30 | |
| 31 | let border_style = self.get_border_style(widget_id, app_state.current_widget.widget_id); |
| 32 | let hide_x_labels = should_hide_x_label( |
| 33 | app_state.app_config_fields.hide_time, |
| 34 | app_state.app_config_fields.autohide_time, |
| 35 | widget_state.graph.state_mut().autohide_timer_mut(), |
| 36 | draw_loc, |
| 37 | ); |
| 38 | |
| 39 | let y_max = widget_state.graph.y_max(points.values(), times); |
| 40 | let (adjusted_y_max, y_labels) = |
| 41 | adjust_temp_data_point(y_max, widget_state.max_temp, &app_state.app_config_fields); |
| 42 | let y_bounds = AxisBound::Max(adjusted_y_max); |
| 43 | |
| 44 | // Hide the legend if the width is 90% of the total widget width |
| 45 | // or the height is greater than 50% of the total widget height. |
| 46 | let legend_constraints = LegendConstraints { |
| 47 | width: Constraint::Ratio(9, 10), |
| 48 | height: Constraint::Ratio(1, 2), |
| 49 | }; |
| 50 | |
| 51 | let unit = app_state.app_config_fields.temperature_type.unit(); |
| 52 | let graph_data: Vec<GraphData<'_, f32>> = points |
| 53 | .iter() |
| 54 | .enumerate() |
| 55 | .map(|(itx, (source, values))| { |
| 56 | // TODO: Maybe align the value later. |
| 57 | let name = match values.last() { |
| 58 | Some(latest) => format!("{source}: {latest:.0}{unit}").into(), |
| 59 | None => source.as_str().into(), |
| 60 | }; |
| 61 | GraphData::default() |
| 62 | .name(name) |
| 63 | .style( |
| 64 | self.styles.temp_graph_colour_styles |
| 65 | [itx % self.styles.temp_graph_colour_styles.len()], |
| 66 | ) |
| 67 | .time(times) |
| 68 | .values(values) |
| 69 | }) |
| 70 | .collect(); |
| 71 | |
| 72 | let marker = self.get_marker(app_state.app_config_fields.use_dot); |
| 73 | |
| 74 | let y_labels: Vec<Cow<'_, str>> = y_labels.into_iter().map(Into::into).collect(); |
| 75 | |
| 76 | widget_state.graph.draw( |
| 77 | f, |
| 78 | draw_loc, |
| 79 | GraphDrawCtx { |
| 80 | title: " Temperature ".into(), |
| 81 | border_style, |
| 82 | title_style: self.styles.widget_title_style, |
| 83 | graph_style: self.styles.graph_style, |
| 84 | general_widget_style: self.styles.general_widget_style, |
| 85 | border_type: self.styles.border_type, |
| 86 | marker, |
| 87 | hide_x_labels, |
| 88 | is_selected: app_state.current_widget.widget_id == widget_id, |
| 89 | is_expanded: app_state.is_expanded, |
| 90 | legend_position: app_state.app_config_fields.temperature_legend_position, |
| 91 | legend_constraints: Some(legend_constraints), |
| 92 | }, |
| 93 | y_bounds, |
| 94 | &y_labels, |
| 95 | ChartScaling::Linear, |
| 96 | graph_data, |
| 97 | ); |
| 98 | } |
| 99 | |
| 100 | // Update draw loc in widget map. |
| 101 | if app_state.should_get_widget_bounds() |
| 102 | && let Some(temperature_graph_widget) = app_state.widget_map.get_mut(&widget_id) |
| 103 | { |
| 104 | temperature_graph_widget.top_left_corner = Some((draw_loc.x, draw_loc.y)); |
| 105 | temperature_graph_widget.bottom_right_corner = |
| 106 | Some((draw_loc.x + draw_loc.width, draw_loc.y + draw_loc.height)); |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | /// Returns the required labels. |
| 112 | fn adjust_temp_data_point( |
| 113 | max_entry: f64, upper_limit: Option<f32>, config: &AppConfigFields, |
| 114 | ) -> (f64, [String; 3]) { |
| 115 | let default_upper: f64 = config |
| 116 | .temperature_type |
| 117 | .convert_temp_unit_float(100.0) |
| 118 | .into(); |
| 119 | let unit = config.temperature_type.unit(); |
| 120 | |
| 121 | let max_entry = if let Some(limit) = upper_limit { |
| 122 | limit as f64 |
| 123 | } else if max_entry < default_upper { |
| 124 | default_upper |
| 125 | } else { |
| 126 | max_entry |
| 127 | }; |
| 128 | |
| 129 | let halfway_label = (max_entry / 2.0).ceil() as u32; |
| 130 | let max_entry_label = max_entry.ceil() as u32; |
| 131 | |
| 132 | let labels = [ |
| 133 | format!("0{unit}"), |
| 134 | format!("{halfway_label}{unit}"), |
| 135 | format!("{max_entry_label}{unit}"), |
| 136 | ]; |
| 137 | |
| 138 | (max_entry, labels) |
| 139 | } |
| 140 | |
| 141 | #[cfg(test)] |
| 142 | mod tests { |
| 143 | use super::*; |
| 144 | use crate::app::data::TemperatureType; |
| 145 | |
| 146 | fn config(temperature_type: TemperatureType) -> AppConfigFields { |
| 147 | AppConfigFields { |
| 148 | temperature_type, |
| 149 | ..Default::default() |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | #[test] |
| 154 | fn floors_to_default_upper_when_below() { |
| 155 | let cfg = config(TemperatureType::Celsius); |
| 156 | let (max, labels) = adjust_temp_data_point(40.0, None, &cfg); |
| 157 | assert_eq!(max, 100.0); |
| 158 | assert_eq!(labels, ["0°C", "50°C", "100°C"]); |
| 159 | } |
| 160 | |
| 161 | #[test] |
| 162 | fn keeps_actual_max_when_above_default() { |
| 163 | let cfg = config(TemperatureType::Celsius); |
| 164 | let (max, labels) = adjust_temp_data_point(120.0, None, &cfg); |
| 165 | assert_eq!(max, 120.0); |
| 166 | assert_eq!(labels, ["0°C", "60°C", "120°C"]); |
| 167 | } |
| 168 | |
| 169 | #[test] |
| 170 | fn upper_limit_overrides_actual_max() { |
| 171 | let cfg = config(TemperatureType::Celsius); |
| 172 | let (max, labels) = adjust_temp_data_point(150.0, Some(80.0), &cfg); |
| 173 | assert_eq!(max, 80.0); |
| 174 | assert_eq!(labels, ["0°C", "40°C", "80°C"]); |
| 175 | } |
| 176 | |
| 177 | #[test] |
| 178 | fn fahrenheit_default_is_212() { |
| 179 | let cfg = config(TemperatureType::Fahrenheit); |
| 180 | let (max, labels) = adjust_temp_data_point(100.0, None, &cfg); |
| 181 | assert_eq!(max, 212.0); |
| 182 | assert_eq!(labels, ["0°F", "106°F", "212°F"]); |
| 183 | } |
| 184 | |
| 185 | #[test] |
| 186 | fn kelvin_default_is_373() { |
| 187 | let cfg = config(TemperatureType::Kelvin); |
| 188 | let (max, labels) = adjust_temp_data_point(100.0, None, &cfg); |
| 189 | assert!((max - 373.15).abs() < 1e-3); |
| 190 | assert_eq!(labels, ["0K", "187K", "374K"]); |
| 191 | } |
| 192 | } |