Description:
I would like to enforce consistent legend and style throughout dynamic PieChart
evaluation.
Problem:
Based on current implementation, the color and legend associated with particular datasets switch throughout evaluation.
Example:
DynamicModule[
{n = 20},
Column[{
Row[{Slider[Dynamic[n], {1, 100, 1}], Dynamic[n]}],
Dynamic[
PieChart[#[[All, 2]], ChartLegends -> #[[All, 1]],
ChartStyle -> {Green, Red}] & @
Tally @RandomChoice[{True, False}, n]]
}]
]
Output:
How could I enforce Legend
to include both True
and False
; where True
to be always represented in Green
and False
in Red
? I would also like True
to come before False
in the Legend
.
Sort@Tally@RandomChoice...
it'll sort the values first and won't have this problem. $\endgroup$trueFalseTally[list_] := {#, Count[list, #]} & /@ {True, False};
instead ofTally
It will return {{True,numTrues},{False,numFalses}}` even if there aren't any, then your plot will format correctly. $\endgroup$