charton 0.5.0

A high-performance, layered charting system for Rust, featuring a flexible data core and multi-backend rendering.
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
use crate::core::guide::LegendPosition;
use crate::prelude::SingleColor;
use crate::visual::color::{ColorMap, ColorPalette};

/// A `Theme` defines the visual "look and feel" of a chart.
///
/// It stores constants for aesthetics (colors, fonts) and layout preferences (margins, spacing).
/// It does NOT store data-specific content like titles or domain limits.
#[derive(Clone)]
pub struct Theme {
    // --- Global Canvas & Layout ---
    /// The fill color of the entire chart background.
    pub(crate) background_color: SingleColor,
    /// Default relative margins for the chart (top, right, bottom, left).
    /// Expressed as a ratio [0.0, 1.0] of the canvas size.
    pub(crate) top_margin: f64,
    pub(crate) right_margin: f64,
    pub(crate) bottom_margin: f64,
    pub(crate) left_margin: f64,
    /// Whether to render axes by default.
    pub(crate) show_axes: bool,

    // --- Main Title Styling ---
    /// Font size for the main chart title.
    pub(crate) title_size: f64,
    /// Font family stack for the main chart title.
    pub(crate) title_family: String,
    /// Text color for the main chart title.
    pub(crate) title_color: SingleColor,

    // --- Axis Title (Label) Styling ---
    /// Font size for axis titles (e.g., "Price").
    pub(crate) label_size: f64,
    /// Font family for axis titles.
    pub(crate) label_family: String,
    /// Text color for axis titles.
    pub(crate) label_color: SingleColor,
    /// Spacing between the axis title and the tick labels.
    pub(crate) label_padding: f64,

    // --- Tick Label Styling (The numbers/categories on axes) ---
    /// Font size for the text next to axis ticks.
    pub(crate) tick_label_size: f64,
    /// Font family for tick labels.
    pub(crate) tick_label_family: String,
    /// Text color for tick labels.
    pub(crate) tick_label_color: SingleColor,
    /// Distance between the tick mark and the tick text.
    pub(crate) tick_label_padding: f64,
    /// Default rotation angle in degrees for X-axis tick labels.
    pub(crate) x_tick_label_angle: f64,
    /// Default rotation angle in degrees for Y-axis tick labels.
    pub(crate) y_tick_label_angle: f64,

    // --- Geometry & Stroke Properties ---
    /// Width of the main axis lines.
    pub(crate) axis_width: f64,
    /// Width of the small tick marks.
    pub(crate) tick_width: f64,
    /// The physical length of the tick marks.
    pub(crate) tick_length: f64,
    /// Minimum pixel spacing between ticks to ensure visual density.
    pub(crate) tick_min_spacing: f64,

    // --- Legend Styling ---
    /// Font size for the legend's title.
    pub(crate) legend_title_size: f64,
    /// Font size for legend item labels.
    pub(crate) legend_label_size: f64,
    /// Font family for all legend text.
    pub(crate) legend_label_family: String,
    /// Text color for all legend text.
    pub(crate) legend_label_color: SingleColor,
    /// Default position of the legend relative to the plot.
    pub(crate) legend_position: LegendPosition,
    /// Spacing between the plot area and the legend.
    pub(crate) legend_margin: f64,
    /// Gap between separate legend blocks (e.g., Color vs Size).
    pub(crate) legend_block_gap: f64,
    /// Vertical gap between items within a legend.
    pub(crate) legend_item_v_gap: f64,
    /// Horizontal gap between columns in a multi-column legend.
    pub(crate) legend_col_h_gap: f64,
    /// Spacing between the legend title and its items.
    pub(crate) legend_title_gap: f64,
    /// Spacing between the legend marker (e.g., circle) and its label text.
    pub(crate) legend_marker_text_gap: f64,

    // --- Layout Defense & Auto-Sizing ---
    /// The minimum size (pixels) the data panel must maintain.
    pub(crate) min_panel_size: f64,
    /// Max ratio of the canvas that axes and margins can occupy.
    pub(crate) panel_defense_ratio: f64,
    /// Reserved pixel buffer for axis labels to prevent cropping.
    pub(crate) axis_reserve_buffer: f64,

    // --- Aesthetic Defaults ---
    /// Default color map for continuous data mapping.
    pub(crate) color_map: ColorMap,
    /// Default categorical palette for discrete data mapping.
    pub(crate) palette: ColorPalette,

    // --- Facet (Subplot) Styling ---
    /// Font size for facet strip labels.
    pub(crate) facet_label_size: f64,
    /// Color for facet label text.
    pub(crate) facet_label_color: SingleColor,
    /// Background fill for the facet strip header.
    pub(crate) facet_strip_fill: SingleColor,
    /// Spacing between individual facet panels.
    pub(crate) facet_spacing: f64,
    /// Padding inside the facet strip.
    pub(crate) facet_strip_padding: f64,

    // --- Polar Chart Defaults ---
    /// Default starting angle for polar charts (e.g., 12 o'clock).
    pub(crate) polar_start_angle: f64,
    /// Default angular span (e.g., 360 degrees).
    pub(crate) polar_end_angle: f64,
    /// Default inner radius ratio (e.g., 0.5 for a donut chart).
    pub(crate) polar_inner_radius: f64,

    /// Color for the grid lines.
    /// Typically a faint gray like #BDBDBD or a semi-transparent version of label_color.
    pub(crate) grid_color: SingleColor,
    /// Width of the grid lines (usually thinner than axis_width).
    pub(crate) grid_width: f64,
}

impl Theme {
    // --- Global Configuration ---

    pub fn with_background_color(mut self, color: impl Into<SingleColor>) -> Self {
        self.background_color = color.into();
        self
    }

    pub fn with_top_margin(mut self, margin: f64) -> Self {
        self.top_margin = margin;
        self
    }

    pub fn with_right_margin(mut self, margin: f64) -> Self {
        self.right_margin = margin;
        self
    }

    pub fn with_bottom_margin(mut self, margin: f64) -> Self {
        self.bottom_margin = margin;
        self
    }

    pub fn with_left_margin(mut self, margin: f64) -> Self {
        self.top_margin = margin;
        self
    }

    pub fn with_show_axes(mut self, show: bool) -> Self {
        self.show_axes = show;
        self
    }

    // --- Title ---

    pub fn with_title_size(mut self, size: f64) -> Self {
        self.title_size = size;
        self
    }

    pub fn with_title_family(mut self, family: impl Into<String>) -> Self {
        self.title_family = family.into();
        self
    }

    pub fn with_title_color(mut self, color: impl Into<SingleColor>) -> Self {
        self.title_color = color.into();
        self
    }

    // --- Axis Label ---

    pub fn with_label_size(mut self, size: f64) -> Self {
        self.label_size = size;
        self
    }

    pub fn with_label_family(mut self, family: impl Into<String>) -> Self {
        self.label_family = family.into();
        self
    }

    pub fn with_label_color(mut self, color: impl Into<SingleColor>) -> Self {
        self.label_color = color.into();
        self
    }

    pub fn with_label_padding(mut self, padding: f64) -> Self {
        self.label_padding = padding;
        self
    }

    // --- Tick Label ---

    pub fn with_tick_label_size(mut self, size: f64) -> Self {
        self.tick_label_size = size;
        self
    }

    pub fn with_tick_label_family(mut self, family: impl Into<String>) -> Self {
        self.tick_label_family = family.into();
        self
    }

    pub fn with_tick_label_color(mut self, color: impl Into<SingleColor>) -> Self {
        self.tick_label_color = color.into();
        self
    }

    pub fn with_tick_label_padding(mut self, padding: f64) -> Self {
        self.tick_label_padding = padding;
        self
    }

    pub fn with_x_tick_label_angle(mut self, angle: f64) -> Self {
        self.x_tick_label_angle = angle;
        self
    }

    pub fn with_y_tick_label_angle(mut self, angle: f64) -> Self {
        self.y_tick_label_angle = angle;
        self
    }

    // --- Geometry Strokes ---

    pub fn with_axis_width(mut self, width: f64) -> Self {
        self.axis_width = width;
        self
    }

    pub fn with_tick_width(mut self, width: f64) -> Self {
        self.tick_width = width;
        self
    }

    pub fn with_tick_length(mut self, length: f64) -> Self {
        self.tick_length = length;
        self
    }

    pub fn with_tick_min_spacing(mut self, spacing: f64) -> Self {
        self.tick_min_spacing = spacing;
        self
    }

    // --- Legend Styling ---

    pub fn with_legend_title_size(mut self, size: f64) -> Self {
        self.legend_title_size = size;
        self
    }

    pub fn with_legend_label_size(mut self, size: f64) -> Self {
        self.legend_label_size = size;
        self
    }

    pub fn with_legend_label_family(mut self, family: impl Into<String>) -> Self {
        self.legend_label_family = family.into();
        self
    }

    pub fn with_legend_label_color(mut self, color: impl Into<SingleColor>) -> Self {
        self.legend_label_color = color.into();
        self
    }

    pub fn with_legend_block_gap(mut self, gap: f64) -> Self {
        self.legend_block_gap = gap;
        self
    }

    pub fn with_legend_item_v_gap(mut self, gap: f64) -> Self {
        self.legend_item_v_gap = gap;
        self
    }

    pub fn with_legend_col_h_gap(mut self, gap: f64) -> Self {
        self.legend_col_h_gap = gap;
        self
    }

    pub fn with_legend_title_gap(mut self, gap: f64) -> Self {
        self.legend_title_gap = gap;
        self
    }

    pub fn with_legend_marker_text_gap(mut self, gap: f64) -> Self {
        self.legend_marker_text_gap = gap;
        self
    }

    // --- Legend Logic ---

    pub fn with_legend_position(mut self, position: LegendPosition) -> Self {
        self.legend_position = position;
        self
    }

    pub fn with_legend_margin(mut self, margin: f64) -> Self {
        self.legend_margin = margin;
        self
    }

    // --- Layout Defense ---

    pub fn with_min_panel_size(mut self, size: f64) -> Self {
        self.min_panel_size = size;
        self
    }

    pub fn with_panel_defense_ratio(mut self, ratio: f64) -> Self {
        self.panel_defense_ratio = ratio;
        self
    }

    pub fn with_axis_reserve_buffer(mut self, buffer: f64) -> Self {
        self.axis_reserve_buffer = buffer;
        self
    }

    // --- Color & Palette Defaults ---

    pub fn with_color_map(mut self, map: ColorMap) -> Self {
        self.color_map = map;
        self
    }

    pub fn with_palette(mut self, palette: ColorPalette) -> Self {
        self.palette = palette;
        self
    }

    // --- Facet Styling ---
    /// The font size for the facet labels (the text in the strip).
    pub fn with_facet_label_size(mut self, size: f64) -> Self {
        self.facet_label_size = size;
        self
    }

    /// The color of the facet label text.
    pub fn with_facet_label_color(mut self, color: impl Into<SingleColor>) -> Self {
        self.facet_label_color = color.into();
        self
    }

    /// The background color of the facet strip (the header box).
    pub fn with_facet_strip_fill(mut self, color: impl Into<SingleColor>) -> Self {
        self.facet_strip_fill = color.into();
        self
    }

    /// The spacing between individual facet panels (both horizontal and vertical).
    pub fn with_facet_spacing(mut self, spacing: f64) -> Self {
        self.facet_spacing = spacing;
        self
    }
    /// The padding inside the facet strip.
    pub fn with_facet_strip_padding(mut self, padding: f64) -> Self {
        self.facet_strip_padding = padding;
        self
    }

    /// Calculates a suggested number of ticks based on the available
    /// physical space and the theme's density settings.
    pub fn suggest_tick_count(&self, available_pixels: f64) -> usize {
        // We ensure at least 2 ticks (start and end) are always present.
        ((available_pixels / self.tick_min_spacing).floor() as usize).max(2)
    }

    pub fn with_grid_color(mut self, color: impl Into<SingleColor>) -> Self {
        self.grid_color = color.into();
        self
    }

    pub fn with_grid_width(mut self, width: f64) -> Self {
        self.grid_width = width;
        self
    }
}

impl Default for Theme {
    fn default() -> Self {
        let font_stack = "Inter, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, 'PingFang SC', 'Microsoft YaHei', Ubuntu, Cantarell, 'Noto Sans', sans-serif".to_string();

        Self {
            background_color: "white".into(),
            top_margin: 0.10,
            right_margin: 0.03,
            bottom_margin: 0.08,
            left_margin: 0.06,
            show_axes: true,

            title_size: 18.0,
            title_family: font_stack.clone(),
            title_color: "#333".into(),

            label_size: 15.0,
            label_family: font_stack.clone(),
            label_color: "#333".into(),
            label_padding: 5.0,

            tick_label_size: 13.0,
            tick_label_family: font_stack.clone(),
            tick_label_color: "#333".into(),
            tick_label_padding: 3.0,

            x_tick_label_angle: 0.0,
            y_tick_label_angle: 0.0,

            axis_width: 1.0,
            tick_width: 1.0,
            tick_length: 6.0,
            tick_min_spacing: 50.0,

            legend_title_size: 14.0,
            legend_label_size: 12.0,
            legend_label_family: font_stack,
            legend_label_color: "#333".into(),
            legend_block_gap: 35.0,
            legend_item_v_gap: 3.0,
            legend_col_h_gap: 15.0,
            legend_title_gap: 7.0,
            legend_marker_text_gap: 8.0,

            legend_position: LegendPosition::Right,
            legend_margin: 15.0,

            min_panel_size: 100.0,
            panel_defense_ratio: 0.2,
            axis_reserve_buffer: 60.0,

            color_map: ColorMap::Viridis,
            palette: ColorPalette::Tab10,

            facet_label_size: 11.0,
            facet_label_color: "#333".into(),
            facet_strip_fill: "lightgray".into(),
            facet_spacing: 10.0,
            facet_strip_padding: 5.0,

            polar_start_angle: -std::f64::consts::FRAC_PI_2,
            polar_end_angle: 3.0 * std::f64::consts::FRAC_PI_2, // start + 2*PI
            polar_inner_radius: 0.0,

            grid_color: " #BDBDBD".into(),
            grid_width: 1.0,
        }
    }
}