Debian Codegraph @

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

raw symbols SCIP
1 use itertoolsrust-analyzer cargo itertools 0.15.0 crate/::Itertoolsrust-analyzer cargo itertools 0.15.0 Itertools#;
2 use ratatuirust-analyzer cargo ratatui 0.30.2 crate/::{
3 stylerust-analyzer cargo ratatui-core 0.1.2 style/::Colorrust-analyzer cargo ratatui-core 0.1.2 style/color/Color#,
4 widgetsrust-analyzer cargo ratatui 0.30.2 widgets/::{
5 GraphTyperust-analyzer cargo ratatui-widgets 0.3.2 chart/GraphType#,
6 canvasrust-analyzer cargo ratatui-widgets 0.3.2 canvas/::{Linerust-analyzer cargo ratatui-widgets 0.3.2 canvas/line/Line# as CanvasLinerust-analyzer cargo ratatui-widgets 0.3.2 canvas/line/Line#, Pointsrust-analyzer cargo ratatui-widgets 0.3.2 canvas/points/Points#},
7 },
8 };
9
10 use superrust-analyzer cargo bottom 0.14.9 canvas/components/time_series/vendored/::{Contextrust-analyzer cargo bottom 0.14.9 canvas/components/time_series/vendored/canvas/Context#, Datarust-analyzer cargo bottom 0.14.9 canvas/components/time_series/vendored/Data#, Pointrust-analyzer cargo bottom 0.14.9 canvas/components/time_series/vendored/Point#, TimeChartrust-analyzer cargo bottom 0.14.9 canvas/components/time_series/vendored/TimeChart#};
11
12 impl<F: Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy# + Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default# + Intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#<f64>> TimeChartrust-analyzer cargo bottom 0.14.9 canvas/components/time_series/vendored/TimeChart#<'_, F> {
13 pub(craterust-analyzer cargo bottom 0.14.9 crate/) fn draw_pointsrust-analyzer cargo bottom 0.14.9 canvas/components/time_series/vendored/points/impl#[`TimeChart<'_, F>`]draw_points().(&self, ctx: &mut Contextrust-analyzer cargo bottom 0.14.9 canvas/components/time_series/vendored/canvas/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.datasetsrust-analyzer cargo bottom 0.14.9 canvas/components/time_series/vendored/TimeChart#datasets. {
32 let Datarust-analyzer cargo bottom 0.14.9 canvas/components/time_series/vendored/Data#::Somerust-analyzer cargo bottom 0.14.9 canvas/components/time_series/vendored/Data#Some# { timesrust-analyzer cargo bottom 0.14.9 canvas/components/time_series/vendored/Data#Some#times., valuesrust-analyzer cargo bottom 0.14.9 canvas/components/time_series/vendored/Data#Some#values. } = dataset.datarust-analyzer cargo bottom 0.14.9 canvas/components/time_series/vendored/Dataset#data. else {
33 continue;
34 };
35
36 let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(current_time) = times.lastrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]last().() else {
37 continue;
38 };
39
40 let color = dataset.stylerust-analyzer cargo bottom 0.14.9 canvas/components/time_series/vendored/Dataset#style..fgrust-analyzer cargo ratatui-core 0.1.2 style/Style#fg..unwrap_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap_or().(Colorrust-analyzer cargo ratatui-core 0.1.2 style/color/Color#::Resetrust-analyzer cargo ratatui-core 0.1.2 style/color/Color#Reset#);
41 let left_edge = self.x_axisrust-analyzer cargo bottom 0.14.9 canvas/components/time_series/vendored/TimeChart#x_axis..boundsrust-analyzer cargo bottom 0.14.9 canvas/components/time_series/vendored/Axis#bounds..get_boundsrust-analyzer cargo bottom 0.14.9 canvas/components/time_series/vendored/impl#[AxisBound]get_bounds().()[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().0]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().;
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_baserust-analyzer cargo timeless 0.1.0 data/chunked/impl#[`ChunkedData<D>`]iter_along_base().(times)
48 .revrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#rev().()
49 .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|(&time, &val)| {
50 let from_start = -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][Neg]neg().(current_time.duration_sincerust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/impl#[Instant]duration_since().(time).as_millisrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/impl#[Duration]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.scalingrust-analyzer cargo bottom 0.14.9 canvas/components/time_series/vendored/TimeChart#scaling..scalerust-analyzer cargo bottom 0.14.9 canvas/components/time_series/vendored/impl#[ChartScaling]scale().(val.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#into().()))
56 })
57 .tuple_windowsrust-analyzer cargo itertools 0.15.0 Itertools#tuple_windows().()
58 {
59 if curr.0 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[f64][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[f64][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[f64][`PartialEq<Self>`]eq().left_edge {
60 // The current point hits the left edge. Draw just the current point and halt.
61 ctx.drawrust-analyzer cargo bottom 0.14.9 canvas/components/time_series/vendored/canvas/impl#[`Context<'a>`]draw().(&Pointsrust-analyzer cargo ratatui-widgets 0.3.2 canvas/points/Points# {
62 coordsrust-analyzer cargo ratatui-widgets 0.3.2 canvas/points/Points#coords.: &[curr],
63 colorrust-analyzer cargo ratatui-widgets 0.3.2 canvas/points/Points#color.,
64 });
65
66 break;
67 } else if next.0 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[f64][`PartialOrd<Self>`]lt().<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[f64][`PartialOrd<Self>`]lt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[f64][`PartialOrd<Self>`]lt().left_edge {
68 // The next point goes past the left edge. Interpolate a point + the line and
69 // halt.
70 let interpolated = interpolate_pointrust-analyzer cargo bottom 0.14.9 canvas/components/time_series/vendored/points/interpolate_point().(&next, &curr, left_edge);
71
72 ctx.drawrust-analyzer cargo bottom 0.14.9 canvas/components/time_series/vendored/canvas/impl#[`Context<'a>`]draw().(&CanvasLinerust-analyzer cargo ratatui-widgets 0.3.2 canvas/line/Line# {
73 x1rust-analyzer cargo ratatui-widgets 0.3.2 canvas/line/Line#x1.: curr.0,
74 y1rust-analyzer cargo ratatui-widgets 0.3.2 canvas/line/Line#y1.: curr.1,
75 x2rust-analyzer cargo ratatui-widgets 0.3.2 canvas/line/Line#x2.: left_edge,
76 y2rust-analyzer cargo ratatui-widgets 0.3.2 canvas/line/Line#y2.: interpolated,
77 colorrust-analyzer cargo ratatui-widgets 0.3.2 canvas/line/Line#color.,
78 });
79
80 break;
81 } else {
82 // Draw the current point and the line to the next point.
83 if let GraphTyperust-analyzer cargo ratatui-widgets 0.3.2 chart/GraphType#::Linerust-analyzer cargo ratatui-widgets 0.3.2 chart/GraphType#Line# = dataset.graph_typerust-analyzer cargo bottom 0.14.9 canvas/components/time_series/vendored/Dataset#graph_type. {
84 ctx.drawrust-analyzer cargo bottom 0.14.9 canvas/components/time_series/vendored/canvas/impl#[`Context<'a>`]draw().(&CanvasLinerust-analyzer cargo ratatui-widgets 0.3.2 canvas/line/Line# {
85 x1rust-analyzer cargo ratatui-widgets 0.3.2 canvas/line/Line#x1.: curr.0,
86 y1rust-analyzer cargo ratatui-widgets 0.3.2 canvas/line/Line#y1.: curr.1,
87 x2rust-analyzer cargo ratatui-widgets 0.3.2 canvas/line/Line#x2.: next.0,
88 y2rust-analyzer cargo ratatui-widgets 0.3.2 canvas/line/Line#y2.: next.1,
89 colorrust-analyzer cargo ratatui-widgets 0.3.2 canvas/line/Line#color.,
90 });
91 } else {
92 ctx.drawrust-analyzer cargo bottom 0.14.9 canvas/components/time_series/vendored/canvas/impl#[`Context<'a>`]draw().(&Pointsrust-analyzer cargo ratatui-widgets 0.3.2 canvas/points/Points# {
93 coordsrust-analyzer cargo ratatui-widgets 0.3.2 canvas/points/Points#coords.: &[curr],
94 colorrust-analyzer cargo ratatui-widgets 0.3.2 canvas/points/Points#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_pointrust-analyzer cargo bottom 0.14.9 canvas/components/time_series/vendored/points/interpolate_point().(older_point: &Pointrust-analyzer cargo bottom 0.14.9 canvas/components/time_series/vendored/Point#, newer_point: &Pointrust-analyzer cargo bottom 0.14.9 canvas/components/time_series/vendored/Point#, x: f64) -> f64 {
106 let delta_x = newer_point.0 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Sub<Self>`]sub().older_point.0;
107 let delta_y = newer_point.1 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Sub<Self>`]sub().older_point.1;
108 let slope = delta_y rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Div<Self>`]div()./rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Div<Self>`]div(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Div<Self>`]div().delta_x;
109
110 (older_point.1 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Add<Self>`]add().(x rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Sub<Self>`]sub().older_point.0) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Mul<Self>`]mul().slope).maxrust-analyzer cargo core https://github.com/rust-lang/rust/library/core f64/impl#[f64]max().(0.0)
111 }
112
113 #[cfg(test)]
114 mod testrust-analyzer cargo bottom 0.14.9 canvas/components/time_series/vendored/points/test/ {
115 use superrust-analyzer cargo bottom 0.14.9 canvas/components/time_series/vendored/points/::*;
116
117 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
118 fn time_series_test_interpolationrust-analyzer cargo bottom 0.14.9 canvas/components/time_series/vendored/points/test/time_series_test_interpolation().() {
119 let data = [(-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][Neg]neg().3.0, 8.0), (-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][Neg]neg().1.0, 6.0), (0.0, 5.0)];
120
121 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(interpolate_pointrust-analyzer cargo bottom 0.14.9 canvas/components/time_series/vendored/points/interpolate_point().(&data[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().1]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index()., &data[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().2]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index()., 0.0), 5.0);
122 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(interpolate_pointrust-analyzer cargo bottom 0.14.9 canvas/components/time_series/vendored/points/interpolate_point().(&data[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().1]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index()., &data[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().2]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index()., -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][Neg]neg().0.25), 5.25);
123 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(interpolate_pointrust-analyzer cargo bottom 0.14.9 canvas/components/time_series/vendored/points/interpolate_point().(&data[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().1]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index()., &data[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().2]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index()., -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][Neg]neg().0.5), 5.5);
124 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(interpolate_pointrust-analyzer cargo bottom 0.14.9 canvas/components/time_series/vendored/points/interpolate_point().(&data[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().0]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index()., &data[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().1]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index()., -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][Neg]neg().1.0), 6.0);
125 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(interpolate_pointrust-analyzer cargo bottom 0.14.9 canvas/components/time_series/vendored/points/interpolate_point().(&data[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().0]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index()., &data[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().1]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index()., -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][Neg]neg().1.5), 6.5);
126 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(interpolate_pointrust-analyzer cargo bottom 0.14.9 canvas/components/time_series/vendored/points/interpolate_point().(&data[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().0]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index()., &data[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().1]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index()., -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][Neg]neg().2.0), 7.0);
127 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(interpolate_pointrust-analyzer cargo bottom 0.14.9 canvas/components/time_series/vendored/points/interpolate_point().(&data[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().0]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index()., &data[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().1]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index()., -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][Neg]neg().2.5), 7.5);
128 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(interpolate_pointrust-analyzer cargo bottom 0.14.9 canvas/components/time_series/vendored/points/interpolate_point().(&data[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().0]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index()., &data[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().1]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index()., -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][Neg]neg().3.0), 8.0);
129 }
130 }