Tell me more ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I wrote the following code, but I don't know how to highlight the moving bar.

bsort[list_] := 
 Module[{A = list, tmp}, 
  tmp = Reap[
     Do[If[A[[j]] > A[[j + 1]], 
       Sow[A]; {A[[j + 1]], A[[j]]} = {A[[j]], A[[j + 1]]}], {i, 
       Length@A}, {j, Length@A - i}]][[2, 1]];
  Append[tmp, A]]

d = bsort@RandomSample@Range@10;
ListAnimate[
 BarChart[#, ChartLabels -> Placed[Style[#, 15] & /@ #, Above]] & /@ d]

I want it to look like this:

enter image description here

share|improve this question
Shouldn't bubbles go up ? – Artes Jan 10 at 16:37
1  
@Artes: Tilt head 90 degrees? – murray Jan 10 at 19:01

2 Answers

up vote 13 down vote accepted
dTagged =
  MapAt[
    Style[#, Red] &,
    Rest @ d,
    Position[Differences @ d, _Integer?Positive]
  ] ~Prepend~ First[d];

ListAnimate[
  BarChart[#, ChartLabels -> Placed[Style[#, 15] & /@ #, Above]] & /@ 
   dTagged
]

Mathematica graphics

share|improve this answer

Modifying bsort to include style changes during Sowing:

bsort2[list_] :=  Module[{A = Style[#, GrayLevel[.6]] & /@ list, tmp}, 
 tmp = Reap[Do[If[First /@ (A[[j]] > A[[j + 1]]),
    Sow[ A /. (A[[j]] -> (A[[j]] /. GrayLevel[.6] -> Red))];
      {A[[ j + 1]], A[[j]]} = {A[[j]], A[[j + 1]]}],
   {i, Length@A}, {j, Length@A - i}]][[2, 1]]; Append[tmp, A]];
d2 = bsort2@RandomSample@Range[20];
opts = {ChartBaseStyle -> EdgeForm[White], BaseStyle -> (FontSize -> 14),
  AspectRatio -> 1, Frame -> False, Axes -> False, PlotRangePadding -> 2};

Using ListAnimate:

 ListAnimate[BarChart[Labeled[#, #, Above] & /@ #, opts] & /@ d2]

enter image description here

Using Clock:

Dynamic[BarChart[Labeled[#, #, Above] & /@ d2[[Clock[{1, Length[d2], 1}, 5, 1]]], opts]]

enter image description here

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.