Skip to content

Commit 92a3536

Browse files
Merge pull request #21 from BigThinkcode/radial_regional
Regional based processing of radial plots
2 parents e3a01b3 + 835c75e commit 92a3536

16 files changed

Lines changed: 569 additions & 277 deletions

File tree

lib/matplotex/blueprint/chord.ex

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ defmodule Matplotex.Blueprint.Chord do
3030
element: [],
3131
legend_pos: nil,
3232
border: nil,
33-
show_legend: @false_by_default
33+
show_legend: @false_by_default,
34+
region_title: nil,
35+
region_legend: nil,
36+
region_content: nil
3437
]
3538
|> Keyword.merge(opts)
3639
)

lib/matplotex/element/rad_legend.ex

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,19 @@ defmodule Matplotex.Element.RadLegend do
4141
x: x,
4242
y: y,
4343
width: width,
44-
height: height,
45-
label_margin: label_margin
46-
} = legend
44+
height: height
45+
} = legend, legend_font
4746
) do
47+
4848
%{
4949
legend
5050
| label: %Label{
51-
x: x + width + label_margin,
52-
y: y - height / 4,
51+
x: x + width ,
52+
y: y + height / 2,
5353
text: text,
54-
type: @label_type,
55-
text_anchor: "start"
54+
type: @label_type
5655
}
56+
|> Label.cast_label(legend_font)
5757
}
5858
end
5959
end

lib/matplotex/figure.ex

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ defmodule Matplotex.Figure do
3131
def new(opts) do
3232
struct(__MODULE__, opts)
3333
end
34+
# TODO: put error message in error
35+
# def put_error(figure, opts) do
36+
37+
# end
3438

3539
def add_label(%__MODULE__{axes: %module{} = axes} = figure, label, opts),
3640
do: %{figure | axes: module.add_label(axes, label, opts)}
@@ -110,4 +114,6 @@ defmodule Matplotex.Figure do
110114
defp update_rc_params(_, _) do
111115
raise Matplotex.InputError, keys: [:rc_params], message: "Invalid Input"
112116
end
117+
118+
113119
end

lib/matplotex/figure/areal.ex

Lines changed: 113 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ defmodule Matplotex.Figure.Areal do
3030
alias Matplotex.Figure.Dataset
3131

3232
alias Matplotex.Figure.Text
33+
alias Matplotex.Figure.Areal.Region
34+
alias Matplotex.Figure.RcParams
3335
@default_tick_minimum 0
36+
@zero_to_move 0
3437
def add_label(%__MODULE__{label: nil} = axes, {key, label}, opts) when is_binary(label) do
3538
label =
3639
Map.new()
@@ -143,7 +146,7 @@ defmodule Matplotex.Figure.Areal do
143146

144147
def materialized_by_region(figure) do
145148
figure
146-
|> Lead.set_regions()
149+
|> Lead.set_regions_areal()
147150
|> Cast.cast_xticks_by_region()
148151
|> Cast.cast_yticks_by_region()
149152
|> Cast.cast_hgrids_by_region()
@@ -211,11 +214,119 @@ defmodule Matplotex.Figure.Areal do
211214
def set_frame_size(%__MODULE__{} = axes, frame_size) do
212215
%__MODULE__{axes | size: frame_size}
213216
end
217+
218+
def set_region_title(
219+
%Figure{
220+
axes:
221+
%{
222+
title: title,
223+
region_x: %Region{width: region_x_width},
224+
region_y: %Region{height: region_y_height} = region_y,
225+
region_title: region_title,
226+
size: {_f_width, _f_height},
227+
border: {lx, _by, _, ty}
228+
} = axes,
229+
rc_params: %RcParams{title_font: title_font}
230+
} = figure
231+
) do
232+
space_for_title = Lead.height_required_for_text(title_font, title)
233+
234+
{x_region_title, y_region_title} =
235+
Algebra.transform_given_point(@zero_to_move, -space_for_title, lx, ty)
236+
237+
%Figure{
238+
figure
239+
| axes: %{
240+
axes
241+
| region_title: %Region{
242+
region_title
243+
| x: x_region_title,
244+
y: y_region_title + space_for_title,
245+
width: region_x_width,
246+
height: space_for_title
247+
},
248+
region_y: %Region{
249+
region_y
250+
| height: region_y_height - space_for_title
251+
}
252+
}
253+
}
254+
end
255+
256+
def set_region_title(figure), do: figure
257+
258+
259+
def set_region_legend(
260+
%Figure{
261+
axes:
262+
%{
263+
show_legend: true,
264+
region_x: %Region{width: region_x_width} = region_x,
265+
region_title: %Region{height: region_title_height},
266+
region_legend: region_legend,
267+
size: {f_width, _f_height},
268+
border: {_lx, by, rx, ty}
269+
} = axes,
270+
rc_params: %RcParams{legend_width: legend_width}
271+
} = figure
272+
) do
273+
region_legend_width = f_width * legend_width
274+
region_x_width_after_legend = region_x_width - region_legend_width
275+
276+
{x_region_legend, y_region_legend} =
277+
Algebra.transform_given_point(-region_legend_width, -region_title_height, rx, ty, 0)
278+
279+
%Figure{
280+
figure
281+
| axes: %{
282+
axes
283+
| region_x: %Region{
284+
region_x
285+
| width: region_x_width_after_legend
286+
},
287+
region_legend: %Region{
288+
region_legend
289+
| x: x_region_legend,
290+
y: y_region_legend,
291+
width: region_legend_width,
292+
height: y_region_legend - by
293+
}
294+
}
295+
}
296+
end
297+
def set_region_legend(figure), do: figure
298+
def set_region_content(
299+
%Figure{
300+
axes:
301+
%{
302+
region_x: %Region{x: x_region_x, width: region_x_width},
303+
region_y: %Region{y: y_region_y, height: region_y_height},
304+
region_content: region_content
305+
} = axes
306+
} = figure
307+
) do
308+
%Figure{
309+
figure
310+
| axes: %{
311+
axes
312+
| region_content: %Region{
313+
region_content
314+
| x: x_region_x,
315+
y: y_region_y,
316+
width: region_x_width,
317+
height: region_y_height
318+
}
319+
}
320+
}
321+
end
322+
def set_region_content(figure), do: figure
214323
end
215324
end
325+
216326
def transformation({_labelx, x}, {_labely, y}, xminmax, yminmax, width, height, transition) do
217327
transformation(x, y, xminmax, yminmax, width, height, transition)
218328
end
329+
219330
def transformation({_label, x}, y, xminmax, yminmax, width, height, transition) do
220331
transformation(x, y, xminmax, yminmax, width, height, transition)
221332
end
@@ -224,7 +335,6 @@ defmodule Matplotex.Figure.Areal do
224335
transformation(x, y, xminmax, yminmax, width, height, transition)
225336
end
226337

227-
228338
def transformation(
229339
x,
230340
y,
@@ -246,11 +356,11 @@ defmodule Matplotex.Figure.Areal do
246356
x
247357
|> Enum.zip(y)
248358
|> Enum.map(fn {x, y} ->
249-
250359
x
251360
|> transformation(y, xlim, ylim, width, height, transition)
252361
|> Algebra.flip_y_coordinate()
253362
end)
363+
254364
%Dataset{dataset | transformed: transformed}
255365
end
256366
end

lib/matplotex/figure/areal/bar_chart.ex

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,12 @@ defmodule Matplotex.Figure.Areal.BarChart do
3535
dataset = Dataset.cast(%Dataset{x: x, y: values, pos: pos, width: width}, opts)
3636
datasets = data ++ [dataset]
3737
xydata = flatten_for_data(datasets)
38-
%Figure{figure | rc_params: %RcParams{white_space: width, y_padding: 0}, axes: %{axes | data: xydata, dataset: datasets}}
38+
39+
%Figure{
40+
figure
41+
| rc_params: %RcParams{white_space: width, y_padding: 0},
42+
axes: %{axes | data: xydata, dataset: datasets}
43+
}
3944
end
4045

4146
@impl Areal
@@ -146,7 +151,7 @@ defmodule Matplotex.Figure.Areal.BarChart do
146151

147152
defp hypox(y) do
148153
nof_x = length(y)
149-
@xmin_value|>Nx.linspace(nof_x, n: nof_x)|>Nx.to_list()
154+
@xmin_value |> Nx.linspace(nof_x, n: nof_x) |> Nx.to_list()
150155
end
151156

152157
defp bar_position(x, pos_factor) when pos_factor < 0 do

lib/matplotex/figure/areal/region.ex

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
defmodule Matplotex.Figure.Areal.Region do
22
alias Matplotex.Figure.Areal.XyRegion.Coords
33
@zero_by_default 0
4-
defstruct x: @zero_by_default, y: @zero_by_default, width: @zero_by_default, height: @zero_by_default, name: nil, theta: @zero_by_default, coords: nil
4+
defstruct x: @zero_by_default,
5+
y: @zero_by_default,
6+
width: @zero_by_default,
7+
height: @zero_by_default,
8+
name: nil,
9+
theta: @zero_by_default,
10+
coords: nil
511

612
def get_label_coords(%__MODULE__{x: x, y: y, coords: %Coords{label: {label_x, label_y}}}) do
713
{x + label_x, y + label_y}

lib/matplotex/figure/cast.ex

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@ defmodule Matplotex.Figure.Cast do
138138
rc_params: %RcParams{line_width: line_width}
139139
} = figure
140140
) do
141+
{lx, by} = Algebra.flip_y_coordinate({lx, by})
142+
{rx, ty} = Algebra.flip_y_coordinate({rx, ty})
141143
left = %Line{x1: lx, y1: by, x2: lx, y2: ty, type: "border.left", stroke_width: line_width}
142144
right = %Line{x1: rx, y1: by, x2: rx, y2: ty, type: "border.right", stroke_width: line_width}
143145
top = %Line{x1: lx, x2: rx, y1: ty, y2: ty, type: "border.top", stroke_width: line_width}
@@ -158,14 +160,14 @@ defmodule Matplotex.Figure.Cast do
158160
%Figure{
159161
axes:
160162
%{
161-
coords: %Coords{title: title_coord} = coords,
163+
region_title: region_title,
162164
title: title,
163165
element: elements
164166
} = axes,
165167
rc_params: %RcParams{title_font: title_font}
166168
} = figure
167169
) do
168-
{ttx, tty} = calculate_center(coords, title_coord, :x)
170+
{ttx, tty} = calculate_center(region_title, :x)
169171

170172
title =
171173
%Label{
@@ -189,14 +191,15 @@ defmodule Matplotex.Figure.Cast do
189191
%Figure{
190192
axes:
191193
%{
192-
region_title: region,
194+
region_title: region_title,
193195
title: title,
194196
element: elements
195197
} = axes,
196198
rc_params: %RcParams{title_font: title_font, label_padding: title_padding}
197199
} = figure
198200
) do
199-
{title_x, title_y} = region |> calculate_center(:x) |> Algebra.flip_y_coordinate()
201+
202+
{title_x, title_y} = region_title |> calculate_center(:x) |> Algebra.flip_y_coordinate()
200203

201204
title =
202205
%Label{
@@ -430,7 +433,6 @@ defmodule Matplotex.Figure.Cast do
430433
} = figure
431434
)
432435
when is_list(x_ticks) do
433-
434436
x_ticks = confine_ticks(x_ticks, xlim)
435437
x_data = confine_data(x_data, xlim)
436438
dataset = confine_data(dataset, xlim, :x)
@@ -448,12 +450,12 @@ defmodule Matplotex.Figure.Cast do
448450
Enum.map(ticks_width_position, fn {tick_position, label} ->
449451
{_, y_x_tick_line} =
450452
@zero_to_move
451-
|>Algebra.transform_given_point(height_region_x, x_region_x, y_region_x)
453+
|> Algebra.transform_given_point(height_region_x, x_region_x, y_region_x)
452454
|> Algebra.flip_y_coordinate()
453455

454456
{x_x_tick, y_x_tick} =
455457
tick_position
456-
|>Algebra.transform_given_point(
458+
|> Algebra.transform_given_point(
457459
@zero_to_move,
458460
x_region_x_with_padding,
459461
y_x_tick
@@ -480,6 +482,7 @@ defmodule Matplotex.Figure.Cast do
480482
{%Tick{type: @xtick_type, tick_line: line, label: label}, {x_x_tick, y_x_tick_line}}
481483
end)
482484
|> Enum.unzip()
485+
483486
elements = elements ++ x_tick_elements
484487

485488
%Figure{
@@ -498,6 +501,7 @@ defmodule Matplotex.Figure.Cast do
498501
defp format_tick_label({label, _index}), do: label
499502
defp format_tick_label(label) when is_float(label), do: Float.round(label, 2)
500503
defp format_tick_label(label), do: label
504+
501505
defp content_linespace(number_of_ticks_required, axis_size) do
502506
@lowest_tick |> Nx.linspace(axis_size, n: number_of_ticks_required) |> Nx.to_list()
503507
end
@@ -630,7 +634,7 @@ defmodule Matplotex.Figure.Cast do
630634

631635
{x_y_tick, y_y_tick} =
632636
@zero_to_move
633-
|>Algebra.transform_given_point(
637+
|> Algebra.transform_given_point(
634638
tick_position,
635639
x_y_tick,
636640
y_region_y_with_padding

0 commit comments

Comments
 (0)