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
//! Line dash patterns, marker shapes, hatch fills, and spine visibility.
/// Stroke dash pattern for lines (matplotlib `linestyle`).
///
/// Patterns are scaled by stroke width at draw time so they stay readable
/// across DPI settings.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum LineStyle {
/// Solid line (`'-'`).
#[default]
Solid,
/// Dashed line (`'--'`).
Dashed,
/// Dotted line (`':'`).
Dotted,
/// Dash–dot line (`'-.'`).
DashDot,
}
impl LineStyle {
/// Pixel dash array for a given stroke width in pixels.
///
/// Returns `None` for [`LineStyle::Solid`].
pub fn dash_pattern(self, width_px: f64) -> Option<Vec<f64>> {
let w = width_px.max(0.5);
match self {
Self::Solid => None,
Self::Dashed => Some(vec![6.0 * w, 4.0 * w]),
Self::Dotted => Some(vec![1.0 * w, 2.5 * w]),
Self::DashDot => Some(vec![6.0 * w, 3.0 * w, 1.0 * w, 3.0 * w]),
}
}
}
/// Marker shape for scatter / stem / spy (matplotlib `marker`).
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum MarkerStyle {
/// Filled circle (`'o'`).
#[default]
Circle,
/// Filled square (`'s'`).
Square,
/// Filled upward triangle (`'^'`).
Triangle,
/// Filled downward triangle (`'v'`).
TriangleDown,
/// Filled diamond (`'D'`).
Diamond,
/// Plus / crosshair (`'+'`, stroked).
Plus,
/// X mark (`'x'`, stroked).
Cross,
/// Five-point star (`'*'`, filled).
Star,
/// Tiny filled point (`'.'`).
Point,
}
impl MarkerStyle {
/// `true` when the marker is drawn as a stroke (not a filled path).
pub fn is_stroke(self) -> bool {
matches!(self, Self::Plus | Self::Cross)
}
}
/// Fill hatch pattern (matplotlib `hatch`) for bars / patches.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum Hatch {
/// Solid fill only (no hatch).
#[default]
None,
/// Diagonal lines `/`.
Diagonal,
/// Diagonal lines `\`.
DiagonalBack,
/// Crossed diagonals `x`.
Cross,
/// Horizontal lines `-`.
Horizontal,
/// Vertical lines `|`.
Vertical,
/// Grid `+` (horizontal + vertical).
Grid,
/// Sparse dots `.`.
Dots,
}
impl Hatch {
/// `true` when hatch strokes should be drawn over the fill.
pub fn is_drawn(self) -> bool {
!matches!(self, Self::None)
}
}
/// Which box spines (axes borders) are drawn.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Spines {
/// Left y-spine.
pub left: bool,
/// Bottom x-spine.
pub bottom: bool,
/// Right y-spine.
pub right: bool,
/// Top x-spine.
pub top: bool,
}
impl Default for Spines {
fn default() -> Self {
Self::all()
}
}
impl Spines {
/// All four spines visible (matplotlib default).
pub fn all() -> Self {
Self {
left: true,
bottom: true,
right: true,
top: true,
}
}
/// Only left + bottom (common paper / seaborn `despine` style).
pub fn bottom_left() -> Self {
Self {
left: true,
bottom: true,
right: false,
top: false,
}
}
/// Show/hide the top spine.
pub fn top(mut self, on: bool) -> Self {
self.top = on;
self
}
/// Show/hide the right spine.
pub fn right(mut self, on: bool) -> Self {
self.right = on;
self
}
/// Show/hide the bottom spine.
pub fn bottom(mut self, on: bool) -> Self {
self.bottom = on;
self
}
/// Show/hide the left spine.
pub fn left(mut self, on: bool) -> Self {
self.left = on;
self
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn solid_has_no_dash() {
assert!(LineStyle::Solid.dash_pattern(2.0).is_none());
assert_eq!(LineStyle::Dashed.dash_pattern(2.0).unwrap().len(), 2);
assert_eq!(LineStyle::DashDot.dash_pattern(1.0).unwrap().len(), 4);
}
#[test]
fn spines_bottom_left() {
let s = Spines::bottom_left();
assert!(s.left && s.bottom && !s.top && !s.right);
}
}