Skip to content

Commit e458ed0

Browse files
Merge pull request #23 from BigThinkcode/plot_options
Use plot options to plot a chart
2 parents 2022924 + 48c8d12 commit e458ed0

File tree

12 files changed

+268
-52
lines changed

12 files changed

+268
-52
lines changed

lib/matplotex.ex

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@ defmodule Matplotex do
1010
alias Matplotex.Figure
1111
alias Matplotex.Figure.Areal.BarChart
1212

13+
def bar(values, width) do
14+
bar(width, values, width, [])
15+
end
16+
17+
def bar(values, width, opts) when is_list(opts) do
18+
bar(width, values, width, opts)
19+
end
20+
1321
def bar(pos, values, width) do
1422
bar(pos, values, width, [])
1523
end

lib/matplotex/figure.ex

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ defmodule Matplotex.Figure do
33
@row_column_default 1
44
@margin_default 0.05
55
@figsize {10, 6}
6+
7+
@restricted_keys [:axes, :element, :valid?, :rc_params, :errors]
68
defstruct [
79
:id,
810
:axes,
@@ -32,6 +34,8 @@ defmodule Matplotex.Figure do
3234
struct(__MODULE__, opts)
3335
end
3436

37+
def restricted_keys(), do: @restricted_keys
38+
3539
# TODO: put error message in error
3640
# def put_error(figure, opts) do
3741

lib/matplotex/figure/areal/bar_chart.ex

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
defmodule Matplotex.Figure.Areal.BarChart do
22
import Matplotex.Figure.Numer
3+
alias Matplotex.Figure.Areal.PlotOptions
34
alias Matplotex.Figure.Areal.Region
45
alias Matplotex.Element.Legend
56
alias Matplotex.Figure.Dataset
@@ -41,6 +42,7 @@ defmodule Matplotex.Figure.Areal.BarChart do
4142
| rc_params: %RcParams{white_space: width, y_padding: 0},
4243
axes: %{axes | data: xydata, dataset: datasets}
4344
}
45+
|> PlotOptions.set_options_in_figure(opts)
4446
end
4547

4648
@impl Areal

lib/matplotex/figure/areal/line_plot.ex

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
defmodule Matplotex.Figure.Areal.LinePlot do
2+
alias Matplotex.Figure.Areal.PlotOptions
23
alias Matplotex.Utils.Algebra
34
alias Matplotex.Figure.Areal.Region
45
alias Matplotex.Figure.Areal.Ticker
@@ -31,14 +32,16 @@ defmodule Matplotex.Figure.Areal.LinePlot do
3132
def create(
3233
%Figure{axes: %__MODULE__{dataset: data} = axes} = figure,
3334
{x, y},
34-
opts \\ []
35+
opts
3536
) do
3637
x = determine_numeric_value(x)
3738
y = determine_numeric_value(y)
3839
dataset = Dataset.cast(%Dataset{x: x, y: y}, opts)
3940
datasets = data ++ [dataset]
4041
xydata = flatten_for_data(datasets)
42+
4143
%Figure{figure | axes: %{axes | data: xydata, dataset: datasets}}
44+
|> PlotOptions.set_options_in_figure(opts)
4245
end
4346

4447
@impl Areal

lib/matplotex/figure/areal/plot_options.ex

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,48 @@ defmodule Matplotex.Figure.Areal.PlotOptions do
9999
|> set_options_in_rc_params_struct(options)
100100
end
101101

102+
@spec set_options_in_figure(Figure.t(), keyword()) :: Figure.t()
103+
def set_options_in_figure(%Figure{} = figure, opts) do
104+
figure
105+
|> cast_figure(opts)
106+
|> cast_axes(opts)
107+
|> cast_rc_params(opts)
108+
end
109+
110+
defp cast_figure(figure, opts) do
111+
struct(figure, opts)
112+
end
113+
114+
defp cast_axes(%Figure{axes: axes} = figure, opts) do
115+
opts = Keyword.delete(opts, :label)
116+
117+
%Figure{
118+
figure
119+
| axes: axes |> struct(opts) |> cast_two_d_structs(opts)
120+
# |>fulfill_tick_and_lim()
121+
}
122+
end
123+
124+
# defp fulfill_tick_and_lim(%{tick: nil, limit: nil} = axes) do
125+
126+
# end
127+
128+
defp cast_two_d_structs(%{label: label, tick: tick, limit: limit} = axes, opts)
129+
when is_map(opts) do
130+
%{
131+
axes
132+
| label: TwoD.update(label, opts, :label),
133+
tick: TwoD.update(tick, opts, :tick),
134+
limit: TwoD.update(limit, opts, :limit)
135+
}
136+
end
137+
138+
defp cast_two_d_structs(axes, opts), do: cast_two_d_structs(axes, Enum.into(opts, %{}))
139+
140+
defp cast_rc_params(%Figure{rc_params: rc_params} = figure, opts) do
141+
%Figure{figure | rc_params: rc_params |> RcParams.update_with_font(opts) |> struct(opts)}
142+
end
143+
102144
defp set_options_in_figure_struct(%Figure{} = figure, %__MODULE__{
103145
figsize: figsize,
104146
figrows: figrows,

lib/matplotex/figure/areal/scatter.ex

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
defmodule Matplotex.Figure.Areal.Scatter do
2+
alias Matplotex.Figure.Areal.PlotOptions
23
alias Matplotex.Figure.Areal.Region
34
alias Matplotex.Figure.Areal.Ticker
45
alias Matplotex.Figure.Marker
@@ -32,7 +33,9 @@ defmodule Matplotex.Figure.Areal.Scatter do
3233
dataset = Dataset.cast(%Dataset{x: x, y: y}, opts)
3334
datasets = data ++ [dataset]
3435
xydata = flatten_for_data(datasets)
36+
3537
%Figure{figure | axes: %{axes | data: xydata, dataset: datasets}}
38+
|> PlotOptions.set_options_in_figure(opts)
3639
end
3740

3841
@impl Areal

lib/matplotex/figure/cast.ex

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ defmodule Matplotex.Figure.Cast do
8989
} = figure
9090
) do
9191
# Convert to the svg plane
92-
9392
{left_x, bottom_y} = Algebra.flip_y_coordinate({content_x, content_y})
9493

9594
{right_x, top_y} =

lib/matplotex/figure/lead.ex

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,29 @@ defmodule Matplotex.Figure.Lead do
7878
end
7979

8080
defp maybe_generate_ticks(ticks, limit, data, number_of_ticks) do
81-
if is_nil(ticks) || length(ticks) < 3 do
82-
generate_ticks(limit, data, ceil(number_of_ticks))
83-
else
84-
{ticks, limit}
81+
cond do
82+
is_nil(ticks) || length(ticks) < 3 ->
83+
generate_ticks(limit, data, ceil(number_of_ticks))
84+
85+
is_nil(limit) ->
86+
{ticks, generate_limit(data)}
87+
88+
true ->
89+
{ticks, limit}
8590
end
8691
end
8792

8893
defp generate_ticks(nil, data, number_of_ticks) do
94+
data
95+
|> generate_limit()
96+
|> generate_ticks(data, number_of_ticks)
97+
end
98+
99+
defp generate_ticks({lower_limit, upper_limit} = lim, _data, number_of_ticks) do
100+
{lower_limit |> Nx.linspace(upper_limit, n: number_of_ticks) |> Nx.to_list(), lim}
101+
end
102+
103+
defp generate_limit(data) do
89104
{min, upper_limit} = Enum.min_max(data)
90105

91106
lower_limit =
@@ -95,11 +110,7 @@ defmodule Matplotex.Figure.Lead do
95110
0
96111
end
97112

98-
generate_ticks({lower_limit, upper_limit}, data, number_of_ticks)
99-
end
100-
101-
defp generate_ticks({lower_limit, upper_limit} = lim, _data, number_of_ticks) do
102-
{lower_limit |> Nx.linspace(upper_limit, n: number_of_ticks) |> Nx.to_list(), lim}
113+
{lower_limit, upper_limit}
103114
end
104115

105116
defp set_region_xy(
@@ -124,6 +135,7 @@ defmodule Matplotex.Figure.Lead do
124135
) do
125136
space_for_ylabel = height_required_for_text(y_label_font, y_label)
126137
y_tick = Enum.max_by(y_ticks, &tick_length(&1))
138+
127139
space_for_yticks = length_required_for_text(y_tick_font, y_tick)
128140

129141
space_required_for_region_y =
@@ -352,8 +364,11 @@ defmodule Matplotex.Figure.Lead do
352364
rotation: 0
353365
},
354366
text
355-
),
356-
do: tick_length(text) * to_number(font_size) * (pt_to_inch_ratio / 2) + flate
367+
) do
368+
text_size = tick_length(text) * to_number(font_size) * (pt_to_inch_ratio / 2)
369+
offset_for_text_length = 1 / tick_length(text) * (pt_to_inch_ratio / 2) * to_number(font_size)
370+
text_size + offset_for_text_length + flate
371+
end
357372

358373
def length_required_for_text(
359374
%Font{

lib/matplotex/figure/rc_params.ex

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ defmodule Matplotex.Figure.RcParams do
128128
y_label_font
129129
end
130130

131-
def update_with_font(rc_params, params) do
131+
def update_with_font(rc_params, params) when is_map(params) do
132132
rc_params
133133
|> update_font(params, :x_label)
134134
|> update_font(params, :y_label)
@@ -137,6 +137,11 @@ defmodule Matplotex.Figure.RcParams do
137137
|> update_font(params, :title)
138138
end
139139

140+
def update_with_font(rc_params, params) do
141+
params = Enum.into(params, %{})
142+
update_with_font(rc_params, params)
143+
end
144+
140145
defp update_font(
141146
rc_params,
142147
params,

lib/matplotex/figure/two_d.ex

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
11
defmodule Matplotex.Figure.TwoD do
22
defstruct [:x, :y]
3+
4+
def update(twod, opts, context) do
5+
%__MODULE__{
6+
twod
7+
| x: fetch_from_opts(opts, :x, context),
8+
y: fetch_from_opts(opts, :y, context)
9+
}
10+
end
11+
12+
defp fetch_from_opts(opts, key, context) do
13+
Map.get(opts, :"#{key}_#{context}")
14+
end
315
end

0 commit comments

Comments
 (0)