Skip to content

Commit 84073d4

Browse files
author
Mohammed Sadique
committed
cleanup
1 parent c95f892 commit 84073d4

2 files changed

Lines changed: 25 additions & 100 deletions

File tree

lib/matplotex/figure/areal.ex

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,19 @@ defmodule Matplotex.Figure.Areal do
395395
Algebra.transform_given_point(x, y, sx, sy, tx, ty)
396396
end
397397

398+
399+
def do_transform(%Dataset{x: x, y: y, bottom: bottom} = dataset, xlim, ylim, width, height, transition) when is_list(bottom) do
400+
y = [y | bottom]|> Nx.tensor() |> Nx.transpose()|> Nx.to_list()
401+
402+
transformed =
403+
x
404+
|> Enum.zip(y)
405+
|> Enum.map(fn {x, y} ->
406+
transform_with_bottom(x,y, xlim, ylim, width, height, transition)
407+
end)
408+
409+
%Dataset{dataset | transformed: transformed}
410+
end
398411
def do_transform(%Dataset{x: x, y: y} = dataset, xlim, ylim, width, height, transition) do
399412
transformed =
400413
x
@@ -407,4 +420,13 @@ defmodule Matplotex.Figure.Areal do
407420

408421
%Dataset{dataset | transformed: transformed}
409422
end
423+
424+
425+
defp transform_with_bottom(x, y, xlim, ylim, width, height, transition) when is_list(y) do
426+
y_top = Enum.sum(y)
427+
y_bottom = y|>tl()|>Enum.sum()
428+
transformed = transformation(x, y_top, xlim, ylim, width, height, transition) |> Algebra.flip_y_coordinate()
429+
{_, transformed_y_bottom} = transformation(x, y_bottom, xlim, ylim, width, height,transition) |> Algebra.flip_y_coordinate()
430+
{transformed, transformed_y_bottom}
431+
end
410432
end

lib/matplotex/figure/areal/bar_chart.ex

Lines changed: 3 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
defmodule Matplotex.Figure.Areal.BarChart do
22
@moduledoc false
33
import Matplotex.Figure.Numer
4-
alias Matplotex.Utils.Algebra
54
alias Matplotex.Figure.Areal.PlotOptions
65
alias Matplotex.Figure.Areal.Region
76
alias Matplotex.Element.Legend
@@ -52,48 +51,6 @@ defmodule Matplotex.Figure.Areal.BarChart do
5251
def materialize(figure) do
5352
materialize_bars(figure)
5453
end
55-
defp materialize_bars(
56-
%Figure{
57-
axes:
58-
%{
59-
dataset: datasets,
60-
limit: %{x: xlim, y: ylim},
61-
type: "stacked",
62-
region_content: %Region{
63-
x: x_region_content,
64-
y: y_region_content,
65-
width: width_region_content,
66-
height: height_region_content
67-
},
68-
element: elements
69-
} = axes,
70-
rc_params: %RcParams{
71-
x_padding: x_padding,
72-
white_space: white_space
73-
}
74-
} = figure
75-
) do
76-
x_padding_value = width_region_content * x_padding + white_space
77-
shrinked_width_region_content = width_region_content - x_padding_value * 2
78-
bar_elements =
79-
datasets
80-
|> Enum.map(fn dataset ->
81-
dataset
82-
|> do_transform_with_bottom(
83-
xlim,
84-
ylim,
85-
shrinked_width_region_content,
86-
height_region_content,
87-
{x_region_content + x_padding_value, y_region_content}
88-
)
89-
|> capture_stacked(-y_region_content)
90-
end)
91-
|> List.flatten()
92-
93-
elements_with_bar = elements ++ bar_elements
94-
95-
%Figure{figure | axes: %{axes | element: elements_with_bar}}
96-
end
9754

9855
defp materialize_bars(
9956
%Figure{
@@ -176,7 +133,7 @@ defmodule Matplotex.Figure.Areal.BarChart do
176133
edge_color: edge_color,
177134
alpha: alpha,
178135
line_width: line_width
179-
} = dataset) do
136+
} = dataset, bly) do
180137
capture(
181138
to_capture,
182139
captured ++
@@ -194,10 +151,10 @@ defmodule Matplotex.Figure.Areal.BarChart do
194151
stroke_width: line_width
195152
}
196153
],
197-
dataset
154+
dataset,
155+
bly
198156
)
199157
end
200-
def capture([], captured, _dataset), do: captured
201158

202159

203160
def capture(
@@ -236,26 +193,6 @@ defmodule Matplotex.Figure.Areal.BarChart do
236193
end
237194

238195
def capture([], captured, _dataset, _bly), do: captured
239-
defp capture_stacked(%Dataset{transformed: transformed}=dataset, bly) do
240-
capture_stacked(transformed, [],dataset, bly)
241-
end
242-
defp capture_stacked(to_capture, captured, dataset, bly) do
243-
to_capture
244-
|>Enum.map(fn point ->
245-
format_point(point, bly)
246-
end)
247-
|>capture(captured, dataset)
248-
end
249-
250-
defp format_point({{_x, _y}, _y_bottom} = point, _bly), do: point
251-
# sum_of_tail = y |> Enum.sort()|>tl()|>Enum.sum
252-
# sum_of_second_tail = y|>tl()|>tl()|>Enum.sum()
253-
# {{x, y|>Enum.max()|>Kernel.-(sum_of_tail)}, y|>tl()|>Enum.max()|> Kernel.-(sum_of_second_tail)}
254-
# end
255-
256-
defp format_point({x, y}, bly) do
257-
{{x, y}, bly}
258-
end
259196
defp hypox(y) do
260197
nof_x = length(y)
261198
@xmin_value |> Nx.linspace(nof_x, n: nof_x) |> Nx.to_list()
@@ -272,38 +209,4 @@ defmodule Matplotex.Figure.Areal.BarChart do
272209
end)
273210
end
274211

275-
defp do_transform_with_bottom(%Dataset{x: x, y: y, bottom: bottom} = dataset, xlim, ylim, width, height, transition) when is_list(bottom) do
276-
y = [y | bottom]|> Nx.tensor() |> Nx.transpose()|> Nx.to_list()
277-
278-
transformed =
279-
x
280-
|> Enum.zip(y)
281-
|> Enum.map(fn {x, y} ->
282-
transform_with_bottom(x,y, xlim, ylim, width, height, transition)
283-
end)
284-
285-
%Dataset{dataset | transformed: transformed}
286-
end
287-
288-
defp do_transform_with_bottom(dataset, xlim, ylim, width, height, transition) do
289-
do_transform(dataset, xlim, ylim, width, height, transition)
290-
end
291-
292-
defp transform_with_bottom(x, y, xlim, ylim, width, height, transition) when is_list(y) do
293-
# transformed = Enum.map(y, fn y_with_bottom ->
294-
# transformation(x, y_with_bottom, xlim, ylim, width, height, transition)
295-
# |> Algebra.flip_y_coordinate()
296-
# end)
297-
# |>Enum.unzip()
298-
y_top = Enum.sum(y)
299-
y_bottom = y|>tl()|>Enum.sum()
300-
transformed = transformation(x, y_top, xlim, ylim, width, height, transition) |> Algebra.flip_y_coordinate()
301-
{_, transformed_y_bottom} = transformation(x, y_bottom, xlim, ylim, width, height,transition) |> Algebra.flip_y_coordinate()
302-
{transformed, transformed_y_bottom}
303-
end
304-
305-
defp transform_with_bottom(x, y, xlim, ylim, width, height, transition) do
306-
transformation(x, y, xlim, ylim, width, height, transition)
307-
|> Algebra.flip_y_coordinate()
308-
end
309212
end

0 commit comments

Comments
 (0)