Skip to content

Commit 3e6493c

Browse files
author
Mohammed Sadique
committed
stacked bar
1 parent 9b0dac1 commit 3e6493c

3 files changed

Lines changed: 54 additions & 4 deletions

File tree

lib/matplotex/figure/areal/bar_chart.ex

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,51 @@ defmodule Matplotex.Figure.Areal.BarChart do
5050
def materialize(figure) do
5151
materialize_bars(figure)
5252
end
53+
defp materialize_bars(
54+
%Figure{
55+
axes:
56+
%{
57+
dataset: datasets,
58+
data: data,
59+
limit: %{x: xlim, y: ylim},
60+
type: "stacked",
61+
region_content: %Region{
62+
x: x_region_content,
63+
y: y_region_content,
64+
width: width_region_content,
65+
height: height_region_content
66+
},
67+
element: elements
68+
} = axes,
69+
rc_params: %RcParams{
70+
x_padding: x_padding,
71+
white_space: white_space,
72+
concurrency: concurrency
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+
79+
bar_elements =
80+
datasets
81+
|> Enum.map(fn dataset ->
82+
dataset
83+
|> do_transform(
84+
xlim,
85+
ylim,
86+
shrinked_width_region_content,
87+
height_region_content,
88+
{x_region_content + x_padding_value, y_region_content}
89+
)
90+
|> capture(-y_region_content, concurrency)
91+
end)
92+
|> List.flatten()
93+
94+
elements_with_bar = elements ++ bar_elements
95+
96+
%Figure{figure | axes: %{axes | element: elements_with_bar}}
97+
end
5398

5499
defp materialize_bars(
55100
%Figure{

lib/matplotex/figure/areal/plot_options.ex

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,11 @@ defmodule Matplotex.Figure.Areal.PlotOptions do
3636
opts = Keyword.delete(opts, :label)
3737
cmap = Keyword.get(opts, :cmap)
3838
colors = Keyword.get(opts, :colors)
39+
bottom = Keyword.get(opts, :bottom)
3940

4041
%Figure{
4142
figure
42-
| axes: axes |> struct(opts) |> cast_two_d_structs(opts)|> update_cmap(cmap, colors)
43+
| axes: axes |> struct(opts) |> cast_two_d_structs(opts)|> update_cmap(cmap, colors) |> update_type(bottom)
4344
}
4445
end
4546

@@ -63,13 +64,16 @@ defmodule Matplotex.Figure.Areal.PlotOptions do
6364
Keyword.drop(opts, @immutable_keys)
6465
end
6566
defp update_cmap(axes, nil, colors) when is_list(colors) do
66-
6767
%{axes | cmap: Colormap.fetch_cmap(@default_cmap)}
6868
end
6969

7070
defp update_cmap(axes, cmap, colors) when is_list(colors) do
7171
%{axes | cmap: Colormap.fetch_cmap(cmap)}
7272
end
7373

74-
defp update_cmap(figure, _, _), do: figure
74+
defp update_cmap(axes, _, _), do: axes
75+
defp update_type(axes, nil), do: axes
76+
defp update_type(axes, bottom) when is_list(bottom) do
77+
%{axes | type: "stacked"}
78+
end
7579
end

lib/matplotex/figure/dataset.ex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ defmodule Matplotex.Figure.Dataset do
2424
marker: @default_marker,
2525
linestyle: @default_linestyle,
2626
marker_size: @default_marker_size,
27-
line_width: @line_width
27+
line_width: @line_width,
28+
bottom: nil
2829
]
2930

3031
def cast(dataset, values) do

0 commit comments

Comments
 (0)