plotkit-core 0.2.0

Core types and logic for the plotkit plotting library
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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
//! Theme system controlling all visual defaults.
//!
//! A plot rendered with zero custom styling must look professional. The
//! [`Theme::default()`] configuration follows the Visual Design Brief:
//!
//! - White background, despined axes (top + right hidden), light grid behind data.
//! - Tableau-10 categorical palette, viridis continuous colormap.
//! - Outward ticks, readable font sizes (title 14 pt bold, labels 11 pt, ticks 9 pt).
//!
//! Additional built-in themes are available via [`Theme::dark()`],
//! [`Theme::seaborn()`], [`Theme::ggplot()`], and [`Theme::publication()`].

use crate::primitives::{Color, FontWeight};

// ---------------------------------------------------------------------------
// LineStyle
// ---------------------------------------------------------------------------

/// Line drawing style.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum LineStyle {
    /// A continuous solid line.
    Solid,
    /// A dashed line (e.g. `[6, 4]`).
    Dashed,
    /// A dotted line (e.g. `[2, 2]`).
    Dotted,
    /// Alternating long dash and dot (e.g. `[6, 3, 2, 3]`).
    DashDot,
}

// ---------------------------------------------------------------------------
// Marker
// ---------------------------------------------------------------------------

/// Scatter plot marker shape.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum Marker {
    /// Filled circle.
    Circle,
    /// Filled square.
    Square,
    /// Filled upward-pointing triangle.
    Triangle,
    /// Filled diamond (rotated square).
    Diamond,
    /// Axis-aligned plus sign (stroked, not filled).
    Plus,
    /// Diagonal cross / X (stroked, not filled).
    Cross,
    /// Five-pointed star.
    Star,
    /// A single pixel-sized point (smallest possible marker).
    Point,
}

// ---------------------------------------------------------------------------
// Loc (legend location)
// ---------------------------------------------------------------------------

/// Legend location relative to the axes area.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum Loc {
    /// Automatically choose the location that overlaps the fewest data points.
    Best,
    /// Upper-right corner.
    UpperRight,
    /// Upper-left corner.
    UpperLeft,
    /// Lower-left corner.
    LowerLeft,
    /// Lower-right corner.
    LowerRight,
    /// Centered on the right edge.
    Right,
    /// Centered on the left edge.
    CenterLeft,
    /// Centered on the right edge (alias kept for symmetry).
    CenterRight,
    /// Centered on the bottom edge.
    LowerCenter,
    /// Centered on the top edge.
    UpperCenter,
    /// Dead center of the axes area.
    Center,
}

// ---------------------------------------------------------------------------
// TickDirection
// ---------------------------------------------------------------------------

/// Direction in which axis tick marks extend from the spine.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TickDirection {
    /// Ticks extend outward, away from the data area.
    Outward,
    /// Ticks extend inward, into the data area.
    Inward,
}

// ---------------------------------------------------------------------------
// Theme
// ---------------------------------------------------------------------------

/// Visual theme controlling all rendering defaults.
///
/// Every visual parameter that a renderer or layout engine might need lives
/// here. Chart builders read from the active theme to fill in any value the
/// user did not override explicitly.
#[derive(Debug, Clone)]
pub struct Theme {
    /// Background color for the entire figure (outside the axes area).
    pub figure_background: Color,
    /// Background color for the axes face (the data-drawing region).
    pub axes_background: Color,

    // -- Grid ---------------------------------------------------------------
    /// Color of major grid lines.
    pub grid_color: Color,
    /// Width (in px) of major grid lines.
    pub grid_width: f64,
    /// Whether the grid is shown by default (line/scatter: true; bar/hist: false).
    pub show_grid: bool,

    // -- Spines -------------------------------------------------------------
    /// Color of visible axis spines.
    pub spine_color: Color,
    /// Width (in px) of axis spines.
    pub spine_width: f64,
    /// Whether the top spine is drawn.
    pub show_top_spine: bool,
    /// Whether the right spine is drawn.
    pub show_right_spine: bool,
    /// Whether the bottom spine is drawn.
    pub show_bottom_spine: bool,
    /// Whether the left spine is drawn.
    pub show_left_spine: bool,

    // -- Ticks --------------------------------------------------------------
    /// Color of tick marks and tick labels.
    pub tick_color: Color,
    /// Length (in px) of major tick marks.
    pub tick_length: f64,
    /// Direction ticks extend from the spine.
    pub tick_direction: TickDirection,
    /// Font size (in pt) for tick labels.
    pub tick_label_size: f64,

    // -- Labels & Title -----------------------------------------------------
    /// Font size (in pt) for axis labels.
    pub axis_label_size: f64,
    /// Font size (in pt) for the plot title.
    pub title_size: f64,
    /// Font weight for the plot title.
    pub title_weight: FontWeight,
    /// Color used for all text (titles, labels, tick labels).
    pub text_color: Color,

    // -- Data elements ------------------------------------------------------
    /// Default line width (in px) for line plots.
    pub line_width: f64,
    /// Default marker diameter (in px) for scatter plots.
    pub marker_size: f64,
    /// Default marker opacity (0.0 = fully transparent, 1.0 = fully opaque).
    pub marker_alpha: f64,

    // -- Palette ------------------------------------------------------------
    /// Categorical color cycle used when the user does not specify colors.
    pub color_cycle: Vec<Color>,

    // -- Font ---------------------------------------------------------------
    /// Optional font family override. `None` means the renderer picks its
    /// built-in default (typically a clean sans-serif such as Helvetica).
    pub font_family: Option<String>,
}

// ---------------------------------------------------------------------------
// Tableau-10 palette (convenience constant)
// ---------------------------------------------------------------------------

/// The Tableau-10 categorical palette as a fixed-size array.
const TABLEAU_10: [Color; 10] = Color::TABLEAU_10;

// ---------------------------------------------------------------------------
// Default theme
// ---------------------------------------------------------------------------

impl Default for Theme {
    /// Returns the canonical default theme matching the Visual Design Brief.
    ///
    /// - Background: `#FFFFFF`, axes face: `#FFFFFF`
    /// - Grid: `#E6E6E6`, 1 px, shown by default
    /// - Spines: `#333333`, 1 px, top + right hidden (despine look)
    /// - Ticks: outward, 4 px, `#333333`
    /// - Font sizes: title 14 pt bold, axis labels 11 pt, tick labels 9 pt
    /// - Text color: `#333333`
    /// - Line width 1.5 px, marker 6 px diameter, marker alpha 0.8
    /// - Tableau-10 color cycle
    fn default() -> Self {
        let spine = Color::rgb(0x33, 0x33, 0x33);

        Self {
            figure_background: Color::WHITE,
            axes_background: Color::WHITE,

            grid_color: Color::rgb(0xE6, 0xE6, 0xE6),
            grid_width: 1.0,
            show_grid: true,

            spine_color: spine,
            spine_width: 1.0,
            show_top_spine: false,
            show_right_spine: false,
            show_bottom_spine: true,
            show_left_spine: true,

            tick_color: spine,
            tick_length: 4.0,
            tick_direction: TickDirection::Outward,
            tick_label_size: 9.0,

            axis_label_size: 11.0,
            title_size: 14.0,
            title_weight: FontWeight::Bold,
            text_color: spine,

            line_width: 1.5,
            marker_size: 6.0,
            marker_alpha: 0.8,

            color_cycle: TABLEAU_10.to_vec(),

            font_family: None,
        }
    }
}

// ---------------------------------------------------------------------------
// Named themes
// ---------------------------------------------------------------------------

impl Theme {
    /// Dark theme with a near-black background and bright, neon-ish data colors.
    ///
    /// Suited for dashboards and presentations on dark backgrounds.
    pub fn dark() -> Self {
        let bg = Color::rgb(0x1C, 0x1C, 0x1C);
        let text = Color::rgb(0xE0, 0xE0, 0xE0);
        let grid = Color::rgb(0x3A, 0x3A, 0x3A);
        let spine = Color::rgb(0x55, 0x55, 0x55);

        // Bright / neon-ish palette optimised for dark backgrounds.
        let cycle = vec![
            Color::rgb(0x00, 0xD4, 0xFF), // cyan
            Color::rgb(0xFF, 0x6F, 0x61), // coral-red
            Color::rgb(0x7B, 0xED, 0x72), // lime-green
            Color::rgb(0xFF, 0xA6, 0x00), // amber
            Color::rgb(0xD1, 0x7D, 0xFF), // violet
            Color::rgb(0xFF, 0xE1, 0x00), // yellow
            Color::rgb(0x00, 0xFF, 0xAB), // mint
            Color::rgb(0xFF, 0x4D, 0xA6), // hot-pink
            Color::rgb(0x48, 0xBF, 0xE3), // sky-blue
            Color::rgb(0xE8, 0xE8, 0xE8), // light-grey
        ];

        Self {
            figure_background: bg,
            axes_background: bg,

            grid_color: grid,
            grid_width: 1.0,
            show_grid: true,

            spine_color: spine,
            spine_width: 1.0,
            show_top_spine: false,
            show_right_spine: false,
            show_bottom_spine: true,
            show_left_spine: true,

            tick_color: text,
            tick_length: 4.0,
            tick_direction: TickDirection::Outward,
            tick_label_size: 9.0,

            axis_label_size: 11.0,
            title_size: 14.0,
            title_weight: FontWeight::Bold,
            text_color: text,

            line_width: 1.5,
            marker_size: 6.0,
            marker_alpha: 0.9,

            color_cycle: cycle,

            font_family: None,
        }
    }

    /// Seaborn-inspired theme with a tinted axes background and white grid.
    ///
    /// Mimics the popular seaborn `"whitegrid"` aesthetic: a pale blue-grey
    /// axes face (`#EAEAF2`) with white grid lines over it.
    pub fn seaborn() -> Self {
        let text = Color::rgb(0x33, 0x33, 0x33);
        let axes_bg = Color::rgb(0xEA, 0xEA, 0xF2);

        Self {
            figure_background: Color::WHITE,
            axes_background: axes_bg,

            grid_color: Color::WHITE,
            grid_width: 1.0,
            show_grid: true,

            spine_color: Color::rgb(0xCC, 0xCC, 0xCC),
            spine_width: 1.0,
            show_top_spine: false,
            show_right_spine: false,
            show_bottom_spine: true,
            show_left_spine: true,

            tick_color: text,
            tick_length: 0.0, // seaborn typically hides tick marks
            tick_direction: TickDirection::Outward,
            tick_label_size: 9.0,

            axis_label_size: 11.0,
            title_size: 14.0,
            title_weight: FontWeight::Bold,
            text_color: text,

            line_width: 1.5,
            marker_size: 6.0,
            marker_alpha: 0.8,

            color_cycle: TABLEAU_10.to_vec(),

            font_family: None,
        }
    }

    /// ggplot2-inspired theme with a grey panel and white grid.
    ///
    /// Reproduces the characteristic look of R's ggplot2: a medium-grey panel
    /// (`#E5E5E5`), white major grid lines, all four spines hidden, and the
    /// ggplot2 default qualitative palette.
    pub fn ggplot() -> Self {
        let panel = Color::rgb(0xE5, 0xE5, 0xE5);
        let text = Color::rgb(0x30, 0x30, 0x30);

        // Classic ggplot2 qualitative palette (first 8 hues at C=100, L=65).
        let cycle = vec![
            Color::rgb(0xF8, 0x76, 0x6D), // red
            Color::rgb(0xA3, 0xA5, 0x00), // olive-yellow
            Color::rgb(0x00, 0xBA, 0x38), // green
            Color::rgb(0x00, 0xBF, 0xC4), // teal
            Color::rgb(0x61, 0x9C, 0xFF), // blue
            Color::rgb(0xF5, 0x64, 0xE3), // magenta
            Color::rgb(0xFF, 0x64, 0xB0), // pink
            Color::rgb(0xB7, 0x9F, 0x00), // gold
        ];

        Self {
            figure_background: Color::WHITE,
            axes_background: panel,

            grid_color: Color::WHITE,
            grid_width: 1.0,
            show_grid: true,

            // ggplot2 hides all spines by default.
            spine_color: Color::WHITE,
            spine_width: 0.0,
            show_top_spine: false,
            show_right_spine: false,
            show_bottom_spine: false,
            show_left_spine: false,

            tick_color: text,
            tick_length: 0.0, // no visible ticks in ggplot2 default
            tick_direction: TickDirection::Outward,
            tick_label_size: 9.0,

            axis_label_size: 11.0,
            title_size: 14.0,
            title_weight: FontWeight::Bold,
            text_color: text,

            line_width: 1.0,
            marker_size: 5.0,
            marker_alpha: 1.0,

            color_cycle: cycle,

            font_family: None,
        }
    }

    /// Publication-ready theme: crisp, minimal, and suitable for print.
    ///
    /// - Pure white background, no grid.
    /// - All four spines visible but thin (0.5 px) in near-black.
    /// - Inward ticks for a compact footprint.
    /// - Serif font family for journal aesthetics.
    pub fn publication() -> Self {
        let ink = Color::rgb(0x1A, 0x1A, 0x1A);

        Self {
            figure_background: Color::WHITE,
            axes_background: Color::WHITE,

            grid_color: Color::rgb(0xD0, 0xD0, 0xD0),
            grid_width: 0.5,
            show_grid: false,

            spine_color: ink,
            spine_width: 0.5,
            show_top_spine: true,
            show_right_spine: true,
            show_bottom_spine: true,
            show_left_spine: true,

            tick_color: ink,
            tick_length: 3.0,
            tick_direction: TickDirection::Inward,
            tick_label_size: 8.0,

            axis_label_size: 10.0,
            title_size: 12.0,
            title_weight: FontWeight::Bold,
            text_color: ink,

            line_width: 1.0,
            marker_size: 4.0,
            marker_alpha: 1.0,

            color_cycle: TABLEAU_10.to_vec(),

            font_family: Some("serif".to_string()),
        }
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn default_theme_background_is_white() {
        let t = Theme::default();
        assert_eq!(t.figure_background, Color::WHITE);
        assert_eq!(t.axes_background, Color::WHITE);
    }

    #[test]
    fn default_theme_despine_look() {
        let t = Theme::default();
        assert!(!t.show_top_spine);
        assert!(!t.show_right_spine);
        assert!(t.show_bottom_spine);
        assert!(t.show_left_spine);
    }

    #[test]
    fn default_theme_grid() {
        let t = Theme::default();
        assert_eq!(t.grid_color, Color::rgb(0xE6, 0xE6, 0xE6));
        assert!((t.grid_width - 1.0).abs() < f64::EPSILON);
        assert!(t.show_grid);
    }

    #[test]
    fn default_theme_spines() {
        let t = Theme::default();
        let expected = Color::rgb(0x33, 0x33, 0x33);
        assert_eq!(t.spine_color, expected);
        assert!((t.spine_width - 1.0).abs() < f64::EPSILON);
    }

    #[test]
    fn default_theme_ticks() {
        let t = Theme::default();
        assert_eq!(t.tick_color, Color::rgb(0x33, 0x33, 0x33));
        assert!((t.tick_length - 4.0).abs() < f64::EPSILON);
        assert_eq!(t.tick_direction, TickDirection::Outward);
    }

    #[test]
    fn default_theme_font_sizes() {
        let t = Theme::default();
        assert!((t.tick_label_size - 9.0).abs() < f64::EPSILON);
        assert!((t.axis_label_size - 11.0).abs() < f64::EPSILON);
        assert!((t.title_size - 14.0).abs() < f64::EPSILON);
        assert_eq!(t.title_weight, FontWeight::Bold);
    }

    #[test]
    fn default_theme_text_color() {
        let t = Theme::default();
        assert_eq!(t.text_color, Color::rgb(0x33, 0x33, 0x33));
    }

    #[test]
    fn default_theme_data_defaults() {
        let t = Theme::default();
        assert!((t.line_width - 1.5).abs() < f64::EPSILON);
        assert!((t.marker_size - 6.0).abs() < f64::EPSILON);
        assert!((t.marker_alpha - 0.8).abs() < f64::EPSILON);
    }

    #[test]
    fn default_theme_tableau_10_cycle() {
        let t = Theme::default();
        assert_eq!(t.color_cycle.len(), 10);
        assert_eq!(t.color_cycle[0], Color::TAB_BLUE);
        assert_eq!(t.color_cycle[9], Color::TAB_CYAN);
    }

    #[test]
    fn dark_theme_has_dark_background() {
        let t = Theme::dark();
        assert_eq!(t.figure_background, Color::rgb(0x1C, 0x1C, 0x1C));
        assert_eq!(t.axes_background, Color::rgb(0x1C, 0x1C, 0x1C));
    }

    #[test]
    fn dark_theme_light_text() {
        let t = Theme::dark();
        assert_eq!(t.text_color, Color::rgb(0xE0, 0xE0, 0xE0));
    }

    #[test]
    fn dark_theme_neon_cycle() {
        let t = Theme::dark();
        assert_eq!(t.color_cycle.len(), 10);
        // First color is a bright cyan.
        assert_eq!(t.color_cycle[0], Color::rgb(0x00, 0xD4, 0xFF));
    }

    #[test]
    fn seaborn_theme_tinted_face() {
        let t = Theme::seaborn();
        assert_eq!(t.axes_background, Color::rgb(0xEA, 0xEA, 0xF2));
    }

    #[test]
    fn seaborn_theme_white_grid() {
        let t = Theme::seaborn();
        assert_eq!(t.grid_color, Color::WHITE);
        assert!(t.show_grid);
    }

    #[test]
    fn ggplot_theme_grey_panel() {
        let t = Theme::ggplot();
        assert_eq!(t.axes_background, Color::rgb(0xE5, 0xE5, 0xE5));
    }

    #[test]
    fn ggplot_theme_white_grid() {
        let t = Theme::ggplot();
        assert_eq!(t.grid_color, Color::WHITE);
        assert!(t.show_grid);
    }

    #[test]
    fn ggplot_theme_no_spines() {
        let t = Theme::ggplot();
        assert!(!t.show_top_spine);
        assert!(!t.show_right_spine);
        assert!(!t.show_bottom_spine);
        assert!(!t.show_left_spine);
    }

    #[test]
    fn ggplot_theme_palette() {
        let t = Theme::ggplot();
        assert_eq!(t.color_cycle.len(), 8);
        assert_eq!(t.color_cycle[0], Color::rgb(0xF8, 0x76, 0x6D));
    }

    #[test]
    fn publication_theme_all_spines_visible() {
        let t = Theme::publication();
        assert!(t.show_top_spine);
        assert!(t.show_right_spine);
        assert!(t.show_bottom_spine);
        assert!(t.show_left_spine);
    }

    #[test]
    fn publication_theme_no_grid() {
        let t = Theme::publication();
        assert!(!t.show_grid);
    }

    #[test]
    fn publication_theme_thin_spines() {
        let t = Theme::publication();
        assert!((t.spine_width - 0.5).abs() < f64::EPSILON);
    }

    #[test]
    fn publication_theme_inward_ticks() {
        let t = Theme::publication();
        assert_eq!(t.tick_direction, TickDirection::Inward);
    }

    #[test]
    fn publication_theme_serif_font() {
        let t = Theme::publication();
        assert_eq!(t.font_family, Some("serif".to_string()));
    }

    #[test]
    fn publication_theme_white_background() {
        let t = Theme::publication();
        assert_eq!(t.figure_background, Color::WHITE);
        assert_eq!(t.axes_background, Color::WHITE);
    }
}