@@ -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 )
0 commit comments