Skip to content

Commit 44325c6

Browse files
committed
extract grid types out of mod.rs
Signed-off-by: Andrei Gherghescu <[email protected]>
1 parent 171277b commit 44325c6

File tree

3 files changed

+264
-272
lines changed

3 files changed

+264
-272
lines changed

plotly/src/layout/axis.rs

Lines changed: 92 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ use serde::Serialize;
33

44
use crate::color::Color;
55
use crate::common::{
6-
Anchor, AxisSide, Calendar, DashType, ExponentFormat, Font, TickFormatStop, TickMode, Title,
6+
Anchor, AxisSide, Calendar, ColorBar, ColorScale, DashType, ExponentFormat, Font,
7+
TickFormatStop, TickMode, Title,
78
};
89
use crate::private::NumOrStringCollection;
910

@@ -65,6 +66,31 @@ pub enum AxisConstrain {
6566
Domain,
6667
}
6768

69+
#[serde_with::skip_serializing_none]
70+
#[derive(Serialize, Debug, Clone, FieldSetter)]
71+
pub struct ColorAxis {
72+
cauto: Option<bool>,
73+
cmin: Option<f64>,
74+
cmax: Option<f64>,
75+
cmid: Option<f64>,
76+
#[serde(rename = "colorscale")]
77+
color_scale: Option<ColorScale>,
78+
#[serde(rename = "autocolorscale")]
79+
auto_color_scale: Option<bool>,
80+
#[serde(rename = "reversescale")]
81+
reverse_scale: Option<bool>,
82+
#[serde(rename = "showscale")]
83+
show_scale: Option<bool>,
84+
#[serde(rename = "colorbar")]
85+
color_bar: Option<ColorBar>,
86+
}
87+
88+
impl ColorAxis {
89+
pub fn new() -> Self {
90+
Default::default()
91+
}
92+
}
93+
6894
#[derive(Serialize, Debug, Clone)]
6995
#[serde(rename_all = "lowercase")]
7096
pub enum RangeMode {
@@ -419,6 +445,18 @@ mod tests {
419445
use serde_json::{json, to_value};
420446

421447
use super::*;
448+
use crate::common::ColorScalePalette;
449+
450+
#[test]
451+
#[rustfmt::skip]
452+
fn serialize_axis_type() {
453+
assert_eq!(to_value(AxisType::Default).unwrap(), json!("-"));
454+
assert_eq!(to_value(AxisType::Linear).unwrap(), json!("linear"));
455+
assert_eq!(to_value(AxisType::Log).unwrap(), json!("log"));
456+
assert_eq!(to_value(AxisType::Date).unwrap(), json!("date"));
457+
assert_eq!(to_value(AxisType::Category).unwrap(), json!("category"));
458+
assert_eq!(to_value(AxisType::MultiCategory).unwrap(), json!("multicategory"));
459+
}
422460

423461
#[test]
424462
#[rustfmt::skip]
@@ -441,6 +479,59 @@ mod tests {
441479
assert_eq!(to_value(TicksPosition::Boundaries).unwrap(), json!("boundaries"));
442480
}
443481

482+
#[test]
483+
fn serialize_axis_constrain() {
484+
assert_eq!(to_value(AxisConstrain::Range).unwrap(), json!("range"));
485+
assert_eq!(to_value(AxisConstrain::Domain).unwrap(), json!("domain"));
486+
}
487+
488+
#[test]
489+
fn serialize_array_show() {
490+
assert_eq!(to_value(ArrayShow::All).unwrap(), json!("all"));
491+
assert_eq!(to_value(ArrayShow::First).unwrap(), json!("first"));
492+
assert_eq!(to_value(ArrayShow::Last).unwrap(), json!("last"));
493+
assert_eq!(to_value(ArrayShow::None).unwrap(), json!("none"));
494+
}
495+
496+
#[test]
497+
fn serialize_color_axis() {
498+
let color_axis = ColorAxis::new()
499+
.auto_color_scale(false)
500+
.cauto(true)
501+
.cmax(1.0)
502+
.cmid(0.5)
503+
.cmin(0.0)
504+
.color_bar(ColorBar::new())
505+
.color_scale(ColorScale::Palette(ColorScalePalette::Greens))
506+
.reverse_scale(false)
507+
.show_scale(true);
508+
509+
let expected = json!({
510+
"autocolorscale": false,
511+
"cauto": true,
512+
"cmin": 0.0,
513+
"cmid": 0.5,
514+
"cmax": 1.0,
515+
"colorbar": {},
516+
"colorscale": "Greens",
517+
"reversescale": false,
518+
"showscale": true,
519+
});
520+
521+
assert_eq!(to_value(color_axis).unwrap(), expected);
522+
}
523+
524+
#[test]
525+
#[rustfmt::skip]
526+
fn serialize_constrain_direction() {
527+
assert_eq!(to_value(ConstrainDirection::Left).unwrap(), json!("left"));
528+
assert_eq!(to_value(ConstrainDirection::Center).unwrap(), json!("center"));
529+
assert_eq!(to_value(ConstrainDirection::Right).unwrap(), json!("right"));
530+
assert_eq!(to_value(ConstrainDirection::Top).unwrap(), json!("top"));
531+
assert_eq!(to_value(ConstrainDirection::Middle).unwrap(), json!("middle"));
532+
assert_eq!(to_value(ConstrainDirection::Bottom).unwrap(), json!("bottom"));
533+
}
534+
444535
#[test]
445536
#[rustfmt::skip]
446537
fn serialize_category_order() {

plotly/src/layout/grid.rs

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
use plotly_derive::FieldSetter;
2+
use serde::Serialize;
3+
4+
#[derive(Serialize, Debug, Clone)]
5+
pub enum RowOrder {
6+
#[serde(rename = "top to bottom")]
7+
TopToBottom,
8+
#[serde(rename = "bottom to top")]
9+
BottomToTop,
10+
}
11+
12+
#[derive(Serialize, Debug, Clone)]
13+
#[serde(rename_all = "lowercase")]
14+
pub enum GridPattern {
15+
Independent,
16+
Coupled,
17+
}
18+
19+
#[derive(Serialize, Debug, Clone)]
20+
#[serde(rename_all = "lowercase")]
21+
pub enum GridXSide {
22+
Bottom,
23+
#[serde(rename = "bottom plot")]
24+
BottomPlot,
25+
#[serde(rename = "top plot")]
26+
TopPlot,
27+
Top,
28+
}
29+
30+
#[derive(Serialize, Debug, Clone)]
31+
#[serde(rename_all = "lowercase")]
32+
pub enum GridYSide {
33+
Left,
34+
#[serde(rename = "left plot")]
35+
LeftPlot,
36+
#[serde(rename = "right plot")]
37+
RightPlot,
38+
Right,
39+
}
40+
41+
#[serde_with::skip_serializing_none]
42+
#[derive(Serialize, Debug, Clone, FieldSetter)]
43+
pub struct GridDomain {
44+
x: Option<Vec<f64>>,
45+
y: Option<Vec<f64>>,
46+
}
47+
48+
impl GridDomain {
49+
pub fn new() -> Self {
50+
Default::default()
51+
}
52+
}
53+
#[serde_with::skip_serializing_none]
54+
#[derive(Serialize, Debug, Clone, FieldSetter)]
55+
pub struct LayoutGrid {
56+
rows: Option<usize>,
57+
#[serde(rename = "roworder")]
58+
row_order: Option<RowOrder>,
59+
columns: Option<usize>,
60+
#[serde(rename = "subplots")]
61+
sub_plots: Option<Vec<String>>,
62+
#[serde(rename = "xaxes")]
63+
x_axes: Option<Vec<String>>,
64+
#[serde(rename = "yaxes")]
65+
y_axes: Option<Vec<String>>,
66+
pattern: Option<GridPattern>,
67+
#[serde(rename = "xgap")]
68+
x_gap: Option<f64>,
69+
#[serde(rename = "ygap")]
70+
y_gap: Option<f64>,
71+
domain: Option<GridDomain>,
72+
#[serde(rename = "xside")]
73+
x_side: Option<GridXSide>,
74+
#[serde(rename = "yside")]
75+
y_side: Option<GridYSide>,
76+
}
77+
78+
impl LayoutGrid {
79+
pub fn new() -> Self {
80+
Default::default()
81+
}
82+
}
83+
84+
#[cfg(test)]
85+
mod tests {
86+
use serde_json::{json, to_value};
87+
88+
use super::*;
89+
90+
#[test]
91+
#[rustfmt::skip]
92+
fn serialize_row_order() {
93+
assert_eq!(to_value(RowOrder::TopToBottom).unwrap(), json!("top to bottom"));
94+
assert_eq!(to_value(RowOrder::BottomToTop).unwrap(), json!("bottom to top"));
95+
}
96+
97+
#[test]
98+
#[rustfmt::skip]
99+
fn serialize_grid_pattern() {
100+
assert_eq!(to_value(GridPattern::Independent).unwrap(), json!("independent"));
101+
assert_eq!(to_value(GridPattern::Coupled).unwrap(), json!("coupled"));
102+
}
103+
104+
#[test]
105+
#[rustfmt::skip]
106+
fn serialize_grid_x_side() {
107+
assert_eq!(to_value(GridXSide::Bottom).unwrap(), json!("bottom"));
108+
assert_eq!(to_value(GridXSide::BottomPlot).unwrap(), json!("bottom plot"));
109+
assert_eq!(to_value(GridXSide::Top).unwrap(), json!("top"));
110+
assert_eq!(to_value(GridXSide::TopPlot).unwrap(), json!("top plot"));
111+
}
112+
113+
#[test]
114+
#[rustfmt::skip]
115+
fn serialize_grid_y_side() {
116+
assert_eq!(to_value(GridYSide::Left).unwrap(), json!("left"));
117+
assert_eq!(to_value(GridYSide::LeftPlot).unwrap(), json!("left plot"));
118+
assert_eq!(to_value(GridYSide::Right).unwrap(), json!("right"));
119+
assert_eq!(to_value(GridYSide::RightPlot).unwrap(), json!("right plot"));
120+
}
121+
122+
#[test]
123+
fn serialize_grid_domain() {
124+
let grid_domain = GridDomain::new().x(vec![0.0]).y(vec![1.0]);
125+
let expected = json!({
126+
"x": [0.0],
127+
"y": [1.0]
128+
});
129+
130+
assert_eq!(to_value(grid_domain).unwrap(), expected);
131+
}
132+
133+
#[test]
134+
fn serialize_layout_grid() {
135+
let layout_grid = LayoutGrid::new()
136+
.rows(224)
137+
.row_order(RowOrder::BottomToTop)
138+
.columns(501)
139+
.sub_plots(vec!["subplots".to_string()])
140+
.x_axes(vec!["xaxes".to_string()])
141+
.y_axes(vec!["yaxes".to_string()])
142+
.pattern(GridPattern::Coupled)
143+
.x_gap(2.2)
144+
.y_gap(4.4)
145+
.domain(GridDomain::new())
146+
.x_side(GridXSide::Top)
147+
.y_side(GridYSide::Right);
148+
149+
let expected = json!({
150+
"rows": 224,
151+
"roworder": "bottom to top",
152+
"columns": 501,
153+
"subplots": ["subplots"],
154+
"xaxes": ["xaxes"],
155+
"yaxes": ["yaxes"],
156+
"pattern": "coupled",
157+
"xgap": 2.2,
158+
"ygap": 4.4,
159+
"domain": {},
160+
"xside": "top",
161+
"yside": "right",
162+
});
163+
164+
assert_eq!(to_value(layout_grid).unwrap(), expected);
165+
}
166+
}

0 commit comments

Comments
 (0)