Skip to content

Commit c8e2029

Browse files
committed
refactor: Move set_axis and set_legend to Axis and Legend modules
1 parent 13dbc45 commit c8e2029

File tree

3 files changed

+176
-177
lines changed

3 files changed

+176
-177
lines changed

src/common/layout.rs

Lines changed: 4 additions & 174 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
use plotly::{
2-
common::Font,
3-
layout::{Axis as AxisPlotly, Legend as LegendPlotly},
4-
Layout as LayoutPlotly,
5-
};
1+
use plotly::Layout as LayoutPlotly;
62

73
use crate::components::{Axis, Legend, Text};
84

@@ -23,175 +19,9 @@ pub(crate) trait Layout {
2319
layout = layout.title(title.to_plotly());
2420
}
2521

26-
layout = layout.x_axis(Self::set_axis(x_title, x_axis));
27-
layout = layout.y_axis(Self::set_axis(y_title, y_axis));
28-
layout = layout.legend(Self::set_legend(legend_title, legend));
22+
layout = layout.x_axis(Axis::set_axis(x_title, x_axis));
23+
layout = layout.y_axis(Axis::set_axis(y_title, y_axis));
24+
layout = layout.legend(Legend::set_legend(legend_title, legend));
2925
layout
3026
}
31-
32-
// TODO: Move axis functions to Axis struct like colorbar
33-
fn set_axis(title: Option<Text>, format: Option<&Axis>) -> AxisPlotly {
34-
let mut axis = AxisPlotly::new();
35-
36-
if let Some(title) = title {
37-
axis = axis.title(title.to_plotly());
38-
}
39-
40-
if let Some(format) = format {
41-
axis = Self::set_axis_format(axis, format);
42-
}
43-
44-
axis
45-
}
46-
47-
// TODO: Move legend functions to Axis struct like colorbar
48-
fn set_legend(title: Option<Text>, format: Option<&Legend>) -> LegendPlotly {
49-
let mut legend = LegendPlotly::new();
50-
51-
if let Some(title) = title {
52-
legend = legend.title(title.to_plotly());
53-
}
54-
55-
if let Some(format) = format {
56-
legend = Self::set_legend_format(legend, format);
57-
}
58-
59-
legend
60-
}
61-
62-
fn set_axis_format(mut axis: AxisPlotly, format: &Axis) -> AxisPlotly {
63-
if let Some(visible) = format.show_axis {
64-
axis = axis.visible(visible.to_owned());
65-
}
66-
67-
if let Some(axis_position) = &format.axis_side {
68-
axis = axis.side(axis_position.to_plotly());
69-
}
70-
71-
if let Some(axis_type) = &format.axis_type {
72-
axis = axis.type_(axis_type.to_plotly());
73-
}
74-
75-
if let Some(color) = format.value_color {
76-
axis = axis.color(color.to_plotly());
77-
}
78-
79-
if let Some(range) = &format.value_range {
80-
axis = axis.range(range.to_owned());
81-
}
82-
83-
if let Some(thousands) = format.value_thousands {
84-
axis = axis.separate_thousands(thousands.to_owned());
85-
}
86-
87-
if let Some(exponent) = &format.value_exponent {
88-
axis = axis.exponent_format(exponent.to_plotly());
89-
}
90-
91-
if let Some(range_values) = &format.tick_values {
92-
axis = axis.tick_values(range_values.to_owned());
93-
}
94-
95-
if let Some(tick_text) = &format.tick_labels {
96-
axis = axis.tick_text(tick_text.to_owned());
97-
}
98-
99-
if let Some(tick_direction) = &format.tick_direction {
100-
axis = axis.ticks(tick_direction.to_plotly_tickdirection());
101-
}
102-
103-
if let Some(tick_length) = format.tick_length {
104-
axis = axis.tick_length(tick_length.to_owned());
105-
}
106-
107-
if let Some(tick_width) = format.tick_width {
108-
axis = axis.tick_width(tick_width.to_owned());
109-
}
110-
111-
if let Some(color) = format.tick_color {
112-
axis = axis.tick_color(color.to_plotly());
113-
}
114-
115-
if let Some(tick_angle) = format.tick_angle {
116-
axis = axis.tick_angle(tick_angle.to_owned());
117-
}
118-
119-
if let Some(font) = &format.tick_font {
120-
axis = axis.tick_font(Font::new().family(font.as_str()));
121-
}
122-
123-
if let Some(show_line) = format.show_line {
124-
axis = axis.show_line(show_line.to_owned());
125-
}
126-
127-
if let Some(color) = format.line_color {
128-
axis = axis.line_color(color.to_plotly());
129-
}
130-
131-
if let Some(line_width) = format.line_width {
132-
axis = axis.line_width(line_width.to_owned());
133-
}
134-
135-
if let Some(show_grid) = format.show_grid {
136-
axis = axis.show_grid(show_grid.to_owned());
137-
}
138-
139-
if let Some(color) = format.grid_color {
140-
axis = axis.grid_color(color.to_plotly());
141-
}
142-
143-
if let Some(grid_width) = format.grid_width {
144-
axis = axis.grid_width(grid_width.to_owned());
145-
}
146-
147-
if let Some(show_zero_line) = format.show_zero_line {
148-
axis = axis.zero_line(show_zero_line.to_owned());
149-
}
150-
151-
if let Some(color) = format.zero_line_color {
152-
axis = axis.zero_line_color(color.to_plotly());
153-
}
154-
155-
if let Some(zero_line_width) = format.zero_line_width {
156-
axis = axis.zero_line_width(zero_line_width.to_owned());
157-
}
158-
159-
if let Some(axis_position) = format.axis_position {
160-
axis = axis.position(axis_position.to_owned());
161-
}
162-
163-
axis
164-
}
165-
166-
fn set_legend_format(mut legend: LegendPlotly, format: &Legend) -> LegendPlotly {
167-
if let Some(color) = format.background_color {
168-
legend = legend.background_color(color.to_plotly());
169-
}
170-
171-
if let Some(color) = format.border_color {
172-
legend = legend.border_color(color.to_plotly());
173-
}
174-
175-
if let Some(width) = format.border_width {
176-
legend = legend.border_width(width);
177-
}
178-
179-
if let Some(font) = &format.font {
180-
legend = legend.font(Font::new().family(font.as_str()));
181-
}
182-
183-
if let Some(orientation) = &format.orientation {
184-
legend = legend.orientation(orientation.to_plotly());
185-
}
186-
187-
if let Some(x) = format.x {
188-
legend = legend.x(x);
189-
}
190-
191-
if let Some(y) = format.y {
192-
legend = legend.y(y);
193-
}
194-
195-
legend
196-
}
19727
}

src/components/axis.rs

Lines changed: 123 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
use plotly::{common::AxisSide as AxisSidePlotly, layout::AxisType as AxisTypePlotly};
1+
use plotly::{
2+
common::{AxisSide as AxisSidePlotly, Font},
3+
layout::{Axis as AxisPlotly, AxisType as AxisTypePlotly},
4+
};
25

3-
use crate::components::{Rgb, TickDirection, ValueExponent};
6+
use crate::components::{Rgb, Text, TickDirection, ValueExponent};
47

58
/// A structure representing a customizable axis.
69
///
@@ -339,6 +342,124 @@ impl Axis {
339342
self.zero_line_width = Some(width);
340343
self
341344
}
345+
346+
pub(crate) fn set_axis(title: Option<Text>, format: Option<&Axis>) -> AxisPlotly {
347+
let mut axis = AxisPlotly::new();
348+
349+
if let Some(title) = title {
350+
axis = axis.title(title.to_plotly());
351+
}
352+
353+
if let Some(format) = format {
354+
axis = Self::set_format(axis, format);
355+
}
356+
357+
axis
358+
}
359+
360+
fn set_format(mut axis: AxisPlotly, format: &Axis) -> AxisPlotly {
361+
if let Some(visible) = format.show_axis {
362+
axis = axis.visible(visible.to_owned());
363+
}
364+
365+
if let Some(axis_position) = &format.axis_side {
366+
axis = axis.side(axis_position.to_plotly());
367+
}
368+
369+
if let Some(axis_type) = &format.axis_type {
370+
axis = axis.type_(axis_type.to_plotly());
371+
}
372+
373+
if let Some(color) = format.value_color {
374+
axis = axis.color(color.to_plotly());
375+
}
376+
377+
if let Some(range) = &format.value_range {
378+
axis = axis.range(range.to_owned());
379+
}
380+
381+
if let Some(thousands) = format.value_thousands {
382+
axis = axis.separate_thousands(thousands.to_owned());
383+
}
384+
385+
if let Some(exponent) = &format.value_exponent {
386+
axis = axis.exponent_format(exponent.to_plotly());
387+
}
388+
389+
if let Some(range_values) = &format.tick_values {
390+
axis = axis.tick_values(range_values.to_owned());
391+
}
392+
393+
if let Some(tick_text) = &format.tick_labels {
394+
axis = axis.tick_text(tick_text.to_owned());
395+
}
396+
397+
if let Some(tick_direction) = &format.tick_direction {
398+
axis = axis.ticks(tick_direction.to_plotly_tickdirection());
399+
}
400+
401+
if let Some(tick_length) = format.tick_length {
402+
axis = axis.tick_length(tick_length.to_owned());
403+
}
404+
405+
if let Some(tick_width) = format.tick_width {
406+
axis = axis.tick_width(tick_width.to_owned());
407+
}
408+
409+
if let Some(color) = format.tick_color {
410+
axis = axis.tick_color(color.to_plotly());
411+
}
412+
413+
if let Some(tick_angle) = format.tick_angle {
414+
axis = axis.tick_angle(tick_angle.to_owned());
415+
}
416+
417+
if let Some(font) = &format.tick_font {
418+
axis = axis.tick_font(Font::new().family(font.as_str()));
419+
}
420+
421+
if let Some(show_line) = format.show_line {
422+
axis = axis.show_line(show_line.to_owned());
423+
}
424+
425+
if let Some(color) = format.line_color {
426+
axis = axis.line_color(color.to_plotly());
427+
}
428+
429+
if let Some(line_width) = format.line_width {
430+
axis = axis.line_width(line_width.to_owned());
431+
}
432+
433+
if let Some(show_grid) = format.show_grid {
434+
axis = axis.show_grid(show_grid.to_owned());
435+
}
436+
437+
if let Some(color) = format.grid_color {
438+
axis = axis.grid_color(color.to_plotly());
439+
}
440+
441+
if let Some(grid_width) = format.grid_width {
442+
axis = axis.grid_width(grid_width.to_owned());
443+
}
444+
445+
if let Some(show_zero_line) = format.show_zero_line {
446+
axis = axis.zero_line(show_zero_line.to_owned());
447+
}
448+
449+
if let Some(color) = format.zero_line_color {
450+
axis = axis.zero_line_color(color.to_plotly());
451+
}
452+
453+
if let Some(zero_line_width) = format.zero_line_width {
454+
axis = axis.zero_line_width(zero_line_width.to_owned());
455+
}
456+
457+
if let Some(axis_position) = format.axis_position {
458+
axis = axis.position(axis_position.to_owned());
459+
}
460+
461+
axis
462+
}
342463
}
343464

344465
/// Enumeration representing the position of the axis.

src/components/legend.rs

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
use crate::{Orientation, Rgb};
1+
use plotly::{common::Font, layout::Legend as LegendPlotly};
2+
3+
use crate::{Orientation, Rgb, Text};
24

35
/// A structure representing a customizable plot legend.
46
///
@@ -130,4 +132,50 @@ impl Legend {
130132
self.y = Some(y);
131133
self
132134
}
135+
136+
pub(crate) fn set_legend(title: Option<Text>, format: Option<&Legend>) -> LegendPlotly {
137+
let mut legend = LegendPlotly::new();
138+
139+
if let Some(title) = title {
140+
legend = legend.title(title.to_plotly());
141+
}
142+
143+
if let Some(format) = format {
144+
legend = Self::set_format(legend, format);
145+
}
146+
147+
legend
148+
}
149+
150+
fn set_format(mut legend: LegendPlotly, format: &Legend) -> LegendPlotly {
151+
if let Some(color) = format.background_color {
152+
legend = legend.background_color(color.to_plotly());
153+
}
154+
155+
if let Some(color) = format.border_color {
156+
legend = legend.border_color(color.to_plotly());
157+
}
158+
159+
if let Some(width) = format.border_width {
160+
legend = legend.border_width(width);
161+
}
162+
163+
if let Some(font) = &format.font {
164+
legend = legend.font(Font::new().family(font.as_str()));
165+
}
166+
167+
if let Some(orientation) = &format.orientation {
168+
legend = legend.orientation(orientation.to_plotly());
169+
}
170+
171+
if let Some(x) = format.x {
172+
legend = legend.x(x);
173+
}
174+
175+
if let Some(y) = format.y {
176+
legend = legend.y(y);
177+
}
178+
179+
legend
180+
}
133181
}

0 commit comments

Comments
 (0)