Debian Codegraph @

src/canvas/components/time_series/vendored/points.rs

raw symbols SCIP
1 use itertools::Itertools;
2 use ratatui::{
3 style::Color,
4 widgets::{
5 GraphType,
6 canvas::{Line as CanvasLine, Points},
7 },
8 };
9
10 use super::{Context, Data, Point, TimeChart};
11
12 impl<F: Copy + Default + Into<f64>> TimeChart<'_, F> {
13 pub(crate) fn draw_points(&self, ctx: &mut Context<'_>) {
14 // Idea is to:
15 // - Go over all datasets, determine *where* a point will be drawn.
16 // - Last point wins for what gets drawn.
17 // - We set _all_ points for all datasets before actually rendering.
18 //
19 // By doing this, it's a bit more efficient from my experience than looping
20 // over each dataset and rendering a new layer each time.
21 //
22 // See https://github.com/ClementTsang/bottom/pull/918 and
23 // https://github.com/ClementTsang/bottom/pull/937 for the original motivation.
24 //
25 // We also additionally do some interpolation logic because we may get caught
26 // missing some points when drawing, but we generally want to avoid
27 // jarring gaps between the edges when there's a point that is off
28 // screen and so a line isn't drawn (right edge generally won't have this issue
29 // issue but it can happen in some cases).
30
31 for dataset in &self.datasets {
32 let Data::Some { times, values } = dataset.data else {
33 continue;
34 };
35
36 let Some(current_time) = times.last() else {
37 continue;
38 };
39
40 let color = dataset.style.fg.unwrap_or(Color::Reset);
41 let left_edge = self.x_axis.bounds.get_bounds()[0];
42
43 // TODO: (points_rework_v1) Can we instead modify the range so it's based on the
44 // epoch rather than having to convert? TODO: (points_rework_v1) Is
45 // this efficient? Or should I prune using take_while first?
46 for (curr, next) in values
47 .iter_along_base(times)
48 .rev()
49 .map(|(&time, &val)| {
50 let from_start = -(current_time.duration_since(time).as_millis() as f64);
51
52 // XXX: Should this be generic over dataset.graph_type instead? That would allow
53 // us to move transformations behind a type - however, that
54 // also means that there's some complexity added.
55 (from_start, self.scaling.scale(val.into()))
56 })
57 .tuple_windows()
58 {
59 if curr.0 == left_edge {
60 // The current point hits the left edge. Draw just the current point and halt.
61 ctx.draw(&Points {
62 coords: &[curr],
63 color,
64 });
65
66 break;
67 } else if next.0 < left_edge {
68 // The next point goes past the left edge. Interpolate a point + the line and
69 // halt.
70 let interpolated = interpolate_point(&next, &curr, left_edge);
71
72 ctx.draw(&CanvasLine {
73 x1: curr.0,
74 y1: curr.1,
75 x2: left_edge,
76 y2: interpolated,
77 color,
78 });
79
80 break;
81 } else {
82 // Draw the current point and the line to the next point.
83 if let GraphType::Line = dataset.graph_type {
84 ctx.draw(&CanvasLine {
85 x1: curr.0,
86 y1: curr.1,
87 x2: next.0,
88 y2: next.1,
89 color,
90 });
91 } else {
92 ctx.draw(&Points {
93 coords: &[curr],
94 color,
95 });
96 }
97 }
98 }
99 }
100 }
101 }
102
103 /// Returns the y-axis value for a given `x`, given two points to draw a line
104 /// between.
105 fn interpolate_point(older_point: &Point, newer_point: &Point, x: f64) -> f64 {
106 let delta_x = newer_point.0 - older_point.0;
107 let delta_y = newer_point.1 - older_point.1;
108 let slope = delta_y / delta_x;
109
110 (older_point.1 + (x - older_point.0) * slope).max(0.0)
111 }
112
113 #[cfg(test)]
114 mod test {
115 use super::*;
116
117 #[test]
118 fn time_series_test_interpolation() {
119 let data = [(-3.0, 8.0), (-1.0, 6.0), (0.0, 5.0)];
120
121 assert_eq!(interpolate_point(&data[1], &data[2], 0.0), 5.0);
122 assert_eq!(interpolate_point(&data[1], &data[2], -0.25), 5.25);
123 assert_eq!(interpolate_point(&data[1], &data[2], -0.5), 5.5);
124 assert_eq!(interpolate_point(&data[0], &data[1], -1.0), 6.0);
125 assert_eq!(interpolate_point(&data[0], &data[1], -1.5), 6.5);
126 assert_eq!(interpolate_point(&data[0], &data[1], -2.0), 7.0);
127 assert_eq!(interpolate_point(&data[0], &data[1], -2.5), 7.5);
128 assert_eq!(interpolate_point(&data[0], &data[1], -3.0), 8.0);
129 }
130 }