liecharts 0.1.0-beta.1

A Rust charting library with PNG and SVG rendering support
Documentation
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
//! 布局元素 - Title、Legend、Axis、Grid 的布局实现

use vello_cpu::kurbo::{Rect, Size};

use super::{LayoutResult, Layoutable, SizeConstraint, measure_text_size, resolve_position};
use crate::{
    model::{Axis, TextStyle},
    option::AxisPosition,
    text::layout_text,
    visual::TextAlign,
};

/// 标题布局元素
pub struct TitleLayout {
    text: String,
    subtext: Option<String>,
    text_style: TextStyle,
    subtext_style: Option<TextStyle>,
    left: crate::model::Position,
    top: crate::model::Position,
    result: Option<LayoutResult>,
}

impl TitleLayout {
    pub fn new(
        text: String,
        subtext: Option<String>,
        text_style: TextStyle,
        subtext_style: Option<TextStyle>,
        left: crate::model::Position,
        top: crate::model::Position,
    ) -> Self {
        Self {
            text,
            subtext,
            text_style,
            subtext_style,
            left,
            top,
            result: None,
        }
    }

    fn calculate_desired_size(&self) -> Size {
        let layout = if let Some(ref subtext) = self.subtext {
            let sub_style = self.subtext_style.clone().unwrap_or_else(|| TextStyle {
                font_size: 12.0,
                ..Default::default()
            });
            let main_with_newline = format!("{}\n", self.text);
            layout_text(
                &[
                    (&main_with_newline, &self.text_style),
                    (subtext, &sub_style),
                ],
                None,
                TextAlign::Center,
            )
        } else {
            layout_text(&[(&self.text, &self.text_style)], None, TextAlign::Center)
        };
        Size::new(layout.width() as f64, layout.height() as f64)
    }
}

impl Layoutable for TitleLayout {
    fn measure(&mut self, constraint: SizeConstraint) -> Size {
        let desired = self.calculate_desired_size();
        let size = constraint.constrain(desired);
        self.result = Some(LayoutResult::new(size));
        size
    }

    fn arrange(&mut self, bounds: Rect) {
        if let Some(ref mut result) = self.result {
            let x = resolve_position(&self.left, bounds.width(), result.desired_size.width);
            let max_y = (bounds.height() - result.desired_size.height).max(0.0);
            let y =
                resolve_position(&self.top, bounds.height(), result.desired_size.height).min(max_y);

            result.bounds = Rect::new(
                bounds.x0 + x,
                bounds.y0 + y,
                bounds.x0 + x + result.desired_size.width,
                bounds.y0 + y + result.desired_size.height,
            );
        }
    }

    fn layout_result(&self) -> Option<&LayoutResult> {
        self.result.as_ref()
    }
}

/// 图例布局元素
pub struct LegendLayout {
    data: Vec<String>,
    orient: crate::model::Orient,
    left: crate::model::Position,
    top: crate::model::Position,
    item_heights: Vec<f64>,
    item_widths: Vec<f64>,
    wrap_cols: usize,
    result: Option<LayoutResult>,
}

impl LegendLayout {
    pub fn new(
        data: Vec<String>,
        orient: crate::model::Orient,
        left: crate::model::Position,
        top: crate::model::Position,
        text_style: TextStyle,
        symbol_size: f64,
        item_height: f64,
    ) -> Self {
        // 预计算每项宽度和高度(与渲染端 LegendComponent 保持一致)
        // 宽度 = 色块 + 间距(5) + 文本宽度 + 右边距(10)
        // 高度取所有项中最大的文本高度,确保横向同行时行高统一
        let mut max_text_height = 0.0f64;
        let item_widths: Vec<f64> = data
            .iter()
            .map(|s| {
                let size = measure_text_size(s, &text_style);
                let text_h = size.height + 8.0;
                if text_h > max_text_height {
                    max_text_height = text_h;
                }
                symbol_size + 5.0 + size.width + 10.0
            })
            .collect();
        let item_height = item_height.max(max_text_height);
        let item_heights: Vec<f64> = data.iter().map(|_| item_height).collect();

        Self {
            data,
            orient,
            left,
            top,
            item_heights,
            item_widths,
            wrap_cols: 0,
            result: None,
        }
    }

    /// 获取每项宽度(用于渲染时对齐)
    pub fn item_widths(&self) -> &[f64] {
        &self.item_widths
    }

    /// 获取每项高度
    pub fn item_heights(&self) -> &[f64] {
        &self.item_heights
    }

    /// 获取换行后的列数
    pub fn wrap_cols(&self) -> usize {
        self.wrap_cols
    }

    /// 计算水平布局换行后的行数
    fn calc_wrap_rows(&self, max_width: f64) -> (usize, Vec<f64>) {
        if self.data.is_empty() {
            return (0, Vec::new());
        }
        let mut rows = 1;
        let mut col_widths = Vec::new();
        let mut current_row: Vec<f64> = Vec::new();
        let mut current_row_width = 0.0;

        for &w in &self.item_widths {
            let gap = if current_row.is_empty() { 0.0 } else { 10.0 };
            if current_row.is_empty() {
                current_row.push(w);
                current_row_width = w;
            } else if current_row_width + gap + w <= max_width {
                current_row.push(w);
                current_row_width += gap + w;
            } else {
                col_widths.push(current_row.iter().copied().fold(0.0f64, f64::max));
                current_row = vec![w];
                current_row_width = w;
                rows += 1;
            }
        }
        if !current_row.is_empty() {
            col_widths.push(current_row.iter().copied().fold(0.0f64, f64::max));
        }
        (rows, col_widths)
    }

    fn calculate_desired_size(&self, max_width: f64) -> Size {
        if self.data.is_empty() {
            return Size::new(0.0, 0.0);
        }

        match self.orient {
            crate::model::Orient::Horizontal => {
                if max_width.is_infinite() {
                    // 不换行:所有项目在一行
                    let total_width: f64 = self.item_widths.iter().sum::<f64>()
                        + 10.0 * (self.data.len().saturating_sub(1)) as f64;
                    let height = self.item_heights[0];
                    Size::new(total_width, height)
                } else {
                    // 换行:计算行数
                    let (rows, _) = self.calc_wrap_rows(max_width);
                    let row_height = self.item_heights[0];
                    Size::new(max_width, rows as f64 * row_height)
                }
            }
            crate::model::Orient::Vertical => {
                let width = self.item_widths.iter().copied().fold(0.0f64, f64::max);
                let height: f64 = self.item_heights.iter().sum::<f64>()
                    + 5.0 * (self.data.len().saturating_sub(1)) as f64;
                Size::new(width, height)
            }
        }
    }
}

impl Layoutable for LegendLayout {
    fn measure(&mut self, constraint: SizeConstraint) -> Size {
        let desired = self.calculate_desired_size(constraint.max_width);
        let size = constraint.constrain(desired);
        // 记录换行列数,供渲染时使用
        if self.orient == crate::model::Orient::Horizontal && constraint.max_width < f64::INFINITY {
            let (_, col_widths) = self.calc_wrap_rows(constraint.max_width);
            self.wrap_cols = col_widths.len();
        } else {
            self.wrap_cols = self.data.len();
        }
        self.result = Some(LayoutResult::new(size));
        size
    }

    fn arrange(&mut self, bounds: Rect) {
        if let Some(ref mut result) = self.result {
            let x = resolve_position(&self.left, bounds.width(), result.desired_size.width);
            let max_y = (bounds.height() - result.desired_size.height).max(0.0);
            let y =
                resolve_position(&self.top, bounds.height(), result.desired_size.height).min(max_y);

            result.bounds = Rect::new(
                bounds.x0 + x,
                bounds.y0 + y,
                bounds.x0 + x + result.desired_size.width,
                bounds.y0 + y + result.desired_size.height,
            );
        }
    }

    fn layout_result(&self) -> Option<&LayoutResult> {
        self.result.as_ref()
    }
}

/// 坐标轴布局元素
///
/// 持有 `model::Axis` 配置,在 measure 阶段计算标签和名称尺寸,
/// 通过 `total_outside_extent()` 统一计算轴线外侧的总占用尺寸。
pub struct AxisLayout {
    config: Axis,
    /// 标签区域在垂直于轴线方向的最大尺寸(垂直轴=宽度,水平轴=高度)
    label_extent: f64,
    result: Option<LayoutResult>,
}

impl AxisLayout {
    pub fn new(config: Axis) -> Self {
        // 预测量标签最大尺寸
        let label_extent = match config.position {
            AxisPosition::Left | AxisPosition::Right => config
                .data
                .as_ref()
                .map(|d| {
                    d.iter()
                        .map(|s| {
                            measure_text_size(
                                s,
                                &TextStyle {
                                    font_size: config.axis_label.font_size,
                                    font_family: config.axis_label.font_family.clone(),
                                    ..Default::default()
                                },
                            )
                            .width
                        })
                        .fold(0.0, f64::max)
                })
                .unwrap_or_else(|| {
                    // 数值轴:用 "9999" 估算最宽4位数标签
                    measure_text_size(
                        "9999",
                        &TextStyle {
                            font_size: config.axis_label.font_size,
                            font_family: config.axis_label.font_family.clone(),
                            ..Default::default()
                        },
                    )
                    .width
                    .max(30.0)
                }),
            AxisPosition::Bottom | AxisPosition::Top => config
                .data
                .as_ref()
                .map(|d| {
                    d.iter()
                        .map(|s| {
                            measure_text_size(
                                s,
                                &TextStyle {
                                    font_size: config.axis_label.font_size,
                                    font_family: config.axis_label.font_family.clone(),
                                    ..Default::default()
                                },
                            )
                            .height
                        })
                        .fold(0.0, f64::max)
                })
                .unwrap_or(config.axis_label.font_size * 2.0),
        };

        Self {
            config,
            label_extent,
            result: None,
        }
    }

    /// 计算轴线外侧方向的总占用尺寸(垂直于轴线)
    ///
    /// 统一公式:tick_length + label_padding + label_extent
    /// 名称不参与布局计算(与 ECharts 行为一致),仅用于渲染定位。
    fn total_outside_extent(&self) -> f64 {
        self.config.tick_length + self.config.label_padding + self.label_extent
    }
}

impl Layoutable for AxisLayout {
    fn measure(&mut self, constraint: SizeConstraint) -> Size {
        let total = self.total_outside_extent();
        let size = match self.config.position {
            AxisPosition::Bottom | AxisPosition::Top => Size::new(
                constraint.max_width,
                total.clamp(constraint.min_height, constraint.max_height),
            ),
            AxisPosition::Left | AxisPosition::Right => Size::new(
                total.clamp(constraint.min_width, constraint.max_width),
                constraint.max_height,
            ),
        };
        self.result = Some(LayoutResult::new(size));
        size
    }

    fn arrange(&mut self, bounds: Rect) {
        if let Some(ref mut result) = self.result {
            result.bounds = bounds;
        }
    }

    fn layout_result(&self) -> Option<&LayoutResult> {
        self.result.as_ref()
    }

    fn axis_position(&self) -> Option<AxisPosition> {
        Some(self.config.position)
    }
}

/// 网格布局元素
pub struct GridLayout {
    result: Option<LayoutResult>,
}

impl Default for GridLayout {
    fn default() -> Self {
        Self::new()
    }
}

impl GridLayout {
    pub fn new() -> Self {
        Self { result: None }
    }
}

impl Layoutable for GridLayout {
    fn measure(&mut self, constraint: SizeConstraint) -> Size {
        // 网格占据所有可用空间
        let size = Size::new(constraint.max_width, constraint.max_height);
        self.result = Some(LayoutResult::new(size));
        size
    }

    fn arrange(&mut self, bounds: Rect) {
        if let Some(ref mut result) = self.result {
            result.bounds = bounds;
        }
    }

    fn layout_result(&self) -> Option<&LayoutResult> {
        self.result.as_ref()
    }
}