Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/matplotex.ex
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ defmodule Matplotex do

* 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

### Matplotex.Element behaviour
## Matplotex.Element behaviour

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.

Expand Down
1 change: 1 addition & 0 deletions lib/matplotex/element/slice.ex
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ defmodule Matplotex.Element.Slice do
A #{get_radius(slice)} #{get_radius(slice)} 0 0 1 #{get_x2(slice)} #{get_y2(slice)}
L #{get_cx(slice)} #{get_cy(slice)}
Z" fill="#{slice.color}" />
#{Element.assemble(slice.label)}
"""
end

Expand Down
2 changes: 1 addition & 1 deletion lib/matplotex/figure/radial.ex
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ defmodule Matplotex.Figure.Radial do
%{axes | title: title, show_title: true}
end

def materialized(figure) do
def materialized_by_region(figure) do
figure
|> Lead.set_regions_radial()
|> Cast.cast_border()
Expand Down
25 changes: 20 additions & 5 deletions lib/matplotex/figure/radial/pie.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
defmodule Matplotex.Figure.Radial.Pie do
@moduledoc false
alias Matplotex.Element.Label
alias Matplotex.Figure.RcParams
alias Matplotex.Utils.Algebra
alias Matplotex.Figure.Areal.Region
Expand Down Expand Up @@ -42,14 +43,13 @@ defmodule Matplotex.Figure.Radial.Pie do
@impl Radial
def materialize(figure) do
figure
|> __MODULE__.materialized()
|> materialize_slices()
end

defp materialize_slices(
%Figure{
figsize: {fwidth, fheight},
rc_params: %RcParams{legend_font: legend_font},
rc_params: %RcParams{legend_font: legend_font, slice_label_font: slice_label_font},
axes:
%__MODULE__{
size: {_width, height},
Expand Down Expand Up @@ -88,7 +88,7 @@ defmodule Matplotex.Figure.Radial.Pie do
}
},
fn raw, color, acc ->
roll_across(raw, color, acc, center, radius, total_size, legend_font)
roll_across(raw, color, acc, center, radius, total_size, legend_font, slice_label_font)
end
)
|> then(fn %Accumulator{slices: slices, legends: legends} ->
Expand Down Expand Up @@ -123,7 +123,8 @@ defmodule Matplotex.Figure.Radial.Pie do
%{x: cx, y: cy} = center,
radius,
total_size,
legend_font
legend_font,
slice_label_font
) do
percentage = size / total_size
angle_for_size = percentage * @full_circle + start_angle
Expand All @@ -140,7 +141,8 @@ defmodule Matplotex.Figure.Radial.Pie do
percentage: percentage,
color: color,
cx: cx,
cy: cy
cy: cy,
label: with_label(x1, y1, x2, y2, cx, cy, label, slice_label_font)
}

{x_legend, y_legend} =
Expand All @@ -166,4 +168,17 @@ defmodule Matplotex.Figure.Radial.Pie do
legend_acc: %LegendAcc{legend_acc | y: y_legend}
}
end

defp with_label(x1, y1, x2, y2, cx, cy, label, label_font) do
label_x = (x1 + x2 + cx) / 3
label_y = (y1 + y2 + cy) / 3

%Label{
type: "pie.slice",
text: label,
x: label_x,
y: label_y
}
|> Label.cast_label(label_font)
end
end
1 change: 1 addition & 0 deletions lib/matplotex/figure/rc_params.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ defmodule Matplotex.Figure.RcParams do
y_label_font: @font,
title_font: %Font{font_size: @default_title_font_size, dominant_baseline: "auto"},
legend_font: %Font{text_anchor: "start"},
slice_label_font: @font,
figure_size: @default_figsize,
figure_dpi: @default_dpi,
line_width: @line_width,
Expand Down
4 changes: 2 additions & 2 deletions test/matplotex/figure/radial/pie_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ defmodule Matplotex.Figure.Radial.PieTest do
# Colors for the slices
colors = ["lightblue", "lightgreen", "orange", "pink"]
figure = Pie.create(%Figure{axes: %Pie{}}, sizes, labels: labels, colors: colors)
assert %Figure{axes: %{element: element}} = Pie.materialize(figure)
assert %Figure{axes: %{element: element}} = figure |> Figure.materialize()

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

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