Skip to content

Commit d50d961

Browse files
author
Mohammed Sadique
committed
unit test
1 parent 5fc14a0 commit d50d961

5 files changed

Lines changed: 90 additions & 50 deletions

File tree

lib/matplotex.ex

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -213,9 +213,7 @@ defmodule Matplotex do
213213
bar(pos, values, width, [])
214214
end
215215

216-
def bar(pos, values, width, opts) do
217-
BarChart.create(%Figure{axes: %BarChart{}}, {pos, values, width}, opts)
218-
end
216+
219217

220218
@doc """
221219
Adds an additional dataset to a bar plot in the given `%Figure{}`.
@@ -251,12 +249,19 @@ defmodule Matplotex do
251249
|> M.bar(0, values2, width, label: "Dataset2", color: "#D3D3D3")
252250
|> M.bar(width, values2, width, label: "Dataset3", color: "green")
253251
"""
252+
253+
def bar(%Figure{} = figure, values, width, opts), do: bar(figure, width, values, width, opts)
254+
255+
def bar(pos, values, width, opts) do
256+
BarChart.create(%Figure{axes: %BarChart{}}, {pos, values, width}, opts)
257+
end
254258
def bar(%Figure{} = figure, pos, values, width, opts) do
255259
figure
256260
|> show_legend()
257261
|> BarChart.create({pos, values, width}, opts)
258262
end
259263

264+
260265
@doc """
261266
Creates a scatter plot based on the given `x` and `y` values, with optional customization provided via `opts`.
262267
@@ -751,7 +756,9 @@ defmodule Matplotex do
751756
def show_legend(figure) do
752757
Figure.show_legend(figure)
753758
end
754-
759+
def hide_legend(figure) do
760+
Figure.hide_legend(figure)
761+
end
755762
def set_options(figure, opts) do
756763
PlotOptions.set_options_in_figure(figure, opts)
757764
end

lib/matplotex/figure.ex

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ defmodule Matplotex.Figure do
6262

6363
def show_legend(%__MODULE__{axes: %module{} = axes} = figure),
6464
do: %{figure | axes: module.show_legend(axes)}
65+
def hide_legend(%__MODULE__{axes: %module{} = axes} = figure),
66+
do: %{figure | axes: module.hide_legend(axes)}
6567

6668
def set_figure_size(%__MODULE__{margin: margin, axes: axes} = figure, {fwidth, fheight} = fsize) do
6769
frame_size = {fwidth - fwidth * 2 * margin, fheight - fheight * 2 * margin}

lib/matplotex/figure/areal.ex

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -170,11 +170,11 @@ defmodule Matplotex.Figure.Areal do
170170
end
171171
end
172172
# For stacked bar chart the flattening supposed to be the sumation of yaxis data
173-
def flatten_for_data(datasets,_data, nil), do: flatten_for_data(datasets)
174-
def flatten_for_data(_datasets,%{x: x, y: y} = _data, bottom) do
175-
y = bottom
176-
|> Tuple.to_list()
177-
|> Kernel.++(y)
173+
def flatten_for_data(datasets, nil), do: flatten_for_data(datasets)
174+
def flatten_for_data([%{x: x, y: y}| _datasets], bottom) do
175+
176+
y= bottom
177+
|> Kernel.++([y])
178178
|> Nx.tensor(names: [:x, :y])
179179
|> Nx.sum(axes: [:x])
180180
|> Nx.to_list()
@@ -215,6 +215,9 @@ defmodule Matplotex.Figure.Areal do
215215
def show_legend(%__MODULE__{} = axes) do
216216
%__MODULE__{axes | show_legend: true}
217217
end
218+
def hide_legend(%__MODULE__{} = axes) do
219+
%__MODULE__{axes | show_legend: false}
220+
end
218221

219222
def set_frame_size(%__MODULE__{} = axes, frame_size) do
220223
%__MODULE__{axes | size: frame_size}

lib/matplotex/figure/areal/bar_chart.ex

Lines changed: 54 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ defmodule Matplotex.Figure.Areal.BarChart do
3838
dataset = Dataset.cast(%Dataset{x: x, y: values, pos: pos, width: width}, opts)
3939
datasets = data ++ [dataset]
4040
bottom = Keyword.get(opts, :bottom)
41-
xydata = flatten_for_data(datasets,data, bottom)
41+
xydata = datasets|>Enum.reverse()|> flatten_for_data(bottom)
4242

4343
%Figure{
4444
figure
@@ -57,7 +57,6 @@ defmodule Matplotex.Figure.Areal.BarChart do
5757
axes:
5858
%{
5959
dataset: datasets,
60-
data: {_x, y},
6160
limit: %{x: xlim, y: ylim},
6261
type: "stacked",
6362
region_content: %Region{
@@ -70,14 +69,12 @@ defmodule Matplotex.Figure.Areal.BarChart do
7069
} = axes,
7170
rc_params: %RcParams{
7271
x_padding: x_padding,
73-
white_space: white_space,
74-
concurrency: concurrency
72+
white_space: white_space
7573
}
7674
} = figure
7775
) do
7876
x_padding_value = width_region_content * x_padding + white_space
7977
shrinked_width_region_content = width_region_content - x_padding_value * 2
80-
8178
bar_elements =
8279
datasets
8380
|> Enum.map(fn dataset ->
@@ -89,7 +86,7 @@ defmodule Matplotex.Figure.Areal.BarChart do
8986
height_region_content,
9087
{x_region_content + x_padding_value, y_region_content}
9188
)
92-
|> capture(-y_region_content, concurrency)
89+
|> capture_stacked(-y_region_content)
9390
end)
9491
|> List.flatten()
9592

@@ -164,7 +161,6 @@ defmodule Matplotex.Figure.Areal.BarChart do
164161
step = (max - min) / (side * 2)
165162
{min..max |> Enum.into([], fn d -> d * round_to_best(step) end), lim}
166163
end
167-
168164
def capture(%Dataset{transformed: transformed} = dataset, bly, concurrency) do
169165
if concurrency do
170166
process_concurrently(transformed, concurrency, [[], dataset, bly])
@@ -173,6 +169,37 @@ defmodule Matplotex.Figure.Areal.BarChart do
173169
end
174170
end
175171

172+
def capture([{{x, y}, bottom} | to_capture], captured, %Dataset{
173+
color: color,
174+
width: width,
175+
pos: pos_factor,
176+
edge_color: edge_color,
177+
alpha: alpha,
178+
line_width: line_width
179+
} = dataset) do
180+
capture(
181+
to_capture,
182+
captured ++
183+
[
184+
%Rect{
185+
type: "figure.bar",
186+
x: bar_position(x, pos_factor),
187+
y: y,
188+
width: width,
189+
height: bottom - y,
190+
color: color,
191+
stroke: edge_color || color,
192+
fill_opacity: alpha,
193+
stroke_opacity: alpha,
194+
stroke_width: line_width
195+
}
196+
],
197+
dataset
198+
)
199+
end
200+
def capture([], captured, _dataset), do: captured
201+
202+
176203
def capture(
177204
[{x, y} | to_capture],
178205
captured,
@@ -209,40 +236,23 @@ defmodule Matplotex.Figure.Areal.BarChart do
209236
end
210237

211238
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
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+
calculate_point(point, bly)
246+
end)
247+
|>capture(captured, dataset)
248+
end
225249

250+
defp calculate_point({x, y}, _bly) when is_list(y) do
251+
{{x, Enum.sum(y)}, y|>tl()|>Enum.sum()}
252+
end
226253

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-
)
254+
defp calculate_point({x, y}, bly) do
255+
{{x, y}, bly}
246256
end
247257
defp hypox(y) do
248258
nof_x = length(y)
@@ -260,7 +270,7 @@ defmodule Matplotex.Figure.Areal.BarChart do
260270
end)
261271
end
262272

263-
defp do_transform_with_bottom(%Dataset{x: x, y: y, bottom: bottom} = dataset, xlim, ylim, width, height, transition) do
273+
defp do_transform_with_bottom(%Dataset{x: x, y: y, bottom: bottom} = dataset, xlim, ylim, width, height, transition) when is_list(bottom) do
264274
y = [y | bottom]|> Nx.tensor() |> Nx.transpose()|> Nx.to_list()
265275

266276
transformed =
@@ -273,6 +283,10 @@ defmodule Matplotex.Figure.Areal.BarChart do
273283
%Dataset{dataset | transformed: transformed}
274284
end
275285

286+
defp do_transform_with_bottom(dataset, xlim, ylim, width, height, transition) do
287+
do_transform(dataset, xlim, ylim, width, height, transition)
288+
end
289+
276290
defp transform_with_bottom(x, y, xlim, ylim, width, height, transition) when is_list(y) do
277291
transformed = Enum.map(y, fn y_with_bottom ->
278292
transformation(x, y_with_bottom, xlim, ylim, width, height, transition)

test/matplotex/figure/areal/bar_chart_test.exs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,22 @@ defmodule Matplotex.Figure.Areal.BarChartTest do
1515
assert %Figure{axes: %{element: elements}} =
1616
BarChart.materialized_by_region(figure) |> BarChart.materialize()
1717

18-
assert assert Enum.filter(elements, fn x -> x.type == "figure.bar" end) |> length() ==
18+
assert Enum.filter(elements, fn x -> x.type == "figure.bar" end) |> length() ==
1919
length(y)
2020
end
21+
22+
test "bottom variable in opts makes stacked bar chart and it will stack the bars" do
23+
values1 = [2,4,3,2]
24+
values2 = [1,2,1,4]
25+
values3 = [3,1,2,1]
26+
width = 0.3
27+
bar = Matplotex.bar(values1, width)|> Matplotex.bar(values2, width, bottom: [values1])
28+
|> Matplotex.bar(values3, width, bottom: [values2, values1])
29+
expected_elements_count = values1 ++ values2 ++ values3 |> length()
30+
assert %Figure{axes: %{element: elements}} = Figure.materialize(bar)
31+
32+
assert Enum.filter(elements, fn x -> x.type == "figure.bar" end) |> length() ==
33+
expected_elements_count
34+
end
2135
end
2236
end

0 commit comments

Comments
 (0)