charts_rs/charts/
common.rs

1// Licensed under the Apache License, Version 2.0 (the "License");
2// you may not use this file except in compliance with the License.
3// You may obtain a copy of the License at
4//
5//     http://www.apache.org/licenses/LICENSE-2.0
6//
7// Unless required by applicable law or agreed to in writing, software
8// distributed under the License is distributed on an "AS IS" BASIS,
9// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10// See the License for the specific language governing permissions and
11// limitations under the License.
12
13use super::{Box, Color};
14use crate::Point;
15use serde::{Deserialize, Serialize};
16
17#[derive(Clone, PartialEq, Debug, Default, Serialize, Deserialize)]
18pub enum Position {
19    #[default]
20    Left,
21    Top,
22    Right,
23    Bottom,
24    Inside,
25}
26
27#[derive(Serialize, Deserialize, Clone, PartialEq, Debug, Default)]
28pub enum Align {
29    Left,
30    #[default]
31    Center,
32    Right,
33}
34
35#[derive(Serialize, Deserialize, Clone, PartialEq, Debug)]
36pub enum Symbol {
37    None,
38    Circle(f32, Option<Color>),
39}
40
41#[derive(Serialize, Deserialize, Clone, PartialEq, Debug)]
42pub enum SeriesCategory {
43    Line,
44    Bar,
45}
46
47#[derive(Serialize, Deserialize, Clone, PartialEq, Debug, Default)]
48pub enum MarkLineCategory {
49    #[default]
50    Average,
51    Min,
52    Max,
53}
54
55#[derive(Serialize, Deserialize, Clone, PartialEq, Debug, Default)]
56pub enum MarkPointCategory {
57    #[default]
58    Min,
59    Max,
60}
61
62#[derive(Serialize, Deserialize, Clone, PartialEq, Debug, Default)]
63pub struct MarkLine {
64    pub category: MarkLineCategory,
65}
66
67#[derive(Serialize, Deserialize, Clone, PartialEq, Debug, Default)]
68pub struct MarkPoint {
69    pub category: MarkPointCategory,
70}
71
72#[derive(Serialize, Deserialize, Clone, PartialEq, Debug, Default)]
73pub struct Series {
74    // name of series
75    pub name: String,
76    // data list of series
77    pub data: Vec<f32>,
78    // start index of series
79    pub start_index: usize,
80    // index of series
81    pub index: Option<usize>,
82    // y axis index of series
83    pub y_axis_index: usize,
84    // whether to display the label
85    pub label_show: bool,
86    // mark lines
87    pub mark_lines: Vec<MarkLine>,
88    // mark points
89    pub mark_points: Vec<MarkPoint>,
90    // colors of series bar
91    pub colors: Option<Vec<Option<Color>>>,
92    // category of series
93    pub category: Option<SeriesCategory>,
94    // stroke dash array for series
95    pub stroke_dash_array: Option<String>,
96}
97
98#[derive(Clone, PartialEq, Debug, Default)]
99pub struct SeriesLabel {
100    pub point: Point,
101    pub text: String,
102}
103
104impl Series {
105    pub fn new(name: String, data: Vec<f32>) -> Self {
106        Series {
107            name,
108            data,
109            index: None,
110            ..Default::default()
111        }
112    }
113}
114impl From<(&str, Vec<f32>)> for Series {
115    fn from(value: (&str, Vec<f32>)) -> Self {
116        Series::new(value.0.to_string(), value.1)
117    }
118}
119
120#[derive(Serialize, Deserialize, Clone, Debug, Default)]
121pub struct YAxisConfig {
122    pub axis_font_size: f32,
123    pub axis_font_color: Color,
124    pub axis_font_weight: Option<String>,
125    pub axis_stroke_color: Color,
126    pub axis_width: Option<f32>,
127    pub axis_split_number: usize,
128    pub axis_name_gap: f32,
129    pub axis_name_align: Option<Align>,
130    pub axis_margin: Option<Box>,
131    pub axis_formatter: Option<String>,
132    pub axis_min: Option<f32>,
133    pub axis_max: Option<f32>,
134}