4
$\begingroup$

I am trying to obtain a thin frame around the bars in this chart but it's proving rather tricky. Can anyone suggest a solution?

size = {3, 12, 8, 0.5, 7, 5, 6, 7, 3, 4, 2};

bar[i_] := Inset[Graphics[{EdgeForm[{Thin, Black}],
    FaceForm@LightBlue, Rectangle[{0, -1/2}, {size[[i]], 1/2}]},
   PlotRange -> {{0, size[[i]]}, {(-1/2), 1/2}}],
  {1, -(Length[size] - i + .5)}, {Left, Center}, size[[i]]];

Graphics[{LightYellow, Rectangle[{0, 0}, {15, -(Length[size] + 1)}],
  Array[bar, Length[size]]},
 PlotRange -> {{0, 15}, {0, -(Length[size] + 1)}}]

enter image description here

A second version has the same problem:

bar[i_] := Inset[Graphics[{LightBlue,
    Rectangle[{0, -1/2}, {size[[i]], 1/2}]},
   PlotRange -> {{0, size[[i]]}, {(-1/2), 1/2}},
   Frame -> True, FrameTicks -> None, ImagePadding -> 0],
  {1, -(Length[size] - i + .5)}, {Left, Center}, size[[i]]];

Edit

Thanks to Heike's comment I have something I can use:

size = {3, 12, 8, 0.5, 7, 5, 6, 7, 3, 4, 2};

bar[i_] := Rectangle[{1, -(i - 1)}, {size[[Length[size] + 1 - i]] + 1, -i}];

Graphics[{LightYellow, Rectangle[{0, 0}, {15, -(Length[size] + 1)}],
  EdgeForm[{Thin, Black}], FaceForm@LightBlue,
  Array[bar, Length[size]]},
 PlotRange -> {{0, 15}, {0, -(Length[size] + 1)}}]

enter image description here

$\endgroup$
1
  • 4
    $\begingroup$ Is there any particular reason why you're using Inset to place the individual bars in the yellow rectangle instead of using Rectangles directly? $\endgroup$
    – Heike
    Commented May 15, 2012 at 13:03

1 Answer 1

2
$\begingroup$

I cannot remember the solution to this problem, but I will point out that at the right ImageSize the lines are not clipped:

Graphics[{LightYellow, Rectangle[{0, 0}, {15, -(Length[size] + 1)}], 
  Array[bar, Length[size]]}, 
 PlotRange -> {{0, 15}, {0, -(Length[size] + 1)}}, 
 ImageSize -> {Automatic, 450}]

Mathematica graphics

$\endgroup$
3
  • $\begingroup$ Okay, I think I remember the solution. A somewhat similar problem affects BarChart, and someone solved it by creating one big polygon for all the bars and drawing Lines over it. $\endgroup$
    – Mr.Wizard
    Commented May 15, 2012 at 13:22
  • $\begingroup$ @ Mr.Wizard - Did you work out that 450 was the right image height by trial and error? I see 150 nearly works but 650 fails more. $\endgroup$ Commented May 16, 2012 at 8:26
  • $\begingroup$ @Chris yes, I simply drag-resized the graphic until it looked good. I saw that you have a working method so I didn't try to reimplement the Polygon/Line solution. $\endgroup$
    – Mr.Wizard
    Commented May 16, 2012 at 12:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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