plotly_fork/layout/
themes.rs

1use once_cell::sync::Lazy;
2
3use crate::{
4    common::{ColorBar, ColorScale, ColorScaleElement, Font, Label, Title},
5    layout::{Axis, ColorAxis, HoverMode, LayoutColorScale, LayoutTemplate, Template},
6};
7
8pub static DEFAULT: Lazy<Template> = Lazy::new(|| {
9    let layout_template = LayoutTemplate::new();
10    Template::new().layout(layout_template)
11});
12
13pub static PLOTLY_WHITE: Lazy<Template> = Lazy::new(|| {
14    let layout_template = LayoutTemplate::new()
15        .color_axis(ColorAxis::new().color_bar(ColorBar::new().outline_width(0)))
16        .color_scale(
17            LayoutColorScale::new()
18                .sequential(ColorScale::Vector(vec![
19                    ColorScaleElement(0., "#0d0887".to_string()),
20                    ColorScaleElement(0.1111111111111111, "#46039f".to_string()),
21                    ColorScaleElement(0.2222222222222222, "#7201a8".to_string()),
22                    ColorScaleElement(0.3333333333333333, "#9c179e".to_string()),
23                    ColorScaleElement(0.4444444444444444, "#bd3786".to_string()),
24                    ColorScaleElement(0.5555555555555556, "#d8576b".to_string()),
25                    ColorScaleElement(0.6666666666666666, "#ed7953".to_string()),
26                    ColorScaleElement(0.7777777777777778, "#fb9f3a".to_string()),
27                    ColorScaleElement(0.8888888888888888, "#fdca26".to_string()),
28                    ColorScaleElement(1., "#f0f921".to_string()),
29                ]))
30                .sequential_minus(ColorScale::Vector(vec![
31                    ColorScaleElement(0., "#0d0887".to_string()),
32                    ColorScaleElement(0.1111111111111111, "#46039f".to_string()),
33                    ColorScaleElement(0.2222222222222222, "#7201a8".to_string()),
34                    ColorScaleElement(0.3333333333333333, "#9c179e".to_string()),
35                    ColorScaleElement(0.4444444444444444, "#bd3786".to_string()),
36                    ColorScaleElement(0.5555555555555556, "#d8576b".to_string()),
37                    ColorScaleElement(0.6666666666666666, "#ed7953".to_string()),
38                    ColorScaleElement(0.7777777777777778, "#fb9f3a".to_string()),
39                    ColorScaleElement(0.8888888888888888, "#fdca26".to_string()),
40                    ColorScaleElement(1., "#f0f921".to_string()),
41                ]))
42                .diverging(ColorScale::Vector(vec![
43                    ColorScaleElement(0., "#8e0152".to_string()),
44                    ColorScaleElement(0.1, "#c51b7d".to_string()),
45                    ColorScaleElement(0.2, "#de77ae".to_string()),
46                    ColorScaleElement(0.3, "#f1b6da".to_string()),
47                    ColorScaleElement(0.4, "#fde0ef".to_string()),
48                    ColorScaleElement(0.5, "#f7f7f7".to_string()),
49                    ColorScaleElement(0.6, "#e6f5d0".to_string()),
50                    ColorScaleElement(0.7, "#b8e186".to_string()),
51                    ColorScaleElement(0.8, "#7fbc41".to_string()),
52                    ColorScaleElement(0.9, "#4d9221".to_string()),
53                    ColorScaleElement(1., "#276419".to_string()),
54                ])),
55        )
56        .colorway(vec![
57            "#636efa", "#EF553B", "#00cc96", "#ab63fa", "#FFA15A", "#19d3f3", "#FF6692", "#B6E880",
58            "#FF97FF", "#FECB52",
59        ])
60        .font(Font::new().color("#2a3f5f"))
61        .hover_label(Label::new().align("left"))
62        .hover_mode(HoverMode::Closest)
63        .paper_background_color("#ffffff")
64        .plot_background_color("#ffffff")
65        .title(Title::new("").x(0.05))
66        .x_axis(
67            Axis::new()
68                .auto_margin(true)
69                .grid_color("#EBF0F8")
70                .line_color("#EBF0F8")
71                // missing title.standoff = 15
72                .zero_line_color("#EBF0F8")
73                .zero_line_width(2),
74        )
75        .y_axis(
76            Axis::new()
77                .auto_margin(true)
78                .grid_color("#EBF0F8")
79                .line_color("#EBF0F8")
80                // missing title.standoff = 15
81                .zero_line_color("#EBF0F8")
82                .zero_line_width(2),
83        );
84    Template::new().layout(layout_template)
85});
86
87pub static PLOTLY_DARK: Lazy<Template> = Lazy::new(|| {
88    // the following are unimplemented: layout.autotypenumbers, layout.polar,
89    // layout.ternary, layout.scene, layout.geo, layout.mapbox, layout.*defaults
90    let layout_template = LayoutTemplate::new()
91        .color_axis(ColorAxis::new().color_bar(ColorBar::new().outline_width(0)))
92        .color_scale(
93            LayoutColorScale::new()
94                .sequential(ColorScale::Vector(vec![
95                    ColorScaleElement(0., "#0d0887".to_string()),
96                    ColorScaleElement(0.1111111111111111, "#46039f".to_string()),
97                    ColorScaleElement(0.2222222222222222, "#7201a8".to_string()),
98                    ColorScaleElement(0.3333333333333333, "#9c179e".to_string()),
99                    ColorScaleElement(0.4444444444444444, "#bd3786".to_string()),
100                    ColorScaleElement(0.5555555555555556, "#d8576b".to_string()),
101                    ColorScaleElement(0.6666666666666666, "#ed7953".to_string()),
102                    ColorScaleElement(0.7777777777777778, "#fb9f3a".to_string()),
103                    ColorScaleElement(0.8888888888888888, "#fdca26".to_string()),
104                    ColorScaleElement(1., "#f0f921".to_string()),
105                ]))
106                .sequential_minus(ColorScale::Vector(vec![
107                    ColorScaleElement(0., "#0d0887".to_string()),
108                    ColorScaleElement(0.1111111111111111, "#46039f".to_string()),
109                    ColorScaleElement(0.2222222222222222, "#7201a8".to_string()),
110                    ColorScaleElement(0.3333333333333333, "#9c179e".to_string()),
111                    ColorScaleElement(0.4444444444444444, "#bd3786".to_string()),
112                    ColorScaleElement(0.5555555555555556, "#d8576b".to_string()),
113                    ColorScaleElement(0.6666666666666666, "#ed7953".to_string()),
114                    ColorScaleElement(0.7777777777777778, "#fb9f3a".to_string()),
115                    ColorScaleElement(0.8888888888888888, "#fdca26".to_string()),
116                    ColorScaleElement(1., "#f0f921".to_string()),
117                ]))
118                .diverging(ColorScale::Vector(vec![
119                    ColorScaleElement(0., "#8e0152".to_string()),
120                    ColorScaleElement(0.1, "#c51b7d".to_string()),
121                    ColorScaleElement(0.2, "#de77ae".to_string()),
122                    ColorScaleElement(0.3, "#f1b6da".to_string()),
123                    ColorScaleElement(0.4, "#fde0ef".to_string()),
124                    ColorScaleElement(0.5, "#f7f7f7".to_string()),
125                    ColorScaleElement(0.6, "#e6f5d0".to_string()),
126                    ColorScaleElement(0.7, "#b8e186".to_string()),
127                    ColorScaleElement(0.8, "#7fbc41".to_string()),
128                    ColorScaleElement(0.9, "#4d9221".to_string()),
129                    ColorScaleElement(1., "#276419".to_string()),
130                ])),
131        )
132        .colorway(vec![
133            "#636efa", "#EF553B", "#00cc96", "#ab63fa", "#FFA15A", "#19d3f3", "#FF6692", "#B6E880",
134            "#FF97FF", "#FECB52",
135        ])
136        .font(Font::new().color("#f2f5fa"))
137        .hover_label(Label::new().align("left"))
138        .hover_mode(HoverMode::Closest)
139        .paper_background_color("#111111")
140        .plot_background_color("#111111")
141        .title(Title::new("").x(0.05))
142        .x_axis(
143            Axis::new()
144                .auto_margin(true)
145                .grid_color("#283442")
146                .line_color("#506784")
147                // missing title.standoff = 15
148                .zero_line_color("#283442")
149                .zero_line_width(2),
150        )
151        .y_axis(
152            Axis::new()
153                .auto_margin(true)
154                .grid_color("#283442")
155                .line_color("#506784")
156                // missing title.standoff = 15
157                .zero_line_color("#283442")
158                .zero_line_width(2),
159        );
160    Template::new().layout(layout_template)
161});
162
163#[cfg(test)]
164mod tests {
165    use super::*;
166    use crate::*;
167
168    #[test]
169    fn test_plotly_dark() {
170        let template = &*PLOTLY_DARK;
171        let layout = Layout::new().template(template);
172        let mut plot = Plot::new();
173        plot.set_layout(layout);
174        plot.add_trace(Bar::new(vec![0], vec![1]));
175
176        let expected =
177            r##"{"template":{"layout":{"title":{"text":"","x":0.05},"font":{"color":"#f2f5fa"}"##; // etc...
178        assert!(plot.to_json().contains(expected));
179    }
180}