@@ -79,6 +79,12 @@ function PowerGraphics._dataframe_plots_internal(
7979 plot. axis. title = title
8080 end
8181
82+ # For stacked bar plots CairoMakie's auto-legend extraction fails because a
83+ # single barplot! object with a vector `color` attribute has no per-element
84+ # color→label mapping. We capture the entries here and build the legend
85+ # manually with PolyElement below.
86+ bar_legend_entries = nothing
87+
8288 if bar
8389 plot_data = sum (data; dims = 1 ) ./ interval
8490
@@ -90,14 +96,16 @@ function PowerGraphics._dataframe_plots_internal(
9096 n = length (labels)
9197 xs = fill (1 , n)
9298 heights = vec (plot_data)
99+ bar_colors = collect (seriescolor[1 : n])
93100 CairoMakie. barplot! (
94101 plot. axis,
95102 xs,
96103 heights;
97104 stack = collect (1 : n),
98- color = collect (seriescolor[ 1 : n]) ,
105+ color = bar_colors ,
99106 label = string .(labels),
100107 )
108+ bar_legend_entries = (string .(labels), bar_colors)
101109
102110 plot. axis. xticks = ([1 ], [" " ])
103111 else
@@ -259,16 +267,41 @@ function PowerGraphics._dataframe_plots_internal(
259267 end
260268
261269 if legend_position == :bottom
262- CairoMakie. Legend (
263- plot. figure[2 , 1 ],
264- plot. axis;
265- orientation = :horizontal ,
266- tellwidth = false ,
267- tellheight = true ,
268- legend_kwargs... ,
269- )
270+ if ! isnothing (bar_legend_entries)
271+ bar_labels, bar_colors = bar_legend_entries
272+ elems = [CairoMakie. PolyElement (; color = c) for c in bar_colors]
273+ CairoMakie. Legend (
274+ plot. figure[2 , 1 ],
275+ elems,
276+ bar_labels;
277+ orientation = :horizontal ,
278+ tellwidth = false ,
279+ tellheight = true ,
280+ legend_kwargs... ,
281+ )
282+ else
283+ CairoMakie. Legend (
284+ plot. figure[2 , 1 ],
285+ plot. axis;
286+ orientation = :horizontal ,
287+ tellwidth = false ,
288+ tellheight = true ,
289+ legend_kwargs... ,
290+ )
291+ end
270292 else
271- CairoMakie. Legend (plot. figure[1 , 2 ], plot. axis; legend_kwargs... )
293+ if ! isnothing (bar_legend_entries)
294+ bar_labels, bar_colors = bar_legend_entries
295+ elems = [CairoMakie. PolyElement (; color = c) for c in bar_colors]
296+ CairoMakie. Legend (
297+ plot. figure[1 , 2 ],
298+ elems,
299+ bar_labels;
300+ legend_kwargs... ,
301+ )
302+ else
303+ CairoMakie. Legend (plot. figure[1 , 2 ], plot. axis; legend_kwargs... )
304+ end
272305 end
273306 plot. has_legend = true
274307 end
0 commit comments