audio_processor_iced_design_system/
charts.rs

1// Augmented Audio: Audio libraries and applications
2// Copyright (c) 2022 Pedro Tacla Yamada
3//
4// The MIT License (MIT)
5//
6// Permission is hereby granted, free of charge, to any person obtaining a copy
7// of this software and associated documentation files (the "Software"), to deal
8// in the Software without restriction, including without limitation the rights
9// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10// copies of the Software, and to permit persons to whom the Software is
11// furnished to do so, subject to the following conditions:
12//
13// The above copyright notice and this permission notice shall be included in
14// all copies or substantial portions of the Software.
15//
16// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22// THE SOFTWARE.
23use iced::widget::canvas::{Cursor, Frame, Geometry, Program, Stroke};
24use iced::{widget::canvas, widget::Canvas, Color, Element, Length, Point, Rectangle, Size};
25
26pub struct LineChart {
27    data: Vec<(f32, f32)>,
28}
29
30impl LineChart {
31    pub fn new(data: Vec<(f32, f32)>) -> Self {
32        LineChart { data }
33    }
34
35    pub fn set_data(&mut self, data: Vec<(f32, f32)>) {
36        self.data = data;
37    }
38}
39
40#[derive(Debug, Clone)]
41pub enum LineChartMessage {}
42
43impl Program<LineChartMessage> for &LineChart {
44    type State = ();
45
46    fn draw(
47        &self,
48        _state: &Self::State,
49        _theme: &iced_style::Theme,
50        bounds: Rectangle,
51        _cursor: Cursor,
52    ) -> Vec<Geometry> {
53        if self.data.is_empty() {
54            return vec![];
55        }
56
57        let size = bounds.size();
58        let Size { width, height } = size;
59        let mut frame = Frame::new(size);
60        let mut path = canvas::path::Builder::new();
61        let min_value_x = self.data.iter().map(|(x, _)| *x).fold(0.0, f32::min);
62        let max_value_x = self.data.iter().map(|(x, _)| *x).fold(0.0, f32::max);
63        let min_value_y = self.data.iter().map(|(_, y)| *y).fold(0.0, f32::min);
64        let max_value_y = self.data.iter().map(|(_, y)| *y).fold(0.0, f32::max);
65        let range_x = (min_value_x, max_value_x);
66        let range_y = (min_value_y, max_value_y);
67
68        for (x, y) in &self.data {
69            let x_prime = interpolate(*x, range_x, (0.0, width));
70            let y_prime = interpolate(*y, range_y, (0.0, height));
71            path.line_to(Point::new(x_prime, y_prime));
72        }
73
74        let stroke = Stroke::default().with_color(Color::new(1.0, 1.0, 1.0, 1.0));
75        frame.stroke(&path.build(), stroke);
76
77        vec![frame.into_geometry()]
78    }
79}
80
81impl LineChart {
82    pub fn element(&self) -> Element<LineChartMessage> {
83        Canvas::new(self)
84            .width(Length::Fill)
85            .height(Length::Fill)
86            .into()
87    }
88}
89
90fn interpolate(value: f32, range_from: (f32, f32), range_to: (f32, f32)) -> f32 {
91    let bounds_from = range_from.1 - range_from.0;
92    let bounds_to = range_to.1 - range_to.0;
93    (value - range_from.0) / bounds_from * bounds_to
94}
95
96#[cfg(test)]
97mod test {
98    use super::*;
99
100    #[test]
101    fn test_interpolate() {
102        assert_eq!(interpolate(1., (0., 1.), (0., 2.)), 2.);
103    }
104
105    #[test]
106    fn test_interpolate_negative_range() {
107        assert_eq!(interpolate(0., (-1., 1.), (0., 2.)), 1.);
108    }
109}