Skip to main content

fission_charts/series/
boxplot.rs

1use fission_core::op::Color;
2use serde::{Deserialize, Serialize};
3
4#[derive(Debug, Clone, Serialize, Deserialize)]
5pub struct BoxplotSeries {
6    pub name: String,
7    pub data: Vec<Vec<f32>>, // [min, Q1, median, Q3, max]
8    pub color: Color,
9}
10
11impl BoxplotSeries {
12    pub fn new(name: &str) -> Self {
13        Self {
14            name: name.into(),
15            data: Vec::new(),
16            color: Color::BLUE,
17        }
18    }
19
20    pub fn data(mut self, data: Vec<Vec<f32>>) -> Self {
21        self.data = data;
22        self
23    }
24
25    pub fn calculate_from_raw(mut self, raw_data: Vec<Vec<f32>>) -> Self {
26        let mut calculated = Vec::new();
27        for mut group in raw_data {
28            if group.is_empty() {
29                continue;
30            }
31            group.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));
32            let min = group[0];
33            let max = group[group.len() - 1];
34            let q1 = group[(group.len() as f32 * 0.25).floor() as usize];
35            let median = group[(group.len() as f32 * 0.5).floor() as usize];
36            let q3 = group[(group.len() as f32 * 0.75).floor() as usize];
37            calculated.push(vec![min, q1, median, q3, max]);
38        }
39        self.data = calculated;
40        self
41    }
42
43    pub fn color(mut self, color: Color) -> Self {
44        self.color = color;
45        self
46    }
47}
48
49impl Into<super::Series> for BoxplotSeries {
50    fn into(self) -> super::Series {
51        super::Series::Boxplot(self)
52    }
53}