esoc-chart 0.1.0

High-level charting API built on esoc-gfx — matplotlib-equivalent for Rust
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
// SPDX-License-Identifier: MIT OR Apache-2.0
//! Rendering pipeline: Figure → Canvas.

use esoc_gfx::canvas::Canvas;
use esoc_gfx::element::{DrawElement, Element};
use esoc_gfx::geom::Rect;
use esoc_gfx::layer::Layer;
use esoc_gfx::style::{Fill, FontStyle, Stroke, TextAnchor};
use esoc_gfx::transform::{AxisTransform, CoordinateTransform, ViewportTransform};

use crate::axes::Axes;
use crate::axis::Scale;
use crate::error::Result;
use crate::figure::Figure;
use crate::legend::{render_legend, LegendEntry};
use crate::series::DataBounds;

/// Margins for plot area (in pixels).
struct Margins {
    top: f64,
    right: f64,
    bottom: f64,
    left: f64,
}

/// Render a figure to a canvas.
pub fn render_figure(fig: &Figure) -> Result<Canvas> {
    let mut canvas = Canvas::new(fig.width, fig.height);

    // Background
    canvas.draw(
        Element::filled_rect(
            Rect::new(0.0, 0.0, fig.width, fig.height),
            Fill::Solid(fig.theme.background),
        ),
        Layer::Background,
    );

    // Figure title
    let title_offset = if let Some(title) = &fig.title {
        let font = FontStyle {
            family: fig.theme.font_family.clone(),
            size: fig.theme.title_font_size,
            weight: 700,
            color: fig.theme.foreground,
            anchor: TextAnchor::Middle,
        };
        canvas.add(DrawElement::text(
            fig.width / 2.0,
            fig.theme.title_font_size + 8.0,
            title,
            font,
            Layer::Annotations,
        ));
        fig.theme.title_font_size + 16.0
    } else {
        8.0
    };

    // For now, single-axes layout (fill available space)
    let n_axes = fig.axes.len().max(1);
    let avail_height = fig.height - title_offset;
    let axes_height = avail_height / n_axes as f64;

    for (i, axes) in fig.axes.iter().enumerate() {
        let axes_y = title_offset + i as f64 * axes_height;
        let axes_rect = Rect::new(0.0, axes_y, fig.width, axes_height);
        render_axes(&mut canvas, axes, axes_rect, &fig.theme)?;
    }

    Ok(canvas)
}

fn render_axes(
    canvas: &mut Canvas,
    axes: &Axes,
    bounds: Rect,
    theme: &crate::theme::Theme,
) -> Result<()> {
    // Compute margins
    let margins = compute_margins(axes, theme);
    let plot_area = Rect::new(
        bounds.x + margins.left,
        bounds.y + margins.top,
        (bounds.width - margins.left - margins.right).max(1.0),
        (bounds.height - margins.top - margins.bottom).max(1.0),
    );

    // Merge data bounds from all series
    let data_bounds = axes
        .series
        .iter()
        .map(|s| s.data_bounds())
        .reduce(DataBounds::union)
        .unwrap_or(DataBounds::new(0.0, 1.0, 0.0, 1.0));

    // Apply manual range overrides or pad
    let (x_min, x_max) = axes.x_config.range.unwrap_or_else(|| {
        let b = data_bounds.pad(0.05);
        (b.x_min, b.x_max)
    });
    let (y_min, y_max) = axes.y_config.range.unwrap_or_else(|| {
        let b = data_bounds.pad(0.05);
        (b.y_min, b.y_max)
    });

    // Build transforms
    let x_transform = match &axes.x_config.scale {
        Scale::Linear => AxisTransform::Linear {
            min: x_min,
            max: x_max,
        },
        Scale::Log => AxisTransform::Log {
            min: x_min,
            max: x_max,
        },
        Scale::Categorical(labels) => AxisTransform::Categorical {
            count: labels.len(),
        },
    };
    let y_transform = match &axes.y_config.scale {
        Scale::Linear => AxisTransform::Linear {
            min: y_min,
            max: y_max,
        },
        Scale::Log => AxisTransform::Log {
            min: y_min,
            max: y_max,
        },
        Scale::Categorical(labels) => AxisTransform::Categorical {
            count: labels.len(),
        },
    };
    let viewport = ViewportTransform::new(plot_area);
    let coord_transform = CoordinateTransform::new(x_transform, y_transform, viewport);

    // Grid lines
    if theme.show_grid {
        render_grid(canvas, &plot_area, x_min, x_max, y_min, y_max, axes, theme);
    }

    // Axis frame
    render_axis_frame(canvas, &plot_area, x_min, x_max, y_min, y_max, axes, theme);

    // Data series
    for (i, series) in axes.series.iter().enumerate() {
        series.render(canvas, &coord_transform, theme, i);
    }

    // Axes title
    if let Some(title) = &axes.title {
        let font = FontStyle {
            family: theme.font_family.clone(),
            size: theme.title_font_size * 0.9,
            weight: 700,
            color: theme.foreground,
            anchor: TextAnchor::Middle,
        };
        canvas.add(DrawElement::text(
            plot_area.x + plot_area.width / 2.0,
            plot_area.y - 8.0,
            title,
            font,
            Layer::Annotations,
        ));
    }

    // X-axis label
    if let Some(label) = &axes.x_config.label {
        let font = FontStyle {
            family: theme.font_family.clone(),
            size: theme.label_font_size,
            weight: 400,
            color: theme.foreground,
            anchor: TextAnchor::Middle,
        };
        canvas.add(DrawElement::text(
            plot_area.x + plot_area.width / 2.0,
            plot_area.bottom() + margins.bottom - 8.0,
            label,
            font,
            Layer::Annotations,
        ));
    }

    // Y-axis label (rotated)
    if let Some(label) = &axes.y_config.label {
        let font = FontStyle {
            family: theme.font_family.clone(),
            size: theme.label_font_size,
            weight: 400,
            color: theme.foreground,
            anchor: TextAnchor::Middle,
        };
        let lx = plot_area.x - margins.left + theme.label_font_size + 4.0;
        let ly = plot_area.y + plot_area.height / 2.0;
        canvas.add(DrawElement::new(
            Element::Text {
                position: esoc_gfx::geom::Point::new(lx, ly),
                content: label.clone(),
                font,
                rotation: Some(-90.0),
            },
            Layer::Annotations,
        ));
    }

    // Legend
    if axes.show_legend {
        let entries: Vec<LegendEntry> = axes
            .series
            .iter()
            .enumerate()
            .filter_map(|(i, s)| {
                s.label().map(|l| LegendEntry {
                    label: l.to_string(),
                    color: theme.palette.get(i),
                })
            })
            .collect();
        if !entries.is_empty() {
            render_legend(canvas, plot_area, &entries, axes.legend_position, theme);
        }
    }

    Ok(())
}

fn compute_margins(axes: &Axes, theme: &crate::theme::Theme) -> Margins {
    let top = if axes.title.is_some() {
        theme.title_font_size * 1.5 + 10.0
    } else {
        20.0
    };
    let bottom = if axes.x_config.label.is_some() {
        theme.tick_font_size * 1.5 + theme.label_font_size + 20.0
    } else {
        theme.tick_font_size * 1.5 + 20.0
    };
    let left = if axes.y_config.label.is_some() {
        theme.tick_font_size * 4.0 + theme.label_font_size + 15.0
    } else {
        theme.tick_font_size * 4.0 + 15.0
    };
    let right = 20.0;

    Margins {
        top,
        right,
        bottom,
        left,
    }
}

fn render_grid(
    canvas: &mut Canvas,
    plot_area: &Rect,
    x_min: f64,
    x_max: f64,
    y_min: f64,
    y_max: f64,
    axes: &Axes,
    theme: &crate::theme::Theme,
) {
    let grid_stroke = Stroke::solid(theme.grid_color, theme.grid_width);

    // X grid
    let x_ticks = crate::axis::nice_ticks(x_min, x_max, axes.x_config.tick_count);
    for &pos in &x_ticks.positions {
        if pos < x_min || pos > x_max {
            continue;
        }
        let t = if (x_max - x_min).abs() < 1e-15 {
            0.5
        } else {
            (pos - x_min) / (x_max - x_min)
        };
        let px = plot_area.x + t * plot_area.width;
        canvas.add(DrawElement::line(
            px,
            plot_area.y,
            px,
            plot_area.bottom(),
            grid_stroke.clone(),
            Layer::Grid,
        ));
    }

    // Y grid
    let y_ticks = crate::axis::nice_ticks(y_min, y_max, axes.y_config.tick_count);
    for &pos in &y_ticks.positions {
        if pos < y_min || pos > y_max {
            continue;
        }
        let t = if (y_max - y_min).abs() < 1e-15 {
            0.5
        } else {
            (pos - y_min) / (y_max - y_min)
        };
        let py = plot_area.bottom() - t * plot_area.height;
        canvas.add(DrawElement::line(
            plot_area.x,
            py,
            plot_area.right(),
            py,
            grid_stroke.clone(),
            Layer::Grid,
        ));
    }
}

fn render_axis_frame(
    canvas: &mut Canvas,
    plot_area: &Rect,
    x_min: f64,
    x_max: f64,
    y_min: f64,
    y_max: f64,
    axes: &Axes,
    theme: &crate::theme::Theme,
) {
    let axis_stroke = Stroke::solid(theme.foreground, theme.axis_width);

    // Bottom axis line
    canvas.add(DrawElement::line(
        plot_area.x,
        plot_area.bottom(),
        plot_area.right(),
        plot_area.bottom(),
        axis_stroke.clone(),
        Layer::Grid,
    ));
    // Left axis line
    canvas.add(DrawElement::line(
        plot_area.x,
        plot_area.y,
        plot_area.x,
        plot_area.bottom(),
        axis_stroke,
        Layer::Grid,
    ));

    // X tick marks and labels
    let x_ticks = crate::axis::nice_ticks(x_min, x_max, axes.x_config.tick_count);
    let tick_font = FontStyle {
        family: theme.font_family.clone(),
        size: theme.tick_font_size,
        weight: 400,
        color: theme.foreground,
        anchor: TextAnchor::Middle,
    };

    for (i, &pos) in x_ticks.positions.iter().enumerate() {
        if pos < x_min || pos > x_max {
            continue;
        }
        let t = if (x_max - x_min).abs() < 1e-15 {
            0.5
        } else {
            (pos - x_min) / (x_max - x_min)
        };
        let px = plot_area.x + t * plot_area.width;

        // Tick mark
        canvas.add(DrawElement::line(
            px,
            plot_area.bottom(),
            px,
            plot_area.bottom() + 5.0,
            Stroke::solid(theme.foreground, 0.5),
            Layer::Grid,
        ));

        // Tick label
        let label = axes
            .x_config
            .tick_labels
            .as_ref()
            .and_then(|tl| tl.get(i))
            .unwrap_or(&x_ticks.labels[i]);
        canvas.add(DrawElement::text(
            px,
            plot_area.bottom() + 5.0 + theme.tick_font_size,
            label,
            tick_font.clone(),
            Layer::Grid,
        ));
    }

    // Y tick marks and labels
    let y_ticks = crate::axis::nice_ticks(y_min, y_max, axes.y_config.tick_count);
    let y_tick_font = FontStyle {
        family: theme.font_family.clone(),
        size: theme.tick_font_size,
        weight: 400,
        color: theme.foreground,
        anchor: TextAnchor::End,
    };

    for (i, &pos) in y_ticks.positions.iter().enumerate() {
        if pos < y_min || pos > y_max {
            continue;
        }
        let t = if (y_max - y_min).abs() < 1e-15 {
            0.5
        } else {
            (pos - y_min) / (y_max - y_min)
        };
        let py = plot_area.bottom() - t * plot_area.height;

        // Tick mark
        canvas.add(DrawElement::line(
            plot_area.x - 5.0,
            py,
            plot_area.x,
            py,
            Stroke::solid(theme.foreground, 0.5),
            Layer::Grid,
        ));

        // Tick label
        let label = axes
            .y_config
            .tick_labels
            .as_ref()
            .and_then(|tl| tl.get(i))
            .unwrap_or(&y_ticks.labels[i]);
        canvas.add(DrawElement::text(
            plot_area.x - 8.0,
            py + theme.tick_font_size * 0.35,
            label,
            y_tick_font.clone(),
            Layer::Grid,
        ));
    }
}

#[cfg(test)]
mod tests {
    use crate::figure::Figure;

    #[test]
    fn test_render_empty_figure() {
        let mut fig = Figure::new().title("Empty");
        fig.add_axes();
        let svg = fig.to_svg().unwrap();
        assert!(svg.contains("<svg"));
        assert!(svg.contains("</svg>"));
    }

    #[test]
    fn test_render_scatter_svg() {
        let mut fig = Figure::new().size(400.0, 300.0).title("Scatter Test");
        let ax = fig.add_axes();
        ax.x_label("X").y_label("Y");
        ax.scatter(&[1.0, 2.0, 3.0, 4.0], &[1.0, 4.0, 2.0, 3.0])
            .label("data")
            .done();
        let svg = fig.to_svg().unwrap();
        assert!(svg.contains("<circle"));
        assert!(svg.contains("Scatter Test"));
    }

    #[test]
    fn test_render_line_svg() {
        let mut fig = Figure::new();
        let ax = fig.add_axes();
        ax.line(&[0.0, 1.0, 2.0], &[0.0, 1.0, 0.5])
            .label("trend")
            .done();
        let svg = fig.to_svg().unwrap();
        assert!(svg.contains("<polyline"));
    }
}