Skip to content

Commit 5fc14a0

Browse files
author
Mohammed Sadique
committed
capture stacked
1 parent ee923db commit 5fc14a0

1 file changed

Lines changed: 64 additions & 3 deletions

File tree

lib/matplotex/figure/areal/bar_chart.ex

Lines changed: 64 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
defmodule Matplotex.Figure.Areal.BarChart do
22
@moduledoc false
33
import Matplotex.Figure.Numer
4+
alias Matplotex.Utils.Algebra
45
alias Matplotex.Figure.Areal.PlotOptions
56
alias Matplotex.Figure.Areal.Region
67
alias Matplotex.Element.Legend
@@ -81,7 +82,7 @@ defmodule Matplotex.Figure.Areal.BarChart do
8182
datasets
8283
|> Enum.map(fn dataset ->
8384
dataset
84-
|> do_transform(
85+
|> do_transform_with_bottom(
8586
xlim,
8687
ylim,
8788
shrinked_width_region_content,
@@ -208,7 +209,41 @@ defmodule Matplotex.Figure.Areal.BarChart do
208209
end
209210

210211
def capture([], captured, _dataset, _bly), do: captured
212+
defp capture_stacked([{x, y} | to_capture], captured, %Dataset{
213+
color: color,
214+
width: width,
215+
pos: pos_factor,
216+
edge_color: edge_color,
217+
alpha: alpha,
218+
line_width: line_width
219+
} = dataset, bly) do
220+
{y, bottom_y} = if is_list(y) do
221+
{Enum.sum(y), y|>tl()|>Enum.sum()}
222+
else
223+
{y, bly}
224+
end
211225

226+
227+
capture_stacked(
228+
to_capture,
229+
captured ++
230+
[
231+
%Rect{
232+
type: "figure.bar",
233+
x: bar_position(x, pos_factor),
234+
y: y,
235+
width: width,
236+
height: bottom_y - y,
237+
color: color,
238+
stroke: edge_color || color,
239+
fill_opacity: alpha,
240+
stroke_opacity: alpha,
241+
stroke_width: line_width
242+
}
243+
],
244+
dataset, bly
245+
)
246+
end
212247
defp hypox(y) do
213248
nof_x = length(y)
214249
@xmin_value |> Nx.linspace(nof_x, n: nof_x) |> Nx.to_list()
@@ -218,12 +253,38 @@ defmodule Matplotex.Figure.Areal.BarChart do
218253
x + pos_factor
219254
end
220255

221-
222-
223256
defp list_of_ticks(data, step) do
224257
1..length(data)
225258
|> Enum.into([], fn d ->
226259
d * step
227260
end)
228261
end
262+
263+
defp do_transform_with_bottom(%Dataset{x: x, y: y, bottom: bottom} = dataset, xlim, ylim, width, height, transition) do
264+
y = [y | bottom]|> Nx.tensor() |> Nx.transpose()|> Nx.to_list()
265+
266+
transformed =
267+
x
268+
|> Enum.zip(y)
269+
|> Enum.map(fn {x, y} ->
270+
transform_with_bottom(x,y, xlim, ylim, width, height, transition)
271+
end)
272+
273+
%Dataset{dataset | transformed: transformed}
274+
end
275+
276+
defp transform_with_bottom(x, y, xlim, ylim, width, height, transition) when is_list(y) do
277+
transformed = Enum.map(y, fn y_with_bottom ->
278+
transformation(x, y_with_bottom, xlim, ylim, width, height, transition)
279+
|> Algebra.flip_y_coordinate()
280+
end)
281+
|>Enum.unzip()
282+
283+
{transformed|>elem(0)|>hd, transformed|> elem(1)}
284+
end
285+
286+
defp transform_with_bottom(x, y, xlim, ylim, width, height, transition) do
287+
transformation(x, y, xlim, ylim, width, height, transition)
288+
|> Algebra.flip_y_coordinate()
289+
end
229290
end

0 commit comments

Comments
 (0)