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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
use super::canvas;
use super::color::*;
use super::common::*;
use super::component::*;
use super::params::*;
use super::theme::{get_default_theme, get_theme, Theme, DEFAULT_Y_AXIS_WIDTH};
use super::util::*;
use super::Canvas;
use super::Chart;
use crate::charts::measure_text_width_family;
use charts_rs_derive::Chart;
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Clone, Debug, Default, Chart)]
pub struct ScatterChart {
    pub width: f32,
    pub height: f32,
    pub x: f32,
    pub y: f32,
    pub margin: Box,
    pub series_list: Vec<Series>,
    pub font_family: String,
    pub background_color: Color,
    pub is_light: bool,

    // title
    pub title_text: String,
    pub title_font_size: f32,
    pub title_font_color: Color,
    pub title_font_weight: Option<String>,
    pub title_margin: Option<Box>,
    pub title_align: Align,
    pub title_height: f32,

    // sub title
    pub sub_title_text: String,
    pub sub_title_font_size: f32,
    pub sub_title_font_color: Color,
    pub sub_title_font_weight: Option<String>,
    pub sub_title_margin: Option<Box>,
    pub sub_title_align: Align,
    pub sub_title_height: f32,

    // legend
    pub legend_font_size: f32,
    pub legend_font_color: Color,
    pub legend_font_weight: Option<String>,
    pub legend_align: Align,
    pub legend_margin: Option<Box>,
    pub legend_category: LegendCategory,
    pub legend_show: Option<bool>,

    // x axis
    pub x_axis_data: Vec<String>,
    pub x_axis_height: f32,
    pub x_axis_stroke_color: Color,
    pub x_axis_font_size: f32,
    pub x_axis_font_color: Color,
    pub x_axis_font_weight: Option<String>,
    pub x_axis_name_gap: f32,
    pub x_axis_name_rotate: f32,
    pub x_axis_margin: Option<Box>,
    pub x_axis_config: YAxisConfig,
    pub x_boundary_gap: Option<bool>,

    // y axis
    pub y_axis_configs: Vec<YAxisConfig>,

    // grid
    pub grid_stroke_color: Color,
    pub grid_stroke_width: f32,

    // series
    pub series_stroke_width: f32,
    pub series_label_font_color: Color,
    pub series_label_font_size: f32,
    pub series_label_font_weight: Option<String>,
    pub series_label_formatter: String,
    pub series_colors: Vec<Color>,
    pub series_symbol: Option<Symbol>,
    pub series_smooth: bool,
    pub series_fill: bool,

    // symbol
    pub series_symbol_sizes: Vec<f32>,
}

impl ScatterChart {
    /// Creates a scatter chart from json.
    pub fn from_json(data: &str) -> canvas::Result<ScatterChart> {
        let mut s = ScatterChart {
            ..Default::default()
        };
        let value = s.fill_option(data)?;
        s.fill_default();

        if let Some(series_symbol_sizes) = get_f32_slice_from_value(&value, "series_symbol_sizes") {
            s.series_symbol_sizes = series_symbol_sizes;
        }
        let theme = get_string_from_value(&value, "theme").unwrap_or_default();
        if let Some(x_axis_config) = value.get("x_axis_config") {
            s.x_axis_config = get_y_axis_config_from_value(get_theme(&theme), x_axis_config);
        }
        Ok(s)
    }
    /// Creates a scatter chart with  theme.
    pub fn new_with_theme(series_list: Vec<Series>, theme: &str) -> ScatterChart {
        let mut s = ScatterChart {
            series_list,
            ..Default::default()
        };
        let theme = get_theme(theme);
        s.fill_theme(theme);
        s.fill_default();

        s
    }
    fn fill_default(&mut self) {
        if self.y_axis_configs[0].axis_stroke_color.is_zero() {
            self.y_axis_configs[0].axis_stroke_color = self.x_axis_stroke_color;
        }
        if self.x_axis_config.axis_split_number == 0 {
            self.x_axis_config = self.y_axis_configs[0].clone();
        }
        self.x_boundary_gap = Some(false);
    }
    /// Creates a scatter chart with default theme.
    pub fn new(series_list: Vec<Series>) -> ScatterChart {
        ScatterChart::new_with_theme(series_list, &get_default_theme())
    }
    /// Converts scatter chart to svg.
    pub fn svg(&self) -> canvas::Result<String> {
        let mut c = Canvas::new_width_xy(self.width, self.height, self.x, self.y);

        self.render_background(c.child(Box::default()));
        c.margin = self.margin.clone();

        let title_height = self.render_title(c.child(Box::default()));

        let legend_height = self.render_legend(c.child(Box::default()));
        // title 与 legend 取较高的值
        let axis_top = if legend_height > title_height {
            legend_height
        } else {
            title_height
        };

        let y_axis_config = self.get_y_axis_config(0);

        let mut y_axis_data_list = vec![];
        let mut x_axis_data_list = vec![];
        for series in self.series_list.iter() {
            for (index, data) in series.data.iter().enumerate() {
                if index % 2 == 0 {
                    x_axis_data_list.push(*data);
                } else {
                    y_axis_data_list.push(*data);
                }
            }
        }
        let y_axis_values = get_axis_values(AxisValueParams {
            data_list: y_axis_data_list,
            split_number: y_axis_config.axis_split_number,
            reverse: Some(true),
            min: y_axis_config.axis_min,
            max: y_axis_config.axis_max,
            thousands_format: false,
        });
        let y_axis_width = if let Some(value) = y_axis_config.axis_width {
            value
        } else {
            let y_axis_formatter = &y_axis_config.axis_formatter.clone().unwrap_or_default();
            let str = format_string(&y_axis_values.data[0], y_axis_formatter);
            if let Ok(b) =
                measure_text_width_family(&self.font_family, y_axis_config.axis_font_size, &str)
            {
                b.width() + 5.0
            } else {
                DEFAULT_Y_AXIS_WIDTH
            }
        };

        let axis_height = c.height() - self.x_axis_height - axis_top;
        let axis_width = c.width() - y_axis_width;
        // 减去顶部文本区域
        if axis_top > 0.0 {
            c = c.child(Box {
                top: axis_top,
                ..Default::default()
            });
        }

        // grid
        self.render_grid(
            c.child(Box {
                left: y_axis_width,
                ..Default::default()
            }),
            axis_width,
            axis_height,
        );
        let x_axis_width = c.width() - y_axis_width;
        c.child(Box {
            left: y_axis_width,
            ..Default::default()
        })
        .grid(Grid {
            right: x_axis_width,
            bottom: axis_height,
            color: Some(self.grid_stroke_color),
            stroke_width: self.grid_stroke_width,
            verticals: y_axis_config.axis_split_number,
            hidden_verticals: vec![0],
            ..Default::default()
        });

        // y axis
        self.render_y_axis(
            c.child(Box::default()),
            y_axis_values.data.clone(),
            axis_height,
            y_axis_width,
            0,
        );

        // x axis
        let x_axis_values = get_axis_values(AxisValueParams {
            data_list: x_axis_data_list,
            split_number: self.x_axis_config.axis_split_number,
            min: self.x_axis_config.axis_min,
            max: self.x_axis_config.axis_max,
            ..Default::default()
        });
        let x_axis_formatter = &self
            .x_axis_config
            .axis_formatter
            .clone()
            .unwrap_or_default();
        let content_width = c.width() - y_axis_width;
        let content_height = axis_height;
        self.render_x_axis(
            c.child(Box {
                top: c.height() - self.x_axis_height,
                left: y_axis_width,
                ..Default::default()
            }),
            x_axis_values
                .data
                .iter()
                .map(|item| format_string(item, x_axis_formatter))
                .collect(),
            axis_width,
        );

        // render dot
        let mut content_canvas = c.child(Box {
            left: y_axis_width,
            ..Default::default()
        });
        let default_symbol_size = 10.0_f32;
        for (index, series) in self.series_list.iter().enumerate() {
            let mut color = get_color(&self.series_colors, series.index.unwrap_or(index));
            let symbol_size = self
                .series_symbol_sizes
                .get(series.index.unwrap_or(index))
                .unwrap_or(&default_symbol_size);
            color = color.with_alpha(210);
            for chunk in series.data.chunks(2) {
                if chunk.len() != 2 {
                    continue;
                }
                let x = content_width - x_axis_values.get_offset_height(chunk[0], content_width);
                let y = y_axis_values.get_offset_height(chunk[1], content_height);
                content_canvas.circle(Circle {
                    fill: Some(color),
                    cx: x,
                    cy: y,
                    r: *symbol_size,
                    ..Default::default()
                });
            }
        }

        c.svg()
    }
}

#[cfg(test)]
mod tests {
    use super::ScatterChart;
    use crate::Align;
    use pretty_assertions::assert_eq;
    #[test]
    fn scatter_chart_basic() {
        let mut scatter_chart = ScatterChart::new(vec![
            (
                "Female",
                vec![
                    161.2, 51.6, 167.5, 59.0, 159.5, 49.2, 157.0, 63.0, 155.8, 53.6, 170.0, 59.0,
                    159.1, 47.6, 166.0, 69.8, 176.2, 66.8, 160.2, 75.2, 172.5, 55.2, 170.9, 54.2,
                    172.9, 62.5, 153.4, 42.0, 160.0, 50.0, 147.2, 49.8, 168.2, 49.2, 175.0, 73.2,
                    157.0, 47.8, 167.6, 68.8, 159.5, 50.6, 175.0, 82.5, 166.8, 57.2, 176.5, 87.8,
                    170.2, 72.8,
                ],
            )
                .into(),
            (
                "Male",
                vec![
                    174.0, 65.6, 175.3, 71.8, 193.5, 80.7, 186.5, 72.6, 187.2, 78.8, 181.5, 74.8,
                    184.0, 86.4, 184.5, 78.4, 175.0, 62.0, 184.0, 81.6, 180.0, 76.6, 177.8, 83.6,
                    192.0, 90.0, 176.0, 74.6, 174.0, 71.0, 184.0, 79.6, 192.7, 93.8, 171.5, 70.0,
                    173.0, 72.4, 176.0, 85.9, 176.0, 78.8, 180.5, 77.8, 172.7, 66.2, 176.0, 86.4,
                    173.5, 81.8,
                ],
            )
                .into(),
        ]);

        scatter_chart.title_text = "Male and female height and weight distribution".to_string();
        scatter_chart.margin.right = 20.0;
        scatter_chart.title_align = Align::Left;
        scatter_chart.sub_title_text = "Data from: Heinz 2003".to_string();
        scatter_chart.sub_title_align = Align::Left;
        scatter_chart.legend_align = Align::Right;
        scatter_chart.y_axis_configs[0].axis_min = Some(40.0);
        scatter_chart.y_axis_configs[0].axis_max = Some(130.0);
        scatter_chart.y_axis_configs[0].axis_formatter = Some("{c} kg".to_string());

        scatter_chart.x_axis_config.axis_min = Some(140.0);
        scatter_chart.x_axis_config.axis_max = Some(230.0);
        scatter_chart.x_axis_config.axis_formatter = Some("{c} cm".to_string());

        scatter_chart.series_symbol_sizes = vec![6.0, 6.0];

        assert_eq!(
            include_str!("../../asset/scatter_chart/basic.svg"),
            scatter_chart.svg().unwrap()
        );
    }
}