Skip to content

Commit c3bc5d9

Browse files
Merge pull request #45 from BigThinkcode/struct_from_option
Sanitizing options
2 parents 860b812 + 02052e8 commit c3bc5d9

2 files changed

Lines changed: 34 additions & 245 deletions

File tree

lib/matplotex/figure/areal/plot_options.ex

Lines changed: 15 additions & 223 deletions
Original file line numberDiff line numberDiff line change
@@ -3,105 +3,23 @@ defmodule Matplotex.Figure.Areal.PlotOptions do
33
alias Matplotex.Figure
44
alias Matplotex.Figure.TwoD
55
alias Matplotex.Figure.RcParams
6-
alias Matplotex.Figure.Font
76

8-
@type t :: %__MODULE__{
9-
figsize: {integer(), integer()},
10-
figrows: integer(),
11-
figcolumns: integer(),
12-
margin: float(),
13-
title: String.t() | nil,
14-
x_label: String.t() | nil,
15-
y_label: String.t() | nil,
16-
x_limit: :default | {float(), float()},
17-
y_limit: :default | {float(), float()},
18-
title_font_size: integer(),
19-
title_font_color: String.t(),
20-
x_label_font_size: integer(),
21-
y_label_font_size: integer(),
22-
x_label_font_color: String.t(),
23-
y_label_font_color: String.t(),
24-
show_x_axis: boolean(),
25-
show_y_axis: boolean(),
26-
x_scale: :default | float(),
27-
y_scale: :default | float(),
28-
show_x_ticks: boolean(),
29-
show_y_ticks: boolean(),
30-
show_x_ticks_label: boolean(),
31-
show_y_ticks_label: boolean(),
32-
x_tick_font_size: integer(),
33-
y_tick_font_size: integer(),
34-
x_tick_label_rotation: integer(),
35-
y_tick_label_rotation: integer(),
36-
x_tick_font_color: String.t(),
37-
y_tick_font_color: String.t(),
38-
show_v_grid: boolean(),
39-
show_h_grid: boolean(),
40-
legend_font_size: integer(),
41-
legend_font_color: String.t(),
42-
plot_line_style: String.t(),
43-
plot_line_width: float(),
44-
grid_linestyle: String.t(),
45-
grid_linewidth: float(),
46-
grid_color: String.t(),
47-
x_padding: float(),
48-
y_padding: float()
49-
}
50-
51-
defstruct figsize: {10, 6},
52-
figrows: 1,
53-
figcolumns: 1,
54-
margin: 0.05,
55-
title: nil,
56-
x_label: nil,
57-
y_label: nil,
58-
x_limit: :default,
59-
y_limit: :default,
60-
title_font_size: 14,
61-
title_font_color: "black",
62-
x_label_font_size: 12,
63-
y_label_font_size: 12,
64-
x_label_font_color: "black",
65-
y_label_font_color: "black",
66-
show_x_axis: true,
67-
show_y_axis: true,
68-
x_scale: :default,
69-
y_scale: :default,
70-
show_x_ticks: true,
71-
show_y_ticks: true,
72-
show_x_ticks_label: true,
73-
show_y_ticks_label: true,
74-
x_tick_font_size: 10,
75-
y_tick_font_size: 10,
76-
x_tick_label_rotation: 0,
77-
y_tick_label_rotation: 0,
78-
x_tick_font_color: "black",
79-
y_tick_font_color: "black",
80-
show_v_grid: true,
81-
show_h_grid: true,
82-
legend_font_size: 9,
83-
legend_font_color: "black",
84-
plot_line_style: "_",
85-
plot_line_width: 1.0,
86-
grid_linestyle: "--",
87-
grid_linewidth: 1.0,
88-
grid_color: "#ddd",
89-
x_padding: 0.05,
90-
y_padding: 0.05
91-
92-
@spec cast_options(map()) :: __MODULE__.t()
93-
def cast_options(opts), do: struct(__MODULE__, opts)
94-
95-
@spec set_options_in_figure(Figure.t(), __MODULE__.t()) :: Figure.t()
96-
def set_options_in_figure(%Figure{} = figure, %__MODULE__{} = options) do
97-
figure
98-
|> set_options_in_figure_struct(options)
99-
|> set_options_in_axes_struct(options)
100-
|> set_options_in_rc_params_struct(options)
101-
end
7+
@immutable_keys [
8+
:axes,
9+
:rc_params,
10+
:elements,
11+
:region_x,
12+
:region_y,
13+
:region_title,
14+
:region_content,
15+
:region_legend,
16+
:region_color_bar
17+
]
10218

10319
@spec set_options_in_figure(Figure.t(), keyword()) :: Figure.t()
10420
def set_options_in_figure(%Figure{} = figure, opts) do
21+
opts = sanitize(opts)
22+
10523
figure
10624
|> cast_figure(opts)
10725
|> cast_axes(opts)
@@ -137,133 +55,7 @@ defmodule Matplotex.Figure.Areal.PlotOptions do
13755
%Figure{figure | rc_params: rc_params |> RcParams.update_with_font(opts) |> struct(opts)}
13856
end
13957

140-
defp set_options_in_figure_struct(%Figure{} = figure, %__MODULE__{
141-
figsize: figsize,
142-
figrows: figrows,
143-
figcolumns: figcolumns,
144-
margin: margin
145-
}) do
146-
%Figure{figure | figsize: figsize, rows: figrows, columns: figcolumns, margin: margin}
147-
end
148-
149-
defp set_options_in_axes_struct(
150-
%Figure{axes: axes} = figure,
151-
%__MODULE__{
152-
title: title,
153-
x_label: x_label,
154-
y_label: y_label,
155-
x_limit: x_limit,
156-
y_limit: y_limit
157-
}
158-
) do
159-
%Figure{
160-
figure
161-
| axes: %{
162-
axes
163-
| title: title,
164-
label: %TwoD{x: x_label, y: y_label},
165-
limit: %TwoD{x: x_limit, y: y_limit}
166-
}
167-
}
168-
end
169-
170-
defp set_options_in_rc_params_struct(
171-
%Figure{
172-
axes: axes,
173-
rc_params:
174-
%RcParams{
175-
title_font: title_font,
176-
x_label_font: x_label_font,
177-
y_label_font: y_label_font,
178-
x_tick_font: x_tick_font,
179-
y_tick_font: y_tick_font,
180-
legend_font: legend_font
181-
} = rc_params
182-
} = figure,
183-
%__MODULE__{
184-
title_font_size: title_font_size,
185-
title_font_color: title_font_color,
186-
x_label_font_size: x_label_font_size,
187-
y_label_font_size: y_label_font_size,
188-
x_label_font_color: x_label_font_color,
189-
y_label_font_color: y_label_font_color,
190-
show_x_axis: show_x_axis,
191-
show_y_axis: show_x_axis,
192-
x_scale: x_scale,
193-
y_scale: y_scale,
194-
show_x_ticks: show_x_ticks,
195-
show_y_ticks: show_y_ticks,
196-
show_x_ticks_label: _show_x_ticks_label,
197-
show_y_ticks_label: _show_y_ticks_label,
198-
x_tick_font_size: x_tick_font_size,
199-
y_tick_font_size: y_tick_font_size,
200-
x_tick_label_rotation: _x_tick_label_rotation,
201-
y_tick_label_rotation: _y_tick_label_rotation,
202-
x_tick_font_color: x_tick_font_color,
203-
y_tick_font_color: y_tick_font_color,
204-
show_v_grid: show_v_grid,
205-
show_h_grid: show_h_grid,
206-
legend_font_size: legend_font_size,
207-
legend_font_color: legend_font_color,
208-
plot_line_style: _plot_line_style,
209-
plot_line_width: _plot_line_width,
210-
grid_color: grid_color,
211-
grid_linestyle: grid_linestyle,
212-
grid_linewidth: grid_linewidth,
213-
x_padding: x_padding,
214-
y_padding: y_padding
215-
}
216-
) do
217-
%Figure{
218-
figure
219-
| rc_params: %RcParams{
220-
rc_params
221-
| title_font: %Font{
222-
title_font
223-
| font_size: title_font_size,
224-
fill: title_font_color
225-
},
226-
x_label_font: %Font{
227-
x_label_font
228-
| font_size: x_label_font_size,
229-
fill: x_label_font_color
230-
},
231-
y_label_font: %Font{
232-
y_label_font
233-
| font_size: y_label_font_size,
234-
fill: y_label_font_color
235-
},
236-
x_tick_font: %Font{
237-
x_tick_font
238-
| font_size: x_tick_font_size,
239-
fill: x_tick_font_color
240-
},
241-
y_tick_font: %Font{
242-
y_tick_font
243-
| font_size: y_tick_font_size,
244-
fill: y_tick_font_color
245-
},
246-
legend_font: %Font{
247-
legend_font
248-
| font_size: legend_font_size,
249-
fill: legend_font_color
250-
},
251-
grid_color: grid_color,
252-
grid_linestyle: grid_linestyle,
253-
grid_linewidth: grid_linewidth,
254-
x_padding: x_padding,
255-
y_padding: y_padding
256-
},
257-
axes: %{
258-
axes
259-
| show_x_axis: show_x_axis,
260-
show_y_axis: show_x_axis,
261-
show_x_ticks: show_x_ticks,
262-
show_y_ticks: show_y_ticks,
263-
show_v_grid: show_v_grid,
264-
show_h_grid: show_h_grid,
265-
scale: %TwoD{x: x_scale, y: y_scale}
266-
}
267-
}
58+
defp sanitize(opts) do
59+
Keyword.drop(opts, @immutable_keys)
26860
end
26961
end
Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,31 @@
11
defmodule Matplotex.Figure.Areal.PlotOptionsTest do
2-
alias Matplotex.Figure
3-
alias Matplotex.Figure.Areal.PlotOptions
42
use Matplotex.PlotCase
53

64
setup do
75
{:ok, %{figure: Matplotex.FrameHelpers.sample_figure()}}
86
end
97

10-
describe "cast_options/1" do
11-
test "generate PlotOptions struct", %{} do
12-
assert %PlotOptions{} =
13-
PlotOptions.cast_options(
14-
margin: 10,
15-
x_limit: {0, 10},
16-
y_limit: {-10, 10}
17-
)
18-
end
19-
end
20-
21-
describe "set_options_in_figure/2" do
22-
test "update figure struct with plotting options", %{figure: figure} do
23-
options =
24-
PlotOptions.cast_options(
25-
margin: 10,
26-
x_limit: {0, 10},
27-
y_limit: {-10, 10}
8+
describe "set_options_in_figure" do
9+
test "plot options should't allow some restricted keys", %{figure: figure} do
10+
figure =
11+
figure
12+
|> Matplotex.set_title("New title")
13+
|> Matplotex.plot([1, 2, 3, 4, 5], [1, 2, 3, 4, 5],
14+
x_lagel: "X",
15+
axes: nil,
16+
region_x: nil,
17+
region_y: nil,
18+
region_content: nil,
19+
region_title: nil,
20+
elements: nil
2821
)
2922

30-
assert %Figure{margin: 10, axes: %{limit: %{x: {0, 10}, y: {-10, 10}}}} =
31-
PlotOptions.set_options_in_figure(figure, options)
23+
refute figure.axes.region_x == nil
24+
refute figure.axes.region_y == nil
25+
refute figure.axes.region_content == nil
26+
refute figure.axes.region_title == nil
27+
refute figure.axes.element == nil
28+
refute figure.axes == nil
3229
end
3330
end
3431
end

0 commit comments

Comments
 (0)