Skip to content

Commit cc390fe

Browse files
Merge pull request #41 from BigThinkcode/label_pie
Label on pie
2 parents 87a8643 + 3999000 commit cc390fe

6 files changed

Lines changed: 26 additions & 9 deletions

File tree

lib/matplotex.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ defmodule Matplotex do
155155
156156
* Custom Elements: The materializer function's primary role is to generate a list of structs that implement the Matplotex.Element behavior. This enables the SVG generator to produce the appropriate tags for custom visualizations
157157
158-
### Matplotex.Element behaviour
158+
## Matplotex.Element behaviour
159159
160160
The Matplotex.Element behavior defines a callback function, assemble/1, which takes a struct as input and returns its corresponding SVG tag as a string. The assemble/1 function is responsible for interpolating the struct's values into the SVG element.
161161

lib/matplotex/element/slice.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ defmodule Matplotex.Element.Slice do
2828
A #{get_radius(slice)} #{get_radius(slice)} 0 0 1 #{get_x2(slice)} #{get_y2(slice)}
2929
L #{get_cx(slice)} #{get_cy(slice)}
3030
Z" fill="#{slice.color}" />
31+
#{Element.assemble(slice.label)}
3132
"""
3233
end
3334

lib/matplotex/figure/radial.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ defmodule Matplotex.Figure.Radial do
3030
%{axes | title: title, show_title: true}
3131
end
3232

33-
def materialized(figure) do
33+
def materialized_by_region(figure) do
3434
figure
3535
|> Lead.set_regions_radial()
3636
|> Cast.cast_border()

lib/matplotex/figure/radial/pie.ex

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
defmodule Matplotex.Figure.Radial.Pie do
22
@moduledoc false
3+
alias Matplotex.Element.Label
34
alias Matplotex.Figure.RcParams
45
alias Matplotex.Utils.Algebra
56
alias Matplotex.Figure.Areal.Region
@@ -42,14 +43,13 @@ defmodule Matplotex.Figure.Radial.Pie do
4243
@impl Radial
4344
def materialize(figure) do
4445
figure
45-
|> __MODULE__.materialized()
4646
|> materialize_slices()
4747
end
4848

4949
defp materialize_slices(
5050
%Figure{
5151
figsize: {fwidth, fheight},
52-
rc_params: %RcParams{legend_font: legend_font},
52+
rc_params: %RcParams{legend_font: legend_font, slice_label_font: slice_label_font},
5353
axes:
5454
%__MODULE__{
5555
size: {_width, height},
@@ -88,7 +88,7 @@ defmodule Matplotex.Figure.Radial.Pie do
8888
}
8989
},
9090
fn raw, color, acc ->
91-
roll_across(raw, color, acc, center, radius, total_size, legend_font)
91+
roll_across(raw, color, acc, center, radius, total_size, legend_font, slice_label_font)
9292
end
9393
)
9494
|> then(fn %Accumulator{slices: slices, legends: legends} ->
@@ -123,7 +123,8 @@ defmodule Matplotex.Figure.Radial.Pie do
123123
%{x: cx, y: cy} = center,
124124
radius,
125125
total_size,
126-
legend_font
126+
legend_font,
127+
slice_label_font
127128
) do
128129
percentage = size / total_size
129130
angle_for_size = percentage * @full_circle + start_angle
@@ -140,7 +141,8 @@ defmodule Matplotex.Figure.Radial.Pie do
140141
percentage: percentage,
141142
color: color,
142143
cx: cx,
143-
cy: cy
144+
cy: cy,
145+
label: with_label(x1, y1, x2, y2, cx, cy, label, slice_label_font)
144146
}
145147

146148
{x_legend, y_legend} =
@@ -166,4 +168,17 @@ defmodule Matplotex.Figure.Radial.Pie do
166168
legend_acc: %LegendAcc{legend_acc | y: y_legend}
167169
}
168170
end
171+
172+
defp with_label(x1, y1, x2, y2, cx, cy, label, label_font) do
173+
label_x = (x1 + x2 + cx) / 3
174+
label_y = (y1 + y2 + cy) / 3
175+
176+
%Label{
177+
type: "pie.slice",
178+
text: label,
179+
x: label_x,
180+
y: label_y
181+
}
182+
|> Label.cast_label(label_font)
183+
end
169184
end

lib/matplotex/figure/rc_params.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ defmodule Matplotex.Figure.RcParams do
2626
y_label_font: @font,
2727
title_font: %Font{font_size: @default_title_font_size, dominant_baseline: "auto"},
2828
legend_font: %Font{text_anchor: "start"},
29+
slice_label_font: @font,
2930
figure_size: @default_figsize,
3031
figure_dpi: @default_dpi,
3132
line_width: @line_width,

test/matplotex/figure/radial/pie_test.exs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ defmodule Matplotex.Figure.Radial.PieTest do
2828
# Colors for the slices
2929
colors = ["lightblue", "lightgreen", "orange", "pink"]
3030
figure = Pie.create(%Figure{axes: %Pie{}}, sizes, labels: labels, colors: colors)
31-
assert %Figure{axes: %{element: element}} = Pie.materialize(figure)
31+
assert %Figure{axes: %{element: element}} = figure |> Figure.materialize()
3232

3333
assert Enum.filter(element, fn elem -> String.contains?(elem.type, "border") end)
3434
|> length() == 4
@@ -42,7 +42,7 @@ defmodule Matplotex.Figure.Radial.PieTest do
4242
# Colors for the slices
4343
colors = ["lightblue", "lightgreen", "orange", "pink"]
4444
figure = Pie.create(%Figure{axes: %Pie{}}, sizes, labels: labels, colors: colors)
45-
assert %Figure{axes: %{element: element}} = Pie.materialize(figure)
45+
assert %Figure{axes: %{element: element}} = figure|> Figure.materialize()
4646

4747
assert Enum.filter(element, fn elem -> elem.type == "pie.slice" end) |> length() ==
4848
length(sizes)

0 commit comments

Comments
 (0)