|
| 1 | +defmodule Matplotex.Figure.Areal.Histogram do |
| 2 | + alias Matplotex.Element.Rect |
| 3 | + alias Matplotex.Figure.RcParams |
| 4 | + alias Matplotex.Figure.Areal.PlotOptions |
| 5 | + alias Matplotex.Figure.Dataset |
| 6 | + alias Matplotex.Figure.Areal.Region |
| 7 | + alias Matplotex.Figure |
| 8 | + alias Matplotex.Figure.Areal |
| 9 | + use Areal |
| 10 | + |
| 11 | + @make_it_zero 0 |
| 12 | + |
| 13 | + frame( |
| 14 | + tick: %TwoD{}, |
| 15 | + limit: %TwoD{}, |
| 16 | + label: %TwoD{}, |
| 17 | + scale: %TwoD{}, |
| 18 | + region_x: %Region{}, |
| 19 | + region_y: %Region{}, |
| 20 | + region_title: %Region{}, |
| 21 | + region_legend: %Region{}, |
| 22 | + region_content: %Region{} |
| 23 | + ) |
| 24 | + |
| 25 | + @impl Areal |
| 26 | + def create(%Figure{axes: %__MODULE__{} = axes, rc_params: rc_params} = figure, {data, bins}, opts) do |
| 27 | + {x, y} = bins_and_hists(data, bins) |
| 28 | + |
| 29 | + dataset = Dataset.cast(%Dataset{x: x, y: y}, opts) |
| 30 | + %Figure{figure | axes: %__MODULE__{axes | data: {x, y}, dataset: [dataset]}, rc_params: %RcParams{rc_params | y_padding: @make_it_zero}} |
| 31 | + |> PlotOptions.set_options_in_figure(opts) |
| 32 | + end |
| 33 | + |
| 34 | + @impl Areal |
| 35 | + def materialize(figure) do |
| 36 | + figure |
| 37 | + |> sanitize() |
| 38 | + |> __MODULE__.materialized_by_region() |
| 39 | + |> materialize_hist() |
| 40 | + end |
| 41 | + |
| 42 | + defp materialize_hist(%Figure{axes: %{dataset: data,limit: %TwoD{x: x_lim, y: y_lim}, region_content: %Region{x: x_region_content, y: y_region_content, width: width_region_content, height: height_region_content}, element: element}, rc_params: %RcParams{x_padding: x_padding, white_space: white_space}}) do |
| 43 | + x_padding_value = width_region_content * x_padding + white_space |
| 44 | + shrinked_width_region_content = width_region_content - x_padding_value * 2 |
| 45 | + |
| 46 | + hist_elements = |
| 47 | + data |
| 48 | + |>Enum.map(fn dataset -> |
| 49 | + dataset |
| 50 | + |> do_transform(x_lim, y_lim, shrinked_width_region_content, height_region_content, {x_region_content + x_padding_value, y_region_content}) |
| 51 | + |> capture(abs(y_region_content), shrinked_width_region_content) |
| 52 | + end) |
| 53 | + |>List.flatten() |
| 54 | + %Figure{axes: %{element: element ++ hist_elements}} |
| 55 | + end |
| 56 | + |
| 57 | + defp capture(%Dataset{transformed: transformed} = dataset, bly, region_width) do |
| 58 | + capture(transformed, [], dataset, bly, region_width) |
| 59 | + end |
| 60 | + |
| 61 | + defp capture( |
| 62 | + [{x, y} | to_capture], |
| 63 | + captured, |
| 64 | + %Dataset{ |
| 65 | + color: color, |
| 66 | + x: bins, |
| 67 | + pos: pos_factor, |
| 68 | + edge_color: edge_color, |
| 69 | + alpha: alpha |
| 70 | + } = dataset, |
| 71 | + bly, |
| 72 | + region_width |
| 73 | + ) do |
| 74 | + capture( |
| 75 | + to_capture, |
| 76 | + captured ++ |
| 77 | + [ |
| 78 | + %Rect{ |
| 79 | + type: "figure.histogram", |
| 80 | + x: bin_position(x, pos_factor), |
| 81 | + y: y, |
| 82 | + width: region_width / length(bins), |
| 83 | + height: bly - y, |
| 84 | + color: color, |
| 85 | + stroke: edge_color, |
| 86 | + fill_opacity: alpha, |
| 87 | + stroke_opacity: alpha |
| 88 | + } |
| 89 | + ], |
| 90 | + dataset, |
| 91 | + bly, |
| 92 | + region_width |
| 93 | + ) |
| 94 | + end |
| 95 | + |
| 96 | + defp capture([], captured, _dataset, _bly, _region_width), do: captured |
| 97 | + |
| 98 | + |
| 99 | + defp bin_position(x, pos_factor) when pos_factor < 0 do |
| 100 | + x + pos_factor |
| 101 | + end |
| 102 | + |
| 103 | + defp bin_position(x, _pos_factor), do: x |
| 104 | + defp bins_and_hists(data, bins) do |
| 105 | + {data_min, data_max} = Enum.min_max(data) |
| 106 | + |
| 107 | + bins_dist = |
| 108 | + data_min |
| 109 | + |> Nx.linspace(data_max, n: bins) |
| 110 | + |> Nx.to_list() |
| 111 | + |
| 112 | + {hists, _} = |
| 113 | + Enum.map_reduce(bins_dist, data_min, fn bin, previous_bin -> |
| 114 | + frequency = Enum.frequencies_by(data, fn point -> point < bin && point > previous_bin end) |
| 115 | + {Map.get(frequency, true, 0), bin} |
| 116 | + end) |
| 117 | + |
| 118 | + {bins_dist, hists} |
| 119 | + end |
| 120 | + |
| 121 | + defp sanitize(%Figure{axes: %__MODULE__{data: {x, y}}= axes} = figure) do |
| 122 | + {ymin, ymax} = Enum.min_max(y) |
| 123 | + {xmin, xmax} = Enum.min_max(x) |
| 124 | + |
| 125 | + %Figure{figure | axes: %__MODULE__{axes | limit: %TwoD{x: {floor(xmin), ceil(xmax)},y: {floor(ymin), ceil(ymax)}}}} |
| 126 | + end |
| 127 | +end |
0 commit comments