Skip to content

Commit 005d8bd

Browse files
author
Mohammed Sadique
committed
regional pie
1 parent e32f301 commit 005d8bd

10 files changed

Lines changed: 84 additions & 57 deletions

File tree

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/cast.ex

Lines changed: 7 additions & 4 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{

lib/matplotex/figure/lead.ex

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ defmodule Matplotex.Figure.Lead do
3333
|> module.set_region_title()
3434
|> module.set_region_legend()
3535
|> module.set_region_content()
36+
|> focus_to_origin()
3637
end
3738

3839
def set_regions_radial(figure), do: figure
@@ -195,11 +196,12 @@ defmodule Matplotex.Figure.Lead do
195196
}
196197
}
197198
end
199+
def focus_to_origin(figure), do: figure
198200
defp plotable_radius(width, height, padding) when height < width do
199-
(height - height * padding) / 2
201+
(height - height * padding * 2) / 2
200202
end
201203
defp plotable_radius(width, height, padding) when width < height do
202-
(width - width * padding) / 2
204+
(width - width * padding * 2) / 2
203205
end
204206
# def focus_to_origin(
205207
# %Figure{
@@ -242,32 +244,32 @@ defmodule Matplotex.Figure.Lead do
242244
# }
243245
# end
244246

245-
defp center_and_radius(width, height, {leftx, _rightx, bottomy, _topy}) when height < width do
246-
radius = height / 2
247+
# defp center_and_radius(width, height, {leftx, _rightx, bottomy, _topy}) when height < width do
248+
# radius = height / 2
247249

248-
centerx = leftx + radius
249-
centery = bottomy + radius
250-
{{centerx, centery}, radius}
251-
end
250+
# centerx = leftx + radius
251+
# centery = bottomy + radius
252+
# {{centerx, centery}, radius}
253+
# end
252254

253-
defp center_and_radius(width, _height, {leftx, _rightx, _bottomy, topy}) do
254-
radius = width / 2
255+
# defp center_and_radius(width, _height, {leftx, _rightx, _bottomy, topy}) do
256+
# radius = width / 2
255257

256-
centerx = leftx + radius
257-
centery = topy - radius
258-
{{centerx, centery}, radius}
259-
end
258+
# centerx = leftx + radius
259+
# centery = topy - radius
260+
# {{centerx, centery}, radius}
261+
# end
260262

261-
defp label_offset(nil, _font_size), do: 0
262-
defp label_offset("", _font_size), do: 0
263+
# defp label_offset(nil, _font_size), do: 0
264+
# defp label_offset("", _font_size), do: 0
263265

264-
defp label_offset(ticks, font_size) when is_list(ticks) do
265-
font_size * @pt_to_inch + @padding
266-
end
266+
# defp label_offset(ticks, font_size) when is_list(ticks) do
267+
# font_size * @pt_to_inch + @padding
268+
# end
267269

268-
defp label_offset(_label, font_size) do
269-
font_size * @pt_to_inch + @padding
270-
end
270+
# defp label_offset(_label, font_size) do
271+
# font_size * @pt_to_inch + @padding
272+
# end
271273

272274
defp tick_length(tick) when is_integer(tick) do
273275
tick |> Integer.to_string() |> String.length()

lib/matplotex/figure/radial.ex

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
defmodule Matplotex.Figure.Radial do
2+
alias Matplotex.Utils.Algebra
3+
24
@callback create(struct(), list(), keyword()) :: struct()
35
@callback materialize(struct()) :: struct()
46

@@ -31,7 +33,7 @@ defmodule Matplotex.Figure.Radial do
3133
figure
3234
|> Lead.set_regions_radial()
3335
|> Cast.cast_border()
34-
|> Cast.cast_title()
36+
|> Cast.cast_title_by_region()
3537
end
3638

3739
def set_region_title(
@@ -72,17 +74,18 @@ defmodule Matplotex.Figure.Radial do
7274
rc_params: %RcParams{legend_width: legend_width},
7375
axes:
7476
%{
75-
border: {_lx, by, rx, _ty},
77+
border: {_lx, by, rx, ty},
7678
show_legend: true,
77-
region_title: %Region{y: y_region_title}
79+
region_title: %Region{y: y_region_title, height: height_region_title}
7880
} = axes
7981
} = figure
8082
) do
83+
IO.inspect(legend_width)
8184
width_region_legend = fwidth * legend_width
8285
height_region_legend = abs(by - y_region_title)
8386

8487
{x_region_legend, y_region_legend} =
85-
Algebra.transform_given_point(rx - width_region_legend, 0, 0, by)
88+
Algebra.transform_given_point(rx, abs(ty), -width_region_legend,height_region_title)|>Algebra.flip_y_coordinate()
8689

8790
%Figure{
8891
figure
@@ -110,6 +113,7 @@ defmodule Matplotex.Figure.Radial do
110113
} = axes
111114
} = figure
112115
) do
116+
113117
width_region_content = width - width_region_legend
114118
height_region_content = height - height_region_title
115119

lib/matplotex/figure/radial/pie.ex

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
defmodule Matplotex.Figure.Radial.Pie do
2+
3+
alias Matplotex.Figure.RcParams
4+
alias Matplotex.Utils.Algebra
25
alias Matplotex.Figure.Areal.Region
36
alias Matplotex.Figure.Radial
47
alias Matplotex.Element.RadLegend
@@ -24,7 +27,11 @@ defmodule Matplotex.Figure.Radial.Pie do
2427

2528
@impl Radial
2629
def create(%Figure{axes: axes} = figure, sizes, opts) do
27-
dataset = Dataset.cast(%Dataset{sizes: sizes}, opts)
30+
dataset = if sizes|>Enum.sum()|>abs() > 0 do
31+
Dataset.cast(%Dataset{sizes: sizes}, opts)
32+
else
33+
raise Matplotex.InputError, "Invalid set of values for a pie chart, sum of sizes should be greater than 0"
34+
end
2835
%Figure{figure | axes: %{axes | dataset: dataset}}
2936
end
3037

@@ -37,13 +44,14 @@ defmodule Matplotex.Figure.Radial.Pie do
3744

3845
defp materialize_slices(
3946
%Figure{
40-
figsize: {_fwidth, fheight},
47+
figsize: {fwidth, fheight},
48+
rc_params: %RcParams{legend_font: legend_font},
4149
axes:
4250
%__MODULE__{
4351
size: {_width, height},
4452
radius: radius,
45-
center: %{y: cy} = center,
46-
legend_pos: {legx, legy},
53+
center: center,
54+
region_legend: region_legend,
4755
dataset: %Dataset{
4856
sizes: sizes,
4957
labels: labels,
@@ -53,11 +61,11 @@ defmodule Matplotex.Figure.Radial.Pie do
5361
element: elements
5462
} = axes
5563
} = figure
56-
) do
64+
) when fwidth > 0 and fheight > 0 do
65+
%Region{x: legx, y: legy} = Algebra.flip_y_coordinate(region_legend)
5766
total_size = Enum.sum(sizes)
5867
legend_rect_side = height / length(sizes) / 2
59-
center = %{center | y: fheight - cy}
60-
68+
center = Algebra.flip_y_coordinate(center)
6169
slices =
6270
sizes
6371
|> Enum.zip(labels)
@@ -74,7 +82,7 @@ defmodule Matplotex.Figure.Radial.Pie do
7482
}
7583
},
7684
fn raw, color, acc ->
77-
roll_across(raw, color, acc, center, radius, total_size)
85+
roll_across(raw, color, acc, center, radius, total_size, legend_font)
7886
end
7987
)
8088
|> then(fn %Accumulator{slices: slices, legends: legends} ->
@@ -108,7 +116,8 @@ defmodule Matplotex.Figure.Radial.Pie do
108116
},
109117
%{x: cx, y: cy} = center,
110118
radius,
111-
total_size
119+
total_size,
120+
legend_font
112121
) do
113122
percentage = size / total_size
114123
angle_for_size = percentage * @full_circle + start_angle
@@ -128,7 +137,7 @@ defmodule Matplotex.Figure.Radial.Pie do
128137
cy: cy
129138
}
130139

131-
y_legend = y_legend - legend_unit_height
140+
{x_legend, y_legend} = Algebra.transform_given_point(0, legend_unit_height, x_legend, y_legend)
132141

133142
legend =
134143
%RadLegend{
@@ -140,7 +149,7 @@ defmodule Matplotex.Figure.Radial.Pie do
140149
height: legend_unit_height,
141150
label: "#{label}-#{Float.ceil(percentage * 100, 2)}%"
142151
}
143-
|> RadLegend.with_label()
152+
|> RadLegend.with_label(legend_font)
144153

145154
%Accumulator{
146155
lead: {x2, y2},

lib/matplotex/figure/rc_params.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ defmodule Matplotex.Figure.RcParams do
2323
y_tick_font: %Font{text_anchor: "start"},
2424
x_label_font: @font,
2525
y_label_font: @font,
26-
title_font: %Font{font_size: @default_title_font_size},
27-
legend_font: @font,
26+
title_font: %Font{font_size: @default_title_font_size, dominant_baseline: "auto"},
27+
legend_font: %Font{text_anchor: "start"},
2828
figure_size: @default_figsize,
2929
figure_dpi: @default_dpi,
3030
line_width: @line_width,

lib/matplotex/helpers.ex

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,8 @@ defmodule Matplotex.Helpers do
270270
sizes
271271
|> Matplotex.pie(colors: colors, labels: labels)
272272
|> Matplotex.set_title("Pie chart")
273-
|> Matplotex.figure(%{figsize: {3.5, 2.5}, margin: 0.05})
273+
|> Matplotex.figure(%{figsize: {4, 3}, margin: 0.05})
274+
|> Matplotex.set_rc_params(%{line_width: 1, legend_width: 0.25})
274275
|> Matplotex.show()
275276
|> copy()
276277
end
@@ -305,7 +306,7 @@ defmodule Matplotex.Helpers do
305306
values
306307
|> Matplotex.pie(colors: colors, labels: categories)
307308
|> Matplotex.set_title("Asias Emission distribution(2008-2011)")
308-
|> Matplotex.set_rc_params(%{line_width: 1})
309+
|> Matplotex.set_rc_params(%{line_width: 1, legend_width: 0.5})
309310
|> Matplotex.figure(%{figsize: {10, 4}, margin: 0.15})
310311
|> Matplotex.show()
311312
|> copy()

lib/matplotex/utils/algebra.ex

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
defmodule Matplotex.Utils.Algebra do
2+
alias Matplotex.Figure.TwoD
23
alias Nx
34

45
@tensor_data_type_bits 64
@@ -90,4 +91,8 @@ defmodule Matplotex.Utils.Algebra do
9091
def flip_y_coordinate({x, y}) do
9192
{x, -y}
9293
end
94+
95+
def flip_y_coordinate(%{y: y} = point) do
96+
%{point | y: -y}
97+
end
9398
end

test/matplotex/figure/lead_test.exs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -242,12 +242,9 @@ defmodule Matplotex.Figure.LeadTest do
242242
assert 2 * lx + width_region_content + width_region_legend == fwidth
243243
assert 2 * abs(ty) + height_region_content + height_region_title == fheight
244244
end
245-
end
246-
247-
describe "focus_to_origin/1" do
248-
@tab radial: true
245+
@tag radial: true
249246
test "sets origin to center of the figure", %{figure: figure} do
250-
assert %Figure{axes: %{center: {cx, cy}}} = Lead.focus_to_origin(figure)
247+
assert %Figure{axes: %{center: %TwoD{x: cx, y: cy} }} = Lead.set_regions_radial(figure)
251248
assert cx != 0 && cy != 0
252249
end
253250
end

0 commit comments

Comments
 (0)