Search code examples
wolfram-mathematicabar-chartmathematica-8

Mathematica: Can I match the BarChart Legend to the stacked bars?


I would like to have the vertical arrangement of colors in stacked bars match the arrangement of colors in the chart legend. But no matter what I try, they don't match. Here's the situation

BarChart[{{5, 37, 56}, {22, 49, 28}, {31, 60, 10}},
   ChartLayout -> "Percentile",
   ChartLegends -> Placed[{"1-Volume", "2-Area", "3-Length"}, Right],
   ChartLabels -> {{"Before", "During", "After"}, None}]

Chart1

In the real-world example the legend has quite a few more entries (6), so it would be nice if the order of the legend colors matched the order in the bars. I realize that I could set the ChartLegends to display at Bottom, but doesn't look good given the many legend entries.

Also, reversing the Legends list does not work as desired. The text of the legends was re-ordered, but the colors were not reordered (see below), so the legend captions no longer match the data in the chart.

Changing the order of the data (or the data and the legend items) does not work either.

Any suggestions?

enter image description here


Solution

  • BarChart[{{5, 37, 56}, {22, 49, 28}, {31, 60, 10}}, 
      ChartLayout -> "Percentile", 
      ChartLegends -> {"1-Volume", "2-Area", "3-Length"}, 
      ChartLabels -> {{"Before", "During", "After"}, None}] /. 
     Column[List[a : Grid[List[___]] ..]] :> Column[Reverse@List@a]
    

    enter image description here

    Edit

    Remember to use FullForm when you want to mess up with Graphics/Chart/Plot internals