From 77e02bc3b498698efcb6ce508b8d185bb8598ac2 Mon Sep 17 00:00:00 2001 From: Mohammed Sadique Date: Tue, 26 Nov 2024 16:16:24 +0530 Subject: [PATCH 1/5] set region xy --- lib/matplotex/element/slice.ex | 17 +- lib/matplotex/figure/areal/bar_chart.ex | 1 + lib/matplotex/figure/font.ex | 8 +- lib/matplotex/figure/lead.ex | 259 +++++++++++------------- lib/matplotex/figure/rc_params.ex | 6 +- lib/matplotex/helpers.ex | 10 +- test/matplotex/figure/lead_test.exs | 7 + 7 files changed, 162 insertions(+), 146 deletions(-) diff --git a/lib/matplotex/element/slice.ex b/lib/matplotex/element/slice.ex index a431105..5891ef8 100644 --- a/lib/matplotex/element/slice.ex +++ b/lib/matplotex/element/slice.ex @@ -2,7 +2,22 @@ defmodule Matplotex.Element.Slice do alias Matplotex.Element use Element - defstruct [:type, :x1, :y1, :x2, :y2, :cx, :cy, :data,:percentage, :radius, :color, :label, :legend] + defstruct [ + :type, + :x1, + :y1, + :x2, + :y2, + :cx, + :cy, + :data, + :percentage, + :radius, + :color, + :label, + :legend + ] + # TODO: change the fucntion name assemble to to_svg @impl Element diff --git a/lib/matplotex/figure/areal/bar_chart.ex b/lib/matplotex/figure/areal/bar_chart.ex index 7dfa848..0f750d9 100644 --- a/lib/matplotex/figure/areal/bar_chart.ex +++ b/lib/matplotex/figure/areal/bar_chart.ex @@ -8,6 +8,7 @@ defmodule Matplotex.Figure.Areal.BarChart do alias Matplotex.Figure alias Matplotex.Figure.Areal use Areal + frame( legend: %Legend{}, coords: %Coords{}, diff --git a/lib/matplotex/figure/font.ex b/lib/matplotex/figure/font.ex index 7715ead..d764f33 100644 --- a/lib/matplotex/figure/font.ex +++ b/lib/matplotex/figure/font.ex @@ -5,13 +5,19 @@ defmodule Matplotex.Figure.Font do @default_font_size 16 @default_font_weight "normal" @font_unit "pt" + @pt_to_inch_ratio 1 / 72 + @text_rotation 0 + @flate 0 defstruct font_size: @default_font_size, font_style: @default_font_style, font_family: @default_font_family, font_weight: @default_font_weight, fill: @default_font_color, - unit_of_measurement: @font_unit + unit_of_measurement: @font_unit, + pt_to_inch_ratio: @pt_to_inch_ratio, + rotation: @text_rotation, + flate: @flate def font_keys() do Map.keys(%__MODULE__{}) -- [:__struct__] diff --git a/lib/matplotex/figure/lead.ex b/lib/matplotex/figure/lead.ex index 611e075..a288f84 100644 --- a/lib/matplotex/figure/lead.ex +++ b/lib/matplotex/figure/lead.ex @@ -1,4 +1,6 @@ defmodule Matplotex.Figure.Lead do + alias Matplotex.Figure.Font + alias Matplotex.Figure.Region alias Matplotex.Figure.TwoD alias Matplotex.Figure.Dimension alias Matplotex.Figure.Coords @@ -8,28 +10,6 @@ defmodule Matplotex.Figure.Lead do @padding 10 / 96 @tick_line_offset 5 / 96 - # def set_spines(%Figure{} = figure) do - # # {{width, height}, {bottom_left, top_left, bottom_right, top_right}} = - # {%Figure{axes: axes}, {width, height}} = - # figure - # |> peel_margin() - # |> peel_label_offsets() - # |> peel_title_offset() - # |> peel_tick_offsets() - # |> calculate_corners() - # |> peel_margin() - - # axes = %{ - # axes - # | size: %{width: width * @dpi, height: height * @dpi} - # } - - # %Figure{figure | axes: axes} - # end - - # def set_spines(_) do - # raise ArgumentError, message: "Invalid figure to proceed" - # end def set_spines(%Figure{} = figure) do figure |> set_xlabel_coords() @@ -40,6 +20,15 @@ defmodule Matplotex.Figure.Lead do |> set_border_coords() end + def set_regions(%Figure{} = figure) do + figure + |> set_region_xy() + + # |>set_region_title() + # |>set_region_legend() + # |>set_region_content() + end + def set_border(%Figure{margin: margin, axes: axes, figsize: {fig_width, fig_height}} = figure) do margin = margin / 2 lx = fig_width * margin @@ -49,6 +38,63 @@ defmodule Matplotex.Figure.Lead do %Figure{figure | axes: %{axes | border: {lx, by, rx, ty}}} end + defp set_region_xy( + %Figure{ + figsize: {f_width, f_height}, + axes: + %{ + region_x: region_x, + region_y: region_y, + label: %TwoD{y: y_label, x: x_label}, + tick: %TwoD{y: y_ticks, x: x_ticks} + } = axes, + rc_params: %RcParams{ + x_label_font: x_label_font, + x_tick_font: x_tick_font, + y_label_font: y_label_font, + y_tick_font: y_tick_font, + label_padding: label_padding, + tick_line_length: tick_line_length + } + } = figure + ) do + # region_x = %Region{x: total space required for ylabel plus yticks plus ytickline plus ypadding y: 0} + space_for_ylabel = height_required_for_text(y_label_font, y_label) + y_tick = Enum.max_by(y_ticks, &String.length/1) + space_for_yticks = length_required_for_text(y_tick_font, y_tick) + + space_required_for_region_y = + [space_for_ylabel, y_tick, space_for_yticks, label_padding, tick_line_length] |> Enum.sum() + + space_for_x_label = height_required_for_text(x_label_font, x_label) + x_tick = Enum.max_by(x_ticks, &String.length/1) + space_for_x_tick = height_required_for_text(x_tick_font, x_tick) + + space_required_for_region_x = + [space_for_x_label, x_tick, space_for_x_tick, label_padding, tick_line_length] |> Enum.sum() + + %Figure{ + figure + | axes: %{ + axes + | region_x: %Region{ + region_x + | x: space_required_for_region_y, + y: 0, + height: space_required_for_region_x, + width: f_width - space_required_for_region_y + }, + region_y: %Region{ + region_y + | x: 0, + y: space_required_for_region_x, + width: space_required_for_region_y, + height: f_height - space_required_for_region_x + } + } + } + end + defp set_xlabel_coords(%Figure{axes: %{tick: %{y: nil}, show_y_ticks: true}} = figure) do figure |> generate_yticks() @@ -273,58 +319,6 @@ defmodule Matplotex.Figure.Lead do # TODO: Sort out how the user gets the control on font of the all texts - # defp calculate_corners( - # {%Figure{axes: %{coords: coords} = axes, figsize: {f_width, f_height}, margin: margin} = - # figure, {width, height}} - # ) do - # by = f_height - height - # ty = f_height - f_height * margin - # lx = f_width - width - # rx = f_width - f_width * margin - - # {%Figure{ - # figure - # | axes: %{ - # axes - # | coords: %{ - # coords - # | bottom_left: {lx * @dpi, by * @dpi}, - # top_left: {lx * @dpi, ty * @dpi}, - # bottom_right: {rx * @dpi, by * @dpi}, - # top_right: {rx * @dpi, ty * @dpi} - # } - # } - # }, {width, height}} - # end - - # defp peel_margin( - # %Figure{figsize: {width, height}, axes: %{coords: coords} = axes, margin: margin} = - # figure - # ) do - # label_coords = {width * margin, height * margin} - - # {%Figure{ - # figure - # | axes: %{ - # axes - # | coords: %{ - # coords - # | title: {width * margin, height - height * margin}, - # y_label: label_coords, - # x_label: label_coords - # } - # } - # }, {width - width * margin, height - height * margin}} - # end - - # # defp peel_margin({width, height, corners}, margin, {f_width, f_height}) do - # # {{width - f_width * margin, height - f_height * margin}, corners} - # # end - # defp peel_margin( - # {%Figure{figsize: {f_width, f_height}, margin: margin} = figure, {width, height}} - # ) do - # {figure, {width - f_width * margin, height - f_height * margin}} - # end defp label_offset(nil, _font_size), do: 0 defp label_offset("", _font_size), do: 0 @@ -336,45 +330,6 @@ defmodule Matplotex.Figure.Lead do font_size * @pt_to_inch + @padding end - # defp peel_label_offsets( - # {%Figure{ - # axes: %{coords: %Coords{x_label: {xlx, xly}, y_label: {ylx, yly}} = coords, label: %{}} = axes, - # rc_params: rc_params - # } = figure, {width, height}} - # ) do - # x_label_font_size = RcParams.get_rc(rc_params, :get_x_label_font_size) - # y_label_font_size = RcParams.get_rc(rc_params, :get_y_label_font_size) - - # x_label_offset = x_label_font_size * @pt_to_inch + @padding - # y_label_offset = y_label_font_size * @pt_to_inch + @padding - - # {%Figure{ - # figure - # | axes: %{ - # axes - # | coords: %{ - # coords - # | x_ticks: {xlx + y_label_offset, xly - x_label_offset}, - # y_ticks: {ylx + y_label_offset, yly - x_label_offset} - # } - # } - # }, {width - x_label_offset, height - y_label_offset}} - # end - - # defp peel_title_offset( - # {%Figure{ - # rc_params: rc_params, - # axes: %{title: title, coords: %Coords{title: {tx, ty}} = coords} = axes - # } = figure, {width, height}} - # ) do - # title_font_size = RcParams.get_rc(rc_params, :get_title_font_size) - - # title_offset = title_font_size * @pt_to_inch + @padding - # top_left = {tx, ty - title_offset} - - # {%Figure{figure | axes: %{axes | title: %{title | height: title_offset * @dpi}, coords: %Coords{coords | top_left: {tlx, tly}}}}, {width, height - title_offset}} - # end - defp ytick_offset(y_ticks, font_size) do tick_size = y_ticks |> Enum.max_by(fn tick -> tick_length(tick) end) |> tick_length() font_size * @pt_to_inch * tick_size + @tick_line_offset + @padding @@ -384,29 +339,6 @@ defmodule Matplotex.Figure.Lead do label_offset(xticks, font_size) + @tick_line_offset end - # defp peel_tick_offsets( - # {%Figure{ - # rc_params: rc_params, - # axes: %{tick: %{y: y_ticks}, coords: %Coords{x_ticks: {xtx, _}} = coords} = axes, - # margin: margin, - # figsize: {_width, figheight} - # } = figure, {width, height}} - # ) do - # tick_size = y_ticks |> Enum.max_by(fn tick -> tick_length(tick) end) |> tick_length() - # y_tick_font_size = RcParams.get_rc(rc_params, :get_y_tick_font_size) - # x_tick_font_size = RcParams.get_rc(rc_params, :get_x_tick_font_size) - # y_tick_offset = y_tick_font_size * @pt_to_inch * tick_size - # x_tick_offset = x_tick_font_size * @pt_to_inch - - # {%Figure{ - # figure - # | axes: %{ - # axes - # | coords: %Coords{coords | title: {(xtx + x_tick_offset) * @dpi, figheight * margin * @dpi}} - # } - # }, {width - y_tick_offset, height - x_tick_offset}} - # end - defp tick_length(tick) when is_integer(tick) do tick |> Integer.to_string() |> String.length() end @@ -430,4 +362,59 @@ defmodule Matplotex.Figure.Lead do defp generate_xticks(%Figure{axes: %module{} = axes} = figure) do %Figure{figure | axes: module.generate_xticks(axes)} end + + defp height_required_for_text( + %Font{ + font_size: font_size, + pt_to_inch_ratio: pt_to_inch_ratio, + flate: flate, + rotation: 0 + }, + _text + ), + do: font_size * pt_to_inch_ratio + flate + + defp height_required_for_text( + %Font{ + font_size: font_size, + pt_to_inch_ratio: pt_to_inch_ratio, + rotation: rotation, + flate: flate + }, + text + ) do + text_height = font_size * pt_to_inch_ratio + text_length = String.length(text) * pt_to_inch_ratio + rotation = deg_to_rad(rotation) + height_for_rotation = :math.sin(rotation) * text_length + text_height + height_for_rotation + flate + end + + defp length_required_for_text( + %Font{ + font_size: font_size, + pt_to_inch_ratio: pt_to_inch_ratio, + flate: flate, + rotation: 0 + }, + text + ), + do: String.length(text) * font_size * pt_to_inch_ratio + flate + + defp length_required_for_text( + %Font{ + font_size: font_size, + pt_to_inch_ratio: pt_to_inch_ratio, + flate: flate, + rotation: rotation + }, + text + ) do + text_length = String.length(text) * pt_to_inch_ratio + rotation = deg_to_rad(rotation) + leng_for_rotation = :math.cos(rotation) * text_length + leng_for_rotation + flate + end + + defp deg_to_rad(deg), do: deg * :math.pi() / 180 end diff --git a/lib/matplotex/figure/rc_params.ex b/lib/matplotex/figure/rc_params.ex index 0f69f11..aac1bbe 100644 --- a/lib/matplotex/figure/rc_params.ex +++ b/lib/matplotex/figure/rc_params.ex @@ -13,9 +13,10 @@ defmodule Matplotex.Figure.RcParams do @grid_linewidth 1 @grid_line_alpha 0.5 @font_uom "pt" - @tick_line_length 5 + @tick_line_length 5 / 96 @font %Font{} @chart_padding 0.05 + @label_padding 5 / 96 defstruct x_tick_font: @font, y_tick_font: @font, x_label_font: @font, @@ -40,7 +41,8 @@ defmodule Matplotex.Figure.RcParams do font_uom: @font_uom, tick_line_length: @tick_line_length, x_padding: @chart_padding, - y_padding: @chart_padding + y_padding: @chart_padding, + label_padding: @label_padding def get_rc(%__MODULE__{} = rc_param, get_func) do apply(__MODULE__, get_func, [rc_param]) diff --git a/lib/matplotex/helpers.ex b/lib/matplotex/helpers.ex index 64dec96..c587dc0 100644 --- a/lib/matplotex/helpers.ex +++ b/lib/matplotex/helpers.ex @@ -294,13 +294,11 @@ defmodule Matplotex.Helpers do end def emission_pie() do - categories = ["2008","2009","2010","2011"] -values = [18.48923375, -17.1923791, -17.48479218, -17.02021634] + categories = ["2008", "2009", "2010", "2011"] + values = [18.48923375, 17.1923791, 17.48479218, 17.02021634] + + colors = ["#76b5c5", "#DEDEDE", "#FBD1A2", "#6195B4"] - colors = ["#76b5c5","#DEDEDE","#FBD1A2","#6195B4"] values |> Matplotex.pie(colors: colors, labels: categories) |> Matplotex.set_title("Asias Emission distribution(2008-2011)") diff --git a/test/matplotex/figure/lead_test.exs b/test/matplotex/figure/lead_test.exs index 091fc69..fedecdf 100644 --- a/test/matplotex/figure/lead_test.exs +++ b/test/matplotex/figure/lead_test.exs @@ -1,4 +1,5 @@ defmodule Matplotex.Figure.LeadTest do + alias Matplotex.Figure.Region alias Matplotex.Figure.Coords alias Matplotex.Figure.LinePlot alias Matplotex.Figure @@ -232,4 +233,10 @@ defmodule Matplotex.Figure.LeadTest do assert blx == font_size / 150 + @padding_and_tick_line_space end end + + describe "set_regions/1" do + test "sets retion_x", %{figure: figure} do + assert %Figure{axes: %{region_x: %Region{x: x, y: y}}} = Lead.set_regions(figure) + end + end end From 88a5b6ac7424e902d1975d2adbfa704ee640d46d Mon Sep 17 00:00:00 2001 From: Mohammed Sadique Date: Tue, 26 Nov 2024 17:12:43 +0530 Subject: [PATCH 2/5] testcase for xy --- lib/matplotex/figure/lead.ex | 10 +++++----- test/matplotex/figure/lead_test.exs | 13 +++++++++++-- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/lib/matplotex/figure/lead.ex b/lib/matplotex/figure/lead.ex index a288f84..c255cf4 100644 --- a/lib/matplotex/figure/lead.ex +++ b/lib/matplotex/figure/lead.ex @@ -60,14 +60,14 @@ defmodule Matplotex.Figure.Lead do ) do # region_x = %Region{x: total space required for ylabel plus yticks plus ytickline plus ypadding y: 0} space_for_ylabel = height_required_for_text(y_label_font, y_label) - y_tick = Enum.max_by(y_ticks, &String.length/1) + y_tick = Enum.max_by(y_ticks, &tick_length(&1)) space_for_yticks = length_required_for_text(y_tick_font, y_tick) space_required_for_region_y = [space_for_ylabel, y_tick, space_for_yticks, label_padding, tick_line_length] |> Enum.sum() space_for_x_label = height_required_for_text(x_label_font, x_label) - x_tick = Enum.max_by(x_ticks, &String.length/1) + x_tick = Enum.max_by(x_ticks, &tick_length/1) space_for_x_tick = height_required_for_text(x_tick_font, x_tick) space_required_for_region_x = @@ -384,7 +384,7 @@ defmodule Matplotex.Figure.Lead do text ) do text_height = font_size * pt_to_inch_ratio - text_length = String.length(text) * pt_to_inch_ratio + text_length = tick_length(text) * pt_to_inch_ratio rotation = deg_to_rad(rotation) height_for_rotation = :math.sin(rotation) * text_length text_height + height_for_rotation + flate @@ -399,7 +399,7 @@ defmodule Matplotex.Figure.Lead do }, text ), - do: String.length(text) * font_size * pt_to_inch_ratio + flate + do: tick_length(text) * font_size * pt_to_inch_ratio + flate defp length_required_for_text( %Font{ @@ -410,7 +410,7 @@ defmodule Matplotex.Figure.Lead do }, text ) do - text_length = String.length(text) * pt_to_inch_ratio + text_length = tick_length(text) * font_size * pt_to_inch_ratio rotation = deg_to_rad(rotation) leng_for_rotation = :math.cos(rotation) * text_length leng_for_rotation + flate diff --git a/test/matplotex/figure/lead_test.exs b/test/matplotex/figure/lead_test.exs index fedecdf..565b4ed 100644 --- a/test/matplotex/figure/lead_test.exs +++ b/test/matplotex/figure/lead_test.exs @@ -236,7 +236,16 @@ defmodule Matplotex.Figure.LeadTest do describe "set_regions/1" do test "sets retion_x", %{figure: figure} do - assert %Figure{axes: %{region_x: %Region{x: x, y: y}}} = Lead.set_regions(figure) - end + assert %Figure{ + axes: %{ + region_x: %Region{x: rxx, y: rxy, width: rxwidth, height: rxheight}, + region_y: %Region{x: ryx, y: ryy, width: rywidth, height: ryheight} + } + } = Lead.set_regions(figure) + + assert Enum.all?([rxx, rxwidth, rxheight,ryy, rywidth, ryheight], & &1 > 0) + assert Enum.all?([rxy, ryx], & &1==0) + + end end end From 7f244aa5f7b79a09ff7693c4265eb42bf4f67361 Mon Sep 17 00:00:00 2001 From: Mohammed Sadique Date: Tue, 26 Nov 2024 20:25:12 +0530 Subject: [PATCH 3/5] added region title --- lib/matplotex/figure/areal/bar_chart.ex | 2 +- lib/matplotex/figure/areal/line_plot.ex | 2 +- lib/matplotex/figure/areal/region.ex | 3 ++ lib/matplotex/figure/areal/scatter.ex | 2 +- .../figure/areal/xy_region/coords.ex | 3 ++ lib/matplotex/figure/lead.ex | 44 +++++++++++++++---- lib/matplotex/figure/region.ex | 3 -- test/matplotex/figure/lead_test.exs | 39 +++++++++++++--- 8 files changed, 77 insertions(+), 21 deletions(-) create mode 100644 lib/matplotex/figure/areal/region.ex create mode 100644 lib/matplotex/figure/areal/xy_region/coords.ex delete mode 100644 lib/matplotex/figure/region.ex diff --git a/lib/matplotex/figure/areal/bar_chart.ex b/lib/matplotex/figure/areal/bar_chart.ex index 0f750d9..753684c 100644 --- a/lib/matplotex/figure/areal/bar_chart.ex +++ b/lib/matplotex/figure/areal/bar_chart.ex @@ -1,6 +1,6 @@ defmodule Matplotex.Figure.Areal.BarChart do import Matplotex.Figure.Numer - alias Matplotex.Figure.Region + alias Matplotex.Figure.Areal.Region alias Matplotex.Figure.Dataset alias Matplotex.Element.Rect alias Matplotex.Figure.RcParams diff --git a/lib/matplotex/figure/areal/line_plot.ex b/lib/matplotex/figure/areal/line_plot.ex index ee52330..adaee5f 100644 --- a/lib/matplotex/figure/areal/line_plot.ex +++ b/lib/matplotex/figure/areal/line_plot.ex @@ -1,5 +1,5 @@ defmodule Matplotex.Figure.Areal.LinePlot do - alias Matplotex.Figure.Region + alias Matplotex.Figure.Areal.Region alias Matplotex.Figure.Areal.Ticker alias Matplotex.Figure.Marker alias Matplotex.Figure.Dataset diff --git a/lib/matplotex/figure/areal/region.ex b/lib/matplotex/figure/areal/region.ex new file mode 100644 index 0000000..213eb39 --- /dev/null +++ b/lib/matplotex/figure/areal/region.ex @@ -0,0 +1,3 @@ +defmodule Matplotex.Figure.Areal.Region do + defstruct [:x, :y, :width, :height, :name, :theta, :coords] +end diff --git a/lib/matplotex/figure/areal/scatter.ex b/lib/matplotex/figure/areal/scatter.ex index 83dcf42..3bddbfd 100644 --- a/lib/matplotex/figure/areal/scatter.ex +++ b/lib/matplotex/figure/areal/scatter.ex @@ -1,5 +1,5 @@ defmodule Matplotex.Figure.Areal.Scatter do - alias Matplotex.Figure.Region + alias Matplotex.Figure.Areal.Region alias Matplotex.Figure.Areal.Ticker alias Matplotex.Figure.Marker alias Matplotex.Figure.Dataset diff --git a/lib/matplotex/figure/areal/xy_region/coords.ex b/lib/matplotex/figure/areal/xy_region/coords.ex new file mode 100644 index 0000000..db4383d --- /dev/null +++ b/lib/matplotex/figure/areal/xy_region/coords.ex @@ -0,0 +1,3 @@ +defmodule Matplotex.Figure.Areal.XyRegion.Coords do + defstruct [:label, :ticks] +end diff --git a/lib/matplotex/figure/lead.ex b/lib/matplotex/figure/lead.ex index c255cf4..ae50877 100644 --- a/lib/matplotex/figure/lead.ex +++ b/lib/matplotex/figure/lead.ex @@ -1,6 +1,7 @@ defmodule Matplotex.Figure.Lead do + alias Matplotex.Figure.Areal.XyRegion.Coords, as: XyCoords alias Matplotex.Figure.Font - alias Matplotex.Figure.Region + alias Matplotex.Figure.Areal.Region alias Matplotex.Figure.TwoD alias Matplotex.Figure.Dimension alias Matplotex.Figure.Coords @@ -21,12 +22,13 @@ defmodule Matplotex.Figure.Lead do end def set_regions(%Figure{} = figure) do + figure |> set_region_xy() - - # |>set_region_title() + |> set_region_title() # |>set_region_legend() # |>set_region_content() + # |> set_border() end def set_border(%Figure{margin: margin, axes: axes, figsize: {fig_width, fig_height}} = figure) do @@ -63,14 +65,14 @@ defmodule Matplotex.Figure.Lead do y_tick = Enum.max_by(y_ticks, &tick_length(&1)) space_for_yticks = length_required_for_text(y_tick_font, y_tick) - space_required_for_region_y = + x_region_x = space_required_for_region_y = [space_for_ylabel, y_tick, space_for_yticks, label_padding, tick_line_length] |> Enum.sum() space_for_x_label = height_required_for_text(x_label_font, x_label) x_tick = Enum.max_by(x_ticks, &tick_length/1) space_for_x_tick = height_required_for_text(x_tick_font, x_tick) - space_required_for_region_x = + y_region_y = space_required_for_region_x = [space_for_x_label, x_tick, space_for_x_tick, label_padding, tick_line_length] |> Enum.sum() %Figure{ @@ -79,21 +81,45 @@ defmodule Matplotex.Figure.Lead do axes | region_x: %Region{ region_x - | x: space_required_for_region_y, + | x: x_region_x , y: 0, height: space_required_for_region_x, - width: f_width - space_required_for_region_y + width: f_width - space_required_for_region_y, + coords: %XyCoords{label: {x_region_x, 0}, ticks: {x_region_x, space_for_x_label}} }, region_y: %Region{ region_y | x: 0, - y: space_required_for_region_x, + y: y_region_y, width: space_required_for_region_y, - height: f_height - space_required_for_region_x + height: f_height - space_required_for_region_x, + coords: %XyCoords{label: {0, y_region_y}, ticks: {space_for_x_label, y_region_y}} } } } end + defp set_region_xy(figure), do: figure + + defp set_region_title(%Figure{figsize: {_f_width, f_height}, axes: %{title: title, region_x: %Region{width: region_x_width}, region_y: %Region{width: region_y_width, height: region_y_height} = region_y, region_title: region_title} = axes, rc_params: %RcParams{title_font: title_font}} = figure) do + space_for_title = height_required_for_text(title_font, title) + + %Figure{ + figure + | axes: %{ + axes + | region_title: %Region{ + region_title + | x: region_y_width, + y: f_height - space_for_title, + width: region_x_width, + height: space_for_title, + }, + region_y: %Region{ + region_y | + height: region_y_height - space_for_title + } + }} + end defp set_xlabel_coords(%Figure{axes: %{tick: %{y: nil}, show_y_ticks: true}} = figure) do figure diff --git a/lib/matplotex/figure/region.ex b/lib/matplotex/figure/region.ex deleted file mode 100644 index 55f37a1..0000000 --- a/lib/matplotex/figure/region.ex +++ /dev/null @@ -1,3 +0,0 @@ -defmodule Matplotex.Figure.Region do - defstruct [:x, :y, :width, :height, :name, :theta] -end diff --git a/test/matplotex/figure/lead_test.exs b/test/matplotex/figure/lead_test.exs index 565b4ed..e43e3f2 100644 --- a/test/matplotex/figure/lead_test.exs +++ b/test/matplotex/figure/lead_test.exs @@ -1,5 +1,5 @@ defmodule Matplotex.Figure.LeadTest do - alias Matplotex.Figure.Region + alias Matplotex.Figure.Areal.Region alias Matplotex.Figure.Coords alias Matplotex.Figure.LinePlot alias Matplotex.Figure @@ -13,7 +13,22 @@ defmodule Matplotex.Figure.LeadTest do setup do figure = Matplotex.FrameHelpers.sample_figure() - {:ok, %{figure: figure}} + + frame_width = 8 + frame_height = 6 + size = {frame_width, frame_height} + margin = 0.1 + x = [1, 3, 7, 4, 2, 5, 6] + y = [1, 3, 7, 4, 2, 5, 6] + ticks = [1, 2, 3, 4, 5, 6, 7] + figure2 = + x + |> Matplotex.plot(y) + |> Matplotex.figure(%{figsize: size, margin: margin}) + |> Matplotex.set_title("The Plot Title") + |> Matplotex.set_xticks(ticks) + |> Matplotex.set_yticks(ticks) + {:ok, %{figure: figure, figure2: figure2}} end describe "set_spines/1" do @@ -235,7 +250,7 @@ defmodule Matplotex.Figure.LeadTest do end describe "set_regions/1" do - test "sets retion_x", %{figure: figure} do + test "sets region_xy", %{figure2: figure} do assert %Figure{ axes: %{ region_x: %Region{x: rxx, y: rxy, width: rxwidth, height: rxheight}, @@ -243,9 +258,21 @@ defmodule Matplotex.Figure.LeadTest do } } = Lead.set_regions(figure) - assert Enum.all?([rxx, rxwidth, rxheight,ryy, rywidth, ryheight], & &1 > 0) - assert Enum.all?([rxy, ryx], & &1==0) + assert Enum.all?([rxx, rxwidth,rxheight, ryy, rywidth, ryheight], &(&1 > 0)) + assert Enum.all?([rxy, ryx], &(&1 ==0)) - end + end + test "set region title updates the values for titles space", %{figure2: figure} do + assert %Figure{ + axes: %{ + region_title: %Region{x: rtx, y: rty, width: rtwidth, height: rtheight} + } + } = Lead.set_regions(figure) + + assert Enum.all?([rtx, rty, rtwidth, rtheight], & &1> 0) + + + + end end end From ee3b8236999170b55f4693635ce4eb211665ab6c Mon Sep 17 00:00:00 2001 From: Mohammed Sadique Date: Wed, 27 Nov 2024 16:02:28 +0530 Subject: [PATCH 4/5] setting content --- lib/matplotex.ex | 16 +++- lib/matplotex/blueprint/chord.ex | 3 +- lib/matplotex/blueprint/frame.ex | 1 + lib/matplotex/figure.ex | 3 + lib/matplotex/figure/areal.ex | 4 + lib/matplotex/figure/lead.ex | 114 ++++++++++++++++++++++++---- lib/matplotex/figure/rc_params.ex | 6 +- lib/matplotex/pie_chart.ex | 38 ---------- test/matplotex/figure/lead_test.exs | 36 +++++++-- 9 files changed, 159 insertions(+), 62 deletions(-) delete mode 100644 lib/matplotex/pie_chart.ex diff --git a/lib/matplotex.ex b/lib/matplotex.ex index 2233f67..7a49f3c 100644 --- a/lib/matplotex.ex +++ b/lib/matplotex.ex @@ -19,7 +19,9 @@ defmodule Matplotex do end def bar(%Figure{} = figure, pos, values, width, opts) do - BarChart.create(figure, {pos, values, width}, opts) + figure + |> show_legend() + |> BarChart.create({pos, values, width}, opts) end def scatter(stream, opts) when is_struct(stream, Stream) do @@ -35,7 +37,9 @@ defmodule Matplotex do end def scatter(%Figure{} = figure, x, y, opts) do - Scatter.create(figure, {x, y}, opts) + figure + |> show_legend() + |> Scatter.create({x, y}, opts) end def pie(sizes, opts \\ []) do @@ -66,7 +70,9 @@ defmodule Matplotex do end def plot(%Figure{} = figure, x, y, opts) do - LinePlot.create(figure, {x, y}, opts) + figure + |> show_legend() + |> LinePlot.create({x, y}, opts) end @doc """ @@ -220,6 +226,10 @@ defmodule Matplotex do Figure.hide_v_grid(figure) end + def show_legend(figure) do + Figure.show_legend(figure) + end + @doc """ Function to update rc params, rc stands for runtime configuration ## Examples diff --git a/lib/matplotex/blueprint/chord.ex b/lib/matplotex/blueprint/chord.ex index 3425d55..c22481f 100644 --- a/lib/matplotex/blueprint/chord.ex +++ b/lib/matplotex/blueprint/chord.ex @@ -29,7 +29,8 @@ defmodule Matplotex.Blueprint.Chord do show_title: @true_by_default, element: [], legend_pos: nil, - border: nil + border: nil, + show_legend: @false_by_default ] |> Keyword.merge(opts) ) diff --git a/lib/matplotex/blueprint/frame.ex b/lib/matplotex/blueprint/frame.ex index 620e6c7..5802db3 100644 --- a/lib/matplotex/blueprint/frame.ex +++ b/lib/matplotex/blueprint/frame.ex @@ -85,6 +85,7 @@ defmodule Matplotex.Blueprint.Frame do errors: [], element: [], show_title: false, + show_legend: false, valid: @valid_by_default, margin: @default_margin, show_x_axis: @show_by_default, diff --git a/lib/matplotex/figure.ex b/lib/matplotex/figure.ex index df60209..aa7f199 100644 --- a/lib/matplotex/figure.ex +++ b/lib/matplotex/figure.ex @@ -50,6 +50,9 @@ defmodule Matplotex.Figure do def hide_v_grid(%__MODULE__{axes: %module{} = axes} = figure), do: %{figure | axes: module.hide_v_grid(axes)} + def show_legend(%__MODULE__{axes: %module{} = axes} = figure), + do: %{figure | axes: module.show_legend(axes)} + def materialize(%__MODULE__{axes: %module{}} = figure), do: module.materialize(figure) def update_figure(figure, params) do diff --git a/lib/matplotex/figure/areal.ex b/lib/matplotex/figure/areal.ex index 7fe98c1..272f0b2 100644 --- a/lib/matplotex/figure/areal.ex +++ b/lib/matplotex/figure/areal.ex @@ -199,6 +199,10 @@ defmodule Matplotex.Figure.Areal do def min_max(ticks) do Enum.min_max(ticks) end + + def show_legend(%__MODULE__{} = axes) do + %__MODULE__{axes | show_legend: true} + end end end diff --git a/lib/matplotex/figure/lead.ex b/lib/matplotex/figure/lead.ex index ae50877..5bcf8b5 100644 --- a/lib/matplotex/figure/lead.ex +++ b/lib/matplotex/figure/lead.ex @@ -22,12 +22,12 @@ defmodule Matplotex.Figure.Lead do end def set_regions(%Figure{} = figure) do - figure |> set_region_xy() |> set_region_title() - # |>set_region_legend() - # |>set_region_content() + |> set_region_legend() + |> set_region_content() + # |> set_border() end @@ -65,14 +65,16 @@ defmodule Matplotex.Figure.Lead do y_tick = Enum.max_by(y_ticks, &tick_length(&1)) space_for_yticks = length_required_for_text(y_tick_font, y_tick) - x_region_x = space_required_for_region_y = + x_region_x = + space_required_for_region_y = [space_for_ylabel, y_tick, space_for_yticks, label_padding, tick_line_length] |> Enum.sum() space_for_x_label = height_required_for_text(x_label_font, x_label) x_tick = Enum.max_by(x_ticks, &tick_length/1) space_for_x_tick = height_required_for_text(x_tick_font, x_tick) - y_region_y = space_required_for_region_x = + y_region_y = + space_required_for_region_x = [space_for_x_label, x_tick, space_for_x_tick, label_padding, tick_line_length] |> Enum.sum() %Figure{ @@ -81,7 +83,7 @@ defmodule Matplotex.Figure.Lead do axes | region_x: %Region{ region_x - | x: x_region_x , + | x: x_region_x, y: 0, height: space_required_for_region_x, width: f_width - space_required_for_region_y, @@ -98,9 +100,22 @@ defmodule Matplotex.Figure.Lead do } } end + defp set_region_xy(figure), do: figure - defp set_region_title(%Figure{figsize: {_f_width, f_height}, axes: %{title: title, region_x: %Region{width: region_x_width}, region_y: %Region{width: region_y_width, height: region_y_height} = region_y, region_title: region_title} = axes, rc_params: %RcParams{title_font: title_font}} = figure) do + defp set_region_title( + %Figure{ + figsize: {_f_width, f_height}, + axes: + %{ + title: title, + region_x: %Region{width: region_x_width}, + region_y: %Region{width: region_y_width, height: region_y_height} = region_y, + region_title: region_title + } = axes, + rc_params: %RcParams{title_font: title_font} + } = figure + ) do space_for_title = height_required_for_text(title_font, title) %Figure{ @@ -112,15 +127,86 @@ defmodule Matplotex.Figure.Lead do | x: region_y_width, y: f_height - space_for_title, width: region_x_width, - height: space_for_title, - }, - region_y: %Region{ - region_y | - height: region_y_height - space_for_title - } - }} + height: space_for_title + }, + region_y: %Region{ + region_y + | height: region_y_height - space_for_title + } + } + } end + defp set_region_legend( + %Figure{ + figsize: {f_width, f_heigt}, + axes: + %{ + show_legend: true, + region_x: %Region{x: x_region_x, width: region_x_width} = region_x, + region_title: %Region{height: region_title_height} = region_title, + region_legend: region_legend + } = axes, + rc_params: %RcParams{legend_width: legend_width} + } = figure + ) do + region_legend_width = f_width * legend_width + region_x_width_after_legend = region_x_width - region_legend_width + legend_region_x = x_region_x + region_x_width_after_legend + legend_region_y = f_heigt - region_title_height + + %Figure{ + figure + | axes: %{ + axes + | region_x: %Region{ + region_x + | width: region_x_width_after_legend + }, + region_title: %Region{ + region_title + | width: region_x_width_after_legend + }, + region_legend: %Region{ + region_legend + | x: legend_region_x, + y: legend_region_y, + width: region_legend_width, + height: legend_region_y + } + } + } + end + + defp set_region_legend(figure), do: figure + + defp set_region_content( + %Figure{ + axes: + %{ + region_x: %Region{x: x_region_x, width: region_x_width}, + region_y: %Region{y: y_region_y, height: region_y_height}, + region_content: region_content + } = axes + } = figure + ) do + %Figure{ + figure + | axes: %{ + axes + | region_content: %Region{ + region_content + | x: x_region_x, + y: y_region_y, + width: region_x_width, + height: region_y_height + } + } + } + end + + defp set_region_content(figure), do: figure + defp set_xlabel_coords(%Figure{axes: %{tick: %{y: nil}, show_y_ticks: true}} = figure) do figure |> generate_yticks() diff --git a/lib/matplotex/figure/rc_params.ex b/lib/matplotex/figure/rc_params.ex index aac1bbe..4c72e46 100644 --- a/lib/matplotex/figure/rc_params.ex +++ b/lib/matplotex/figure/rc_params.ex @@ -17,6 +17,8 @@ defmodule Matplotex.Figure.RcParams do @font %Font{} @chart_padding 0.05 @label_padding 5 / 96 + @default_legend_width_percentage 0.2 + @default_legend_items_orientation :horizontal defstruct x_tick_font: @font, y_tick_font: @font, x_label_font: @font, @@ -42,7 +44,9 @@ defmodule Matplotex.Figure.RcParams do tick_line_length: @tick_line_length, x_padding: @chart_padding, y_padding: @chart_padding, - label_padding: @label_padding + label_padding: @label_padding, + legend_width: @default_legend_width_percentage, + legend_items_orientation: @default_legend_items_orientation def get_rc(%__MODULE__{} = rc_param, get_func) do apply(__MODULE__, get_func, [rc_param]) diff --git a/lib/matplotex/pie_chart.ex b/lib/matplotex/pie_chart.ex deleted file mode 100644 index cc83af9..0000000 --- a/lib/matplotex/pie_chart.ex +++ /dev/null @@ -1,38 +0,0 @@ -defmodule Matplotex.PieChart do - @moduledoc """ - Module wraps functions to create a PieChart - - """ - - import Matplotex.Blueprint.Frame - alias Matplotex.PieChart.Plot - - @type word() :: String.t() - @type params() :: %{ - id: word(), - title: word(), - dataset: list(), - labels: list(), - color_palette: list(), - stroke_width: number(), - width: number(), - height: number(), - margin: number(), - legends: boolean() - } - frame() - @type t() :: frame_struct() - def create(params) do - __MODULE__ - |> Plot.new(params) - |> Plot.set_content() - |> Plot.add_elements() - |> Plot.generate_svg() - end -end diff --git a/test/matplotex/figure/lead_test.exs b/test/matplotex/figure/lead_test.exs index e43e3f2..9c13a5f 100644 --- a/test/matplotex/figure/lead_test.exs +++ b/test/matplotex/figure/lead_test.exs @@ -21,13 +21,15 @@ defmodule Matplotex.Figure.LeadTest do x = [1, 3, 7, 4, 2, 5, 6] y = [1, 3, 7, 4, 2, 5, 6] ticks = [1, 2, 3, 4, 5, 6, 7] + figure2 = x |> Matplotex.plot(y) |> Matplotex.figure(%{figsize: size, margin: margin}) |> Matplotex.set_title("The Plot Title") |> Matplotex.set_xticks(ticks) - |> Matplotex.set_yticks(ticks) + |> Matplotex.set_yticks(ticks) + {:ok, %{figure: figure, figure2: figure2}} end @@ -258,10 +260,10 @@ defmodule Matplotex.Figure.LeadTest do } } = Lead.set_regions(figure) - assert Enum.all?([rxx, rxwidth,rxheight, ryy, rywidth, ryheight], &(&1 > 0)) - assert Enum.all?([rxy, ryx], &(&1 ==0)) - + assert Enum.all?([rxx, rxwidth, rxheight, ryy, rywidth, ryheight], &(&1 > 0)) + assert Enum.all?([rxy, ryx], &(&1 == 0)) end + test "set region title updates the values for titles space", %{figure2: figure} do assert %Figure{ axes: %{ @@ -269,10 +271,34 @@ defmodule Matplotex.Figure.LeadTest do } } = Lead.set_regions(figure) - assert Enum.all?([rtx, rty, rtwidth, rtheight], & &1> 0) + assert Enum.all?([rtx, rty, rtwidth, rtheight], &(&1 > 0)) + end + + test "setting region legend", %{figure2: figure} do + figure = Matplotex.show_legend(figure) + assert %Figure{ + axes: %{ + region_legend: %Region{x: rlx, y: rly, width: rlwidth, height: rlheight} + } + } = Lead.set_regions(figure) + assert Enum.all?([rlx, rly, rlwidth, rlheight], &(&1 > 0)) + end + test "setting content takes the same width of x region and y region", %{figure2: figure} do + assert %Figure{ + axes: %{ + region_x: %Region{x: rxx, width: rxwidth}, + region_y: %Region{y: ryy, height: ryheight}, + region_content: %Region{x: rcx, y: rcy, width: rcwidth, height: rcheight} + } + } = Lead.set_regions(figure) + + assert rxx == rcx + assert ryy == rcy + assert rcwidth == rxwidth + assert ryheight == rcheight end end end From af1257dae569a0aed43141f480eff234e6d52a26 Mon Sep 17 00:00:00 2001 From: Mohammed Sadique Date: Wed, 27 Nov 2024 19:38:29 +0530 Subject: [PATCH 5/5] set all regions with framesize --- lib/matplotex.ex | 10 +++++++++- lib/matplotex/figure.ex | 11 +++++++++++ lib/matplotex/figure/areal.ex | 4 ++++ lib/matplotex/figure/areal/line_plot.ex | 6 +++++- lib/matplotex/figure/lead.ex | 22 ++++++++++++--------- test/matplotex/figure/lead_test.exs | 14 ++++++------- test/matplotex_test.exs | 26 +++++++++++++++++++++++++ 7 files changed, 75 insertions(+), 18 deletions(-) diff --git a/lib/matplotex.ex b/lib/matplotex.ex index 7a49f3c..7a25e91 100644 --- a/lib/matplotex.ex +++ b/lib/matplotex.ex @@ -217,11 +217,19 @@ defmodule Matplotex do iex> Matplotex.figure(figure, figsize: {10,6}, margin: 0.1) %Matplotex.Figure{} """ - + @deprecated def figure(figure, params) do Figure.update_figure(figure, params) end + def set_figure_size(figure, size) do + Figure.set_figure_size(figure, size) + end + + def set_margin(figure, margin) do + Figure.set_margin(figure, margin) + end + def hide_v_grid(figure) do Figure.hide_v_grid(figure) end diff --git a/lib/matplotex/figure.ex b/lib/matplotex/figure.ex index aa7f199..ff375f2 100644 --- a/lib/matplotex/figure.ex +++ b/lib/matplotex/figure.ex @@ -53,6 +53,17 @@ defmodule Matplotex.Figure do def show_legend(%__MODULE__{axes: %module{} = axes} = figure), do: %{figure | axes: module.show_legend(axes)} + def set_figure_size(%__MODULE__{margin: margin, axes: axes} = figure, {fwidth, fheight} = fsize) do + frame_size = {fwidth - fwidth * 2 * margin, fheight - fheight * 2 * margin} + + %{figure | figsize: fsize, axes: %{axes | size: frame_size}} + end + + def set_margin(%__MODULE__{figsize: {fwidth, fheight}, axes: axes} = figure, margin) do + frame_size = {fwidth - fwidth * 2 * margin, fheight - fheight * 2 * margin} + %__MODULE__{figure | margin: margin, axes: %{axes | size: frame_size}} + end + def materialize(%__MODULE__{axes: %module{}} = figure), do: module.materialize(figure) def update_figure(figure, params) do diff --git a/lib/matplotex/figure/areal.ex b/lib/matplotex/figure/areal.ex index 272f0b2..72db769 100644 --- a/lib/matplotex/figure/areal.ex +++ b/lib/matplotex/figure/areal.ex @@ -203,6 +203,10 @@ defmodule Matplotex.Figure.Areal do def show_legend(%__MODULE__{} = axes) do %__MODULE__{axes | show_legend: true} end + + def set_frame_size(%__MODULE__{} = axes, frame_size) do + %__MODULE__{axes | size: frame_size} + end end end diff --git a/lib/matplotex/figure/areal/line_plot.ex b/lib/matplotex/figure/areal/line_plot.ex index adaee5f..1bbc752 100644 --- a/lib/matplotex/figure/areal/line_plot.ex +++ b/lib/matplotex/figure/areal/line_plot.ex @@ -27,7 +27,11 @@ defmodule Matplotex.Figure.Areal.LinePlot do @marker_size 3.5 @impl Areal - def create(%Figure{axes: %__MODULE__{dataset: data} = axes} = figure, {x, y}, opts \\ []) do + def create( + %Figure{axes: %__MODULE__{dataset: data} = axes} = figure, + {x, y}, + opts \\ [] + ) do x = determine_numeric_value(x) y = determine_numeric_value(y) dataset = Dataset.cast(%Dataset{x: x, y: y}, opts) diff --git a/lib/matplotex/figure/lead.ex b/lib/matplotex/figure/lead.ex index 5bcf8b5..47709ec 100644 --- a/lib/matplotex/figure/lead.ex +++ b/lib/matplotex/figure/lead.ex @@ -23,12 +23,11 @@ defmodule Matplotex.Figure.Lead do def set_regions(%Figure{} = figure) do figure + |> set_frame_size() |> set_region_xy() |> set_region_title() |> set_region_legend() |> set_region_content() - - # |> set_border() end def set_border(%Figure{margin: margin, axes: axes, figsize: {fig_width, fig_height}} = figure) do @@ -40,15 +39,20 @@ defmodule Matplotex.Figure.Lead do %Figure{figure | axes: %{axes | border: {lx, by, rx, ty}}} end + defp set_frame_size(%Figure{margin: margin, figsize: {fwidth, fheight}, axes: axes} = figure) do + frame_size = {fwidth - fwidth * 2 * margin, fheight - fheight * 2 * margin} + %Figure{figure | axes: %{axes | size: frame_size}} + end + defp set_region_xy( %Figure{ - figsize: {f_width, f_height}, axes: %{ region_x: region_x, region_y: region_y, label: %TwoD{y: y_label, x: x_label}, - tick: %TwoD{y: y_ticks, x: x_ticks} + tick: %TwoD{y: y_ticks, x: x_ticks}, + size: {f_width, f_height} } = axes, rc_params: %RcParams{ x_label_font: x_label_font, @@ -105,13 +109,13 @@ defmodule Matplotex.Figure.Lead do defp set_region_title( %Figure{ - figsize: {_f_width, f_height}, axes: %{ title: title, region_x: %Region{width: region_x_width}, region_y: %Region{width: region_y_width, height: region_y_height} = region_y, - region_title: region_title + region_title: region_title, + size: {_f_width, f_height} } = axes, rc_params: %RcParams{title_font: title_font} } = figure @@ -139,13 +143,13 @@ defmodule Matplotex.Figure.Lead do defp set_region_legend( %Figure{ - figsize: {f_width, f_heigt}, axes: %{ show_legend: true, region_x: %Region{x: x_region_x, width: region_x_width} = region_x, region_title: %Region{height: region_title_height} = region_title, - region_legend: region_legend + region_legend: region_legend, + size: {f_width, f_height} } = axes, rc_params: %RcParams{legend_width: legend_width} } = figure @@ -153,7 +157,7 @@ defmodule Matplotex.Figure.Lead do region_legend_width = f_width * legend_width region_x_width_after_legend = region_x_width - region_legend_width legend_region_x = x_region_x + region_x_width_after_legend - legend_region_y = f_heigt - region_title_height + legend_region_y = f_height - region_title_height %Figure{ figure diff --git a/test/matplotex/figure/lead_test.exs b/test/matplotex/figure/lead_test.exs index 9c13a5f..d846a57 100644 --- a/test/matplotex/figure/lead_test.exs +++ b/test/matplotex/figure/lead_test.exs @@ -285,15 +285,15 @@ defmodule Matplotex.Figure.LeadTest do assert Enum.all?([rlx, rly, rlwidth, rlheight], &(&1 > 0)) end - test "setting content takes the same width of x region and y region", %{figure2: figure} do + test "setting content takes the same width of x region and y region", %{figure2: figure} do assert %Figure{ - axes: %{ - region_x: %Region{x: rxx, width: rxwidth}, - region_y: %Region{y: ryy, height: ryheight}, - region_content: %Region{x: rcx, y: rcy, width: rcwidth, height: rcheight} - } - } = Lead.set_regions(figure) + axes: %{ + region_x: %Region{x: rxx, width: rxwidth}, + region_y: %Region{y: ryy, height: ryheight}, + region_content: %Region{x: rcx, y: rcy, width: rcwidth, height: rcheight} + } + } = Lead.set_regions(figure) assert rxx == rcx assert ryy == rcy diff --git a/test/matplotex_test.exs b/test/matplotex_test.exs index a46c87e..84e37fe 100644 --- a/test/matplotex_test.exs +++ b/test/matplotex_test.exs @@ -1,6 +1,7 @@ defmodule MatplotexTest do alias Matplotex.InputError use Matplotex.PlotCase + alias Matplotex.Figure setup do x = [1, 3, 7, 4, 2, 5, 6] @@ -119,4 +120,29 @@ defmodule MatplotexTest do assert Matplotex.show(figure_mater) |> is_binary() end + + describe "set_figure_size" do + test "updates the frame size based on margin also", %{figure: figure} do + fwidth = 10 + fheight = 8 + + assert %Figure{margin: margin, axes: %{size: {width, height}}} = + Matplotex.set_figure_size(figure, {fwidth, fheight}) + + assert width == fwidth - fwidth * margin * 2 + assert height == fheight - fheight * margin * 2 + end + end + + describe "set_margin" do + test "updates the frame size according to margin", %{figure: figure} do + margin = 0.01 + + assert %Figure{figsize: {fwidth, fheight}, axes: %{size: {width, height}}} = + Matplotex.set_margin(figure, margin) + + assert width == fwidth - fwidth * margin * 2 + assert height == fheight - fheight * margin * 2 + end + end end