diff --git a/lib/matplotex.ex b/lib/matplotex.ex index 0c7150d..f1ed65d 100644 --- a/lib/matplotex.ex +++ b/lib/matplotex.ex @@ -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. diff --git a/lib/matplotex/element/slice.ex b/lib/matplotex/element/slice.ex index e4626c1..2f504e4 100644 --- a/lib/matplotex/element/slice.ex +++ b/lib/matplotex/element/slice.ex @@ -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 diff --git a/lib/matplotex/figure/radial.ex b/lib/matplotex/figure/radial.ex index 0a1ce21..a403113 100644 --- a/lib/matplotex/figure/radial.ex +++ b/lib/matplotex/figure/radial.ex @@ -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() diff --git a/lib/matplotex/figure/radial/pie.ex b/lib/matplotex/figure/radial/pie.ex index 9b6784e..4989a9c 100644 --- a/lib/matplotex/figure/radial/pie.ex +++ b/lib/matplotex/figure/radial/pie.ex @@ -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 @@ -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}, @@ -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} -> @@ -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 @@ -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} = @@ -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 diff --git a/lib/matplotex/figure/rc_params.ex b/lib/matplotex/figure/rc_params.ex index 9bdc987..2e1791b 100644 --- a/lib/matplotex/figure/rc_params.ex +++ b/lib/matplotex/figure/rc_params.ex @@ -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, diff --git a/test/matplotex/figure/radial/pie_test.exs b/test/matplotex/figure/radial/pie_test.exs index c9ffab3..18fbdda 100644 --- a/test/matplotex/figure/radial/pie_test.exs +++ b/test/matplotex/figure/radial/pie_test.exs @@ -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 @@ -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)