From 09d6801fbbdf0e21ca18d4c2984e8af6d1bf42e3 Mon Sep 17 00:00:00 2001 From: Mohammed Sadique Date: Mon, 27 Jan 2025 21:47:53 +0530 Subject: [PATCH 1/2] sanitize options --- lib/matplotex/figure/areal/plot_options.ex | 226 +----------------- .../figure/areal/plot_options_test.exs | 36 +-- 2 files changed, 17 insertions(+), 245 deletions(-) diff --git a/lib/matplotex/figure/areal/plot_options.ex b/lib/matplotex/figure/areal/plot_options.ex index c50fdcd..0c1680e 100644 --- a/lib/matplotex/figure/areal/plot_options.ex +++ b/lib/matplotex/figure/areal/plot_options.ex @@ -3,105 +3,12 @@ defmodule Matplotex.Figure.Areal.PlotOptions do alias Matplotex.Figure alias Matplotex.Figure.TwoD alias Matplotex.Figure.RcParams - alias Matplotex.Figure.Font - @type t :: %__MODULE__{ - figsize: {integer(), integer()}, - figrows: integer(), - figcolumns: integer(), - margin: float(), - title: String.t() | nil, - x_label: String.t() | nil, - y_label: String.t() | nil, - x_limit: :default | {float(), float()}, - y_limit: :default | {float(), float()}, - title_font_size: integer(), - title_font_color: String.t(), - x_label_font_size: integer(), - y_label_font_size: integer(), - x_label_font_color: String.t(), - y_label_font_color: String.t(), - show_x_axis: boolean(), - show_y_axis: boolean(), - x_scale: :default | float(), - y_scale: :default | float(), - show_x_ticks: boolean(), - show_y_ticks: boolean(), - show_x_ticks_label: boolean(), - show_y_ticks_label: boolean(), - x_tick_font_size: integer(), - y_tick_font_size: integer(), - x_tick_label_rotation: integer(), - y_tick_label_rotation: integer(), - x_tick_font_color: String.t(), - y_tick_font_color: String.t(), - show_v_grid: boolean(), - show_h_grid: boolean(), - legend_font_size: integer(), - legend_font_color: String.t(), - plot_line_style: String.t(), - plot_line_width: float(), - grid_linestyle: String.t(), - grid_linewidth: float(), - grid_color: String.t(), - x_padding: float(), - y_padding: float() - } - - defstruct figsize: {10, 6}, - figrows: 1, - figcolumns: 1, - margin: 0.05, - title: nil, - x_label: nil, - y_label: nil, - x_limit: :default, - y_limit: :default, - title_font_size: 14, - title_font_color: "black", - x_label_font_size: 12, - y_label_font_size: 12, - x_label_font_color: "black", - y_label_font_color: "black", - show_x_axis: true, - show_y_axis: true, - x_scale: :default, - y_scale: :default, - show_x_ticks: true, - show_y_ticks: true, - show_x_ticks_label: true, - show_y_ticks_label: true, - x_tick_font_size: 10, - y_tick_font_size: 10, - x_tick_label_rotation: 0, - y_tick_label_rotation: 0, - x_tick_font_color: "black", - y_tick_font_color: "black", - show_v_grid: true, - show_h_grid: true, - legend_font_size: 9, - legend_font_color: "black", - plot_line_style: "_", - plot_line_width: 1.0, - grid_linestyle: "--", - grid_linewidth: 1.0, - grid_color: "#ddd", - x_padding: 0.05, - y_padding: 0.05 - - @spec cast_options(map()) :: __MODULE__.t() - def cast_options(opts), do: struct(__MODULE__, opts) - - @spec set_options_in_figure(Figure.t(), __MODULE__.t()) :: Figure.t() - def set_options_in_figure(%Figure{} = figure, %__MODULE__{} = options) do - figure - |> set_options_in_figure_struct(options) - |> set_options_in_axes_struct(options) - |> set_options_in_rc_params_struct(options) - end + @immutable_keys [:axes, :rc_params, :elements, :region_x, :region_y, :region_title, :region_content, :region_legend, :region_color_bar] @spec set_options_in_figure(Figure.t(), keyword()) :: Figure.t() def set_options_in_figure(%Figure{} = figure, opts) do + opts = sanitize(opts) figure |> cast_figure(opts) |> cast_axes(opts) @@ -137,133 +44,8 @@ defmodule Matplotex.Figure.Areal.PlotOptions do %Figure{figure | rc_params: rc_params |> RcParams.update_with_font(opts) |> struct(opts)} end - defp set_options_in_figure_struct(%Figure{} = figure, %__MODULE__{ - figsize: figsize, - figrows: figrows, - figcolumns: figcolumns, - margin: margin - }) do - %Figure{figure | figsize: figsize, rows: figrows, columns: figcolumns, margin: margin} - end - - defp set_options_in_axes_struct( - %Figure{axes: axes} = figure, - %__MODULE__{ - title: title, - x_label: x_label, - y_label: y_label, - x_limit: x_limit, - y_limit: y_limit - } - ) do - %Figure{ - figure - | axes: %{ - axes - | title: title, - label: %TwoD{x: x_label, y: y_label}, - limit: %TwoD{x: x_limit, y: y_limit} - } - } + defp sanitize(opts) do + Keyword.drop(opts, @immutable_keys) end - defp set_options_in_rc_params_struct( - %Figure{ - axes: axes, - rc_params: - %RcParams{ - title_font: title_font, - x_label_font: x_label_font, - y_label_font: y_label_font, - x_tick_font: x_tick_font, - y_tick_font: y_tick_font, - legend_font: legend_font - } = rc_params - } = figure, - %__MODULE__{ - title_font_size: title_font_size, - title_font_color: title_font_color, - x_label_font_size: x_label_font_size, - y_label_font_size: y_label_font_size, - x_label_font_color: x_label_font_color, - y_label_font_color: y_label_font_color, - show_x_axis: show_x_axis, - show_y_axis: show_x_axis, - x_scale: x_scale, - y_scale: y_scale, - show_x_ticks: show_x_ticks, - show_y_ticks: show_y_ticks, - show_x_ticks_label: _show_x_ticks_label, - show_y_ticks_label: _show_y_ticks_label, - x_tick_font_size: x_tick_font_size, - y_tick_font_size: y_tick_font_size, - x_tick_label_rotation: _x_tick_label_rotation, - y_tick_label_rotation: _y_tick_label_rotation, - x_tick_font_color: x_tick_font_color, - y_tick_font_color: y_tick_font_color, - show_v_grid: show_v_grid, - show_h_grid: show_h_grid, - legend_font_size: legend_font_size, - legend_font_color: legend_font_color, - plot_line_style: _plot_line_style, - plot_line_width: _plot_line_width, - grid_color: grid_color, - grid_linestyle: grid_linestyle, - grid_linewidth: grid_linewidth, - x_padding: x_padding, - y_padding: y_padding - } - ) do - %Figure{ - figure - | rc_params: %RcParams{ - rc_params - | title_font: %Font{ - title_font - | font_size: title_font_size, - fill: title_font_color - }, - x_label_font: %Font{ - x_label_font - | font_size: x_label_font_size, - fill: x_label_font_color - }, - y_label_font: %Font{ - y_label_font - | font_size: y_label_font_size, - fill: y_label_font_color - }, - x_tick_font: %Font{ - x_tick_font - | font_size: x_tick_font_size, - fill: x_tick_font_color - }, - y_tick_font: %Font{ - y_tick_font - | font_size: y_tick_font_size, - fill: y_tick_font_color - }, - legend_font: %Font{ - legend_font - | font_size: legend_font_size, - fill: legend_font_color - }, - grid_color: grid_color, - grid_linestyle: grid_linestyle, - grid_linewidth: grid_linewidth, - x_padding: x_padding, - y_padding: y_padding - }, - axes: %{ - axes - | show_x_axis: show_x_axis, - show_y_axis: show_x_axis, - show_x_ticks: show_x_ticks, - show_y_ticks: show_y_ticks, - show_v_grid: show_v_grid, - show_h_grid: show_h_grid, - scale: %TwoD{x: x_scale, y: y_scale} - } - } - end end diff --git a/test/matplotex/figure/areal/plot_options_test.exs b/test/matplotex/figure/areal/plot_options_test.exs index 30f0d5e..832b058 100644 --- a/test/matplotex/figure/areal/plot_options_test.exs +++ b/test/matplotex/figure/areal/plot_options_test.exs @@ -1,34 +1,24 @@ defmodule Matplotex.Figure.Areal.PlotOptionsTest do - alias Matplotex.Figure - alias Matplotex.Figure.Areal.PlotOptions + use Matplotex.PlotCase setup do {:ok, %{figure: Matplotex.FrameHelpers.sample_figure()}} end - describe "cast_options/1" do - test "generate PlotOptions struct", %{} do - assert %PlotOptions{} = - PlotOptions.cast_options( - margin: 10, - x_limit: {0, 10}, - y_limit: {-10, 10} - ) - end - end + describe "set_options_in_figure" do + test "plot options should't allow some restricted keys", %{figure: figure} do + figure = figure + |> Matplotex.set_title("New title") + |> Matplotex.plot([1,2,3,4,5], [1,2,3,4,5], [x_lagel: "X", axes: nil, region_x: nil, region_y: nil , region_content: nil, region_title: nil, elements: nil]) + refute figure.axes.region_x == nil + refute figure.axes.region_y == nil + refute figure.axes.region_content == nil + refute figure.axes.region_title == nil + refute figure.axes.element == nil + refute figure.axes == nil - describe "set_options_in_figure/2" do - test "update figure struct with plotting options", %{figure: figure} do - options = - PlotOptions.cast_options( - margin: 10, - x_limit: {0, 10}, - y_limit: {-10, 10} - ) - - assert %Figure{margin: 10, axes: %{limit: %{x: {0, 10}, y: {-10, 10}}}} = - PlotOptions.set_options_in_figure(figure, options) end end + end From 02052e848d5ec3e4de0458c265fcfb203feb5d90 Mon Sep 17 00:00:00 2001 From: Mohammed Sadique Date: Mon, 27 Jan 2025 22:25:40 +0530 Subject: [PATCH 2/2] format --- lib/matplotex/figure/areal/plot_options.ex | 14 ++++++++++++-- .../figure/areal/plot_options_test.exs | 19 +++++++++++++------ 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/lib/matplotex/figure/areal/plot_options.ex b/lib/matplotex/figure/areal/plot_options.ex index 0c1680e..8e9c526 100644 --- a/lib/matplotex/figure/areal/plot_options.ex +++ b/lib/matplotex/figure/areal/plot_options.ex @@ -4,11 +4,22 @@ defmodule Matplotex.Figure.Areal.PlotOptions do alias Matplotex.Figure.TwoD alias Matplotex.Figure.RcParams - @immutable_keys [:axes, :rc_params, :elements, :region_x, :region_y, :region_title, :region_content, :region_legend, :region_color_bar] + @immutable_keys [ + :axes, + :rc_params, + :elements, + :region_x, + :region_y, + :region_title, + :region_content, + :region_legend, + :region_color_bar + ] @spec set_options_in_figure(Figure.t(), keyword()) :: Figure.t() def set_options_in_figure(%Figure{} = figure, opts) do opts = sanitize(opts) + figure |> cast_figure(opts) |> cast_axes(opts) @@ -47,5 +58,4 @@ defmodule Matplotex.Figure.Areal.PlotOptions do defp sanitize(opts) do Keyword.drop(opts, @immutable_keys) end - end diff --git a/test/matplotex/figure/areal/plot_options_test.exs b/test/matplotex/figure/areal/plot_options_test.exs index 832b058..adf49be 100644 --- a/test/matplotex/figure/areal/plot_options_test.exs +++ b/test/matplotex/figure/areal/plot_options_test.exs @@ -1,5 +1,4 @@ defmodule Matplotex.Figure.Areal.PlotOptionsTest do - use Matplotex.PlotCase setup do @@ -8,17 +7,25 @@ defmodule Matplotex.Figure.Areal.PlotOptionsTest do describe "set_options_in_figure" do test "plot options should't allow some restricted keys", %{figure: figure} do - figure = figure - |> Matplotex.set_title("New title") - |> Matplotex.plot([1,2,3,4,5], [1,2,3,4,5], [x_lagel: "X", axes: nil, region_x: nil, region_y: nil , region_content: nil, region_title: nil, elements: nil]) + figure = + figure + |> Matplotex.set_title("New title") + |> Matplotex.plot([1, 2, 3, 4, 5], [1, 2, 3, 4, 5], + x_lagel: "X", + axes: nil, + region_x: nil, + region_y: nil, + region_content: nil, + region_title: nil, + elements: nil + ) + refute figure.axes.region_x == nil refute figure.axes.region_y == nil refute figure.axes.region_content == nil refute figure.axes.region_title == nil refute figure.axes.element == nil refute figure.axes == nil - end end - end