zephyr_sdk/
charting.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#![allow(missing_docs)]

use charming_fork_zephyr::{
    component::{Axis, Legend},
    element::{AreaStyle, AxisType, Color, ColorStop, Tooltip, Trigger},
    series::{Bar, Line},
    Chart,
};
use serde::Serialize;
pub use table::Table;

mod table;

#[derive(Serialize, Default)]
pub struct Dashboard {
    title: Option<Title>,
    description: Option<String>,
    data: Vec<DashboardEntry>,
}

#[derive(Serialize)]
pub enum ChartType {
    #[serde(rename = "chart")]
    Chart,
    #[serde(rename = "table")]
    Table,
}

#[derive(Serialize)]
pub enum ChartTypeWrapped {
    #[serde(rename = "inner")]
    Chart(Chart),

    #[serde(rename = "inner")]
    Table(Table),
}

#[derive(Serialize, Default)]
struct Title {
    text: String,
}

#[derive(Serialize, Default)]
pub struct DashboardEntry {
    #[serde(rename = "type")]
    chart_type: Option<ChartType>,
    title: Title,
    height: String,
    width: String,
    #[serde(flatten)]
    inner: Option<ChartTypeWrapped>,
}

impl Dashboard {
    pub fn title(mut self, title: &impl ToString) -> Self {
        self.title = Some(Title {
            text: title.to_string(),
        });
        self
    }

    pub fn description(mut self, description: &impl ToString) -> Self {
        self.description = Some(description.to_string());
        self
    }

    pub fn entry(mut self, entry: DashboardEntry) -> Self {
        self.data.push(entry);
        self
    }

    pub fn new() -> Self {
        Self::default()
    }
}

impl DashboardEntry {
    pub fn new() -> Self {
        Self {
            chart_type: None,
            title: Title {
                text: String::new(),
            },
            height: String::from("300px"),
            width: String::from("1000px"),
            inner: None,
        }
    }

    pub fn title(mut self, title: impl ToString) -> Self {
        self.title = Title {
            text: title.to_string(),
        };
        self
    }

    pub fn chart(mut self, chart: Chart) -> Self {
        self.chart_type = Some(ChartType::Chart);
        self.inner = Some(ChartTypeWrapped::Chart(chart));

        self
    }

    pub fn table(mut self, table: Table) -> Self {
        self.chart_type = Some(ChartType::Table);
        self.inner = Some(ChartTypeWrapped::Table(table));

        self
    }
}

pub struct DashboardBuilder {
    dashboard: Dashboard,
}

impl DashboardBuilder {
    pub fn new(title: &str, desc: &str) -> Self {
        let dashboard = Dashboard::new();
        let dashboard = dashboard.title(&title.to_string());
        let dashboard = dashboard.description(&desc.to_string());

        Self { dashboard }
    }

    pub fn add_table(mut self, title: &str, columns: Vec<String>, rows: Vec<Vec<String>>) -> Self {
        let mut table = Table::new().columns(columns);
        for row in rows {
            table = table.row(row);
        }
        self.dashboard = self
            .dashboard
            .entry(DashboardEntry::new().title(title).table(table));
        self
    }

    pub fn add_bar_chart(
        mut self,
        title: &str,
        hover_title: &str,
        categories: Vec<&str>,
        data: Vec<i64>,
    ) -> Self {
        let chart = Chart::new()
            .legend(Legend::new().show(true))
            .tooltip(Tooltip::new().trigger(Trigger::Axis))
            .x_axis(Axis::new().type_(AxisType::Category).data(categories))
            .y_axis(Axis::new().type_(AxisType::Value))
            .series(Bar::new().name(hover_title).data(data));

        self.dashboard = self
            .dashboard
            .entry(DashboardEntry::new().title(title).chart(chart));
        self
    }

    pub fn add_line_chart(
        mut self,
        title: &str,
        categories: Vec<String>,
        series: Vec<(&str, Vec<i64>)>,
    ) -> Self {
        let mut chart = Chart::new()
            .legend(Legend::new().show(true).left("150px").top("3%"))
            .tooltip(Tooltip::new().trigger(Trigger::Axis))
            .x_axis(Axis::new().type_(AxisType::Category).data(categories))
            .y_axis(Axis::new().type_(AxisType::Value));

        for (name, data) in series {
            chart = chart.series(Line::new().name(name).data(data).area_style(
                AreaStyle::new().color(Color::LinearGradient {
                    x: 0,
                    y: 0,
                    x2: 0,
                    y2: 1,
                    color_stops: vec![
                        ColorStop::new(0, "rgb(84, 112, 198)"),
                        ColorStop::new(1, "rgb(79, 209, 242)"),
                    ],
                }),
            ));
        }

        self.dashboard = self
            .dashboard
            .entry(DashboardEntry::new().title(title).chart(chart));
        self
    }

    pub fn build(self) -> Dashboard {
        self.dashboard
    }
}