1
$\begingroup$

while searching for solutions making textures in bar charts possible, I found this code posted by the user halmir here: Hatched bars and bar-specific background in BarChart

Unfortunately this doesn't work for low chart heights, which are smaller than the width of the chart. The term (yt - width) becomes negative and errors appear.

For example:

BarChart[{2, -1, .05, -3, 3, 2.5}, BarSpacing -> 2, 
 ChartElementFunction -> barFilled[.65, 3, 35], ChartStyle -> 61, 
 GridLines -> {None, Automatic}]

enter image description here

Is there another solution to change the appearance of barcharts? Already tried the approach How to change texture of chart bar? which uses the texture command and images. For big numbers and stacked bar charts it is noch efficient anymore.

If you know an easy approach for changing barcharts, my computer and I would be very thankful:)

Sorry for the huge text & all the best, Kilian

$\endgroup$
  • 1
    $\begingroup$ Please add code with a BarChart and data that shows the problem bars, so we don't have to invent a data set etc, $\endgroup$ – MarcoB Jun 28 '18 at 22:09
0
$\begingroup$

I modified the code of the former posts like this:

barHatched[gap_, h_, seg_, 
color_][{{xmin_, xmax_}, {ymin_, ymax_}}, ___] := 
Module[{width, line, yt, yb, lend}, {yb, yt} = Sort[{ymin, ymax}];
width = xmax - xmin;

line = Table[{{xmin + i, yb}, {xmin + i, yt}}, {i, 0, width, h/seg}];
line = {Line[line]};

{{Opacity[1], EdgeForm[], 
Rectangle[{xmin, ymin}, {xmax + gap, ymax}]}, {color, 
CapForm["Butt"], line}, {FaceForm[], 
Rectangle[{xmin, ymin}, {xmax, ymax}]}}]


barHatched2[gap_, h_, seg_, 
color_][{{xmin_, xmax_}, {ymin_, ymax_}}, ___] := 
Module[{width, line, line2, yt, yb, lend, lend2}, {yb, yt} = 
Sort[{ymin, ymax}];
width = xmax - xmin;

 line = Table[{{xmin, i}, {xmax, i + width}}, {i, yb, yt - width, 
 h/seg}];
 line2 = 
 Table[{{xmin + i, yb}, {xmax, yb + width - i}}, {i, h/seg, width, 
 h/seg}];

 If[yt > width, lend = line[[-1, 1, 2]]];
 lend2 = line2[[1, 1, 2]];

 line = {If[yt > width, Line[line]],

 Line[Table[{{xmin + i, yb}, {xmax, yb + width - i}}, {i, h/seg, 
   width, h/seg}]],

 If[yt > width, 
 Line[Table[{{xmin, lend + i}, {xmax - (lend + width - yt) - i, 
     yt}}, {i, h/seg, width, h/seg}]],
 Line[
  Table[{{xmin, yb + i}, {xmin + yt - i, yt}}, {i, 0, yt, h/seg}]]]


  };

 {{Opacity[.2], EdgeForm[], 
  Rectangle[{xmin, ymin}, {xmax + gap, ymax}]}, {CapForm["Butt"], 
  line}, {FaceForm[], Rectangle[{xmin, ymin}, {xmax, ymax}]}}]

In cases ymax < width some changes had to be made. So it also works for lists with amounts smaller than the width of bars. Thanks for asking and offering your help!!

$\endgroup$
  • $\begingroup$ Is this an answer or additional information for the original post? I ask because it's not clear what the original issue is and therefore it's not clear if the above is an answer or not. (It's certainly fine and encouraged to answer one's own questions.) It seems that a graphic illustrating the problem would be very helpful. $\endgroup$ – JimB Jun 28 '18 at 23:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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