I was testing PieChart using the following random data before touching my own dataset and found the ChartLabels
using the Callout
function is so slow. I wonder why and is there anyway to improve its performance? I tried PerformanceGoal→"Speed"
(learned from here) as the PieChart
option, it seems to me not very effective. On my laptop, the following three ChartLabels
option yield 0.204785, 15.0763, 48.0431
with AbsoluteTiming
.
Generate data
SeedRandom[14];
list = RandomInteger[{1, 100}, 300];
stats = Counts@list
rules=Normal@stats;
Plot PieChart
Table[AbsoluteTiming[PieChart[stats, ChartStyle -> "Pastel", ImageSize -> Full, opts]],
{opts, {
ChartLabels -> Placed[Keys@stats, "RadialCallout"], (*okay*)
ChartLabels -> Callout[Keys@stats, Automatic], (*slow*)
ChartLabels ->
Callout[Row[{Style[#[[1]], Italic], " = ", Style[#[[2]]]}] & /@
rules, Automatic] (* too slow *)
}}]
BTW, when I changed the Head
to BarChart
with the rest the same, it is faster. However, the Callout
slow issue remained.
—————————————————————————
Further tests on my laptop showed with specific ImageSize
, the AbsoluteTiming
was sometimes short and sometimes long. For example. 800
or 1000
still took long execution time (> 40 sec) while 1200
and 2000
were faster (< 10 sec).
—————————————————————————
I tried the following. In general, it was faster on my laptop.
callout=List@@@(Normal@stats);
statsCallout=Callout[#[[2]],#[[1]]<>" = "<>ToString@#[[2]]]& /@ callout;
PieChart[statsCallout, ImageSize->1200, ChartStyle->"Pastel"]//AbsoluteTiming
callouts = Callout[Row[{Style[#[[1]], Italic], " = ", Style[#[[2]]]}] & /@ rules, Automatic];
and thenPieChart[stats, ChartLabels->callouts]
, I think it should work fine. I imagine it's redoing theMap
for every single label. $\endgroup$ImageSize→Full
option in my original post:) But because of this, I found out the reason LoL. WithCallout
pre-calculated, the improvement of timing seems to me marginally, 47ish comparing to the original 48 withImageSize->Full
. But if I changedImageSize
to a specific size, say2000
instead ofFull
, the timing was reduced greatly. I do not know why, but it worked. $\endgroup$