| 1 | use std::{borrow::Cow, time::Instant}; |
| 2 | |
| 3 | use ratatui::{Frame, layout::Rect}; |
| 4 | |
| 5 | use super::{ |
| 6 | AxisBound, ChartScaling, GraphData, GraphDrawCtx, TimeGraph, TimeseriesConfig, TimeseriesState, |
| 7 | }; |
| 8 | |
| 9 | /// A time series graph that expects data to be in a percentage format, |
| 10 | /// from 0% to 100%. |
| 11 | pub struct PercentTimeGraph { |
| 12 | state: TimeseriesState, |
| 13 | } |
| 14 | |
| 15 | impl PercentTimeGraph { |
| 16 | pub(crate) fn new(config: TimeseriesConfig, autohide_timer: Option<Instant>) -> Self { |
| 17 | PercentTimeGraph { |
| 18 | state: TimeseriesState::new(config, autohide_timer), |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | pub(crate) fn state_mut(&mut self) -> &mut TimeseriesState { |
| 23 | &mut self.state |
| 24 | } |
| 25 | |
| 26 | pub(crate) fn draw<F: Copy + Default + Into<f64>>( |
| 27 | &self, f: &mut Frame<'_>, draw_loc: Rect, ctx: GraphDrawCtx<'_>, |
| 28 | data: Vec<GraphData<'_, F>>, |
| 29 | ) { |
| 30 | const Y_BOUNDS: AxisBound = AxisBound::Max(100.5); |
| 31 | const Y_LABELS: [Cow<'static, str>; 2] = [Cow::Borrowed(" 0%"), Cow::Borrowed("100%")]; |
| 32 | |
| 33 | TimeGraph { |
| 34 | x_min: -(self.state.current_display_time() as f64), |
| 35 | hide_x_labels: ctx.hide_x_labels, |
| 36 | y_bounds: Y_BOUNDS, |
| 37 | y_labels: &Y_LABELS, |
| 38 | graph_style: ctx.graph_style, |
| 39 | general_widget_style: ctx.general_widget_style, |
| 40 | border_style: ctx.border_style, |
| 41 | border_type: ctx.border_type, |
| 42 | title: ctx.title, |
| 43 | is_selected: ctx.is_selected, |
| 44 | is_expanded: ctx.is_expanded, |
| 45 | title_style: ctx.title_style, |
| 46 | legend_position: ctx.legend_position, |
| 47 | legend_constraints: ctx.legend_constraints, |
| 48 | marker: ctx.marker, |
| 49 | scaling: ChartScaling::Linear, |
| 50 | } |
| 51 | .draw(f, draw_loc, data) |
| 52 | } |
| 53 | } |