I would like to diagonally hatch the bars in BarChart
and to have three different background colors for the different bars, like this:
Any ideas how to reproduce this image? The closest post I found is here.
Answer
With a bit of manual parameters:
barFilled[gap_, h_, seg_][{{xmin_, xmax_}, {ymin_, ymax_}}, ___] :=
Module[{width, line, yt, yb, lend},
{yb, yt} = Sort[{ymin, ymax}];
width = xmax - xmin;
line = Table[{{xmin, i}, {xmax, i + width}}, {i, yb, yt - width, h/seg}];
lend = line[[-1, 1, 2]];
line = {Line[line],
Line[Table[{{xmin + i, yb}, {xmax, yb + width - i}}, {i, h/seg, width, h/seg}]],
Line[Table[{{xmin, lend + i}, {xmax - (lend + width - yt) - i,yt}}, {i, h/seg, width + h/seg, h/seg}]]};
{{Opacity[.2], EdgeForm[], Rectangle[{xmin, -h}, {xmax + gap, h}]},
{CapForm["Butt"], line}, {FaceForm[], Rectangle[{xmin, ymin}, {xmax, ymax}]}}]
BarChart[{2, -1, 1.5, -3, 3, 2.5}, BarSpacing -> 2,
ChartElementFunction -> barFilled[.65, 3, 35], ChartStyle -> 61,
GridLines -> {None, Automatic}]
Like AimForClarity mentioned, to avoid empty strip, we could replace 0 with some dummy value + meta and define barFilled for that value. For example:
barFilled[gap_, h_, seg_][{{xmin_, xmax_}, {ymin_, ymax_}}, _, {None}] :=
{{Opacity[.2], EdgeForm[], Rectangle[{xmin, -h}, {xmax + gap, h}]}}
BarChart[{2, -1, 0, -2, 1, 2} /. {0 -> (1 -> None)}, BarSpacing -> 2, ChartElementFunction -> barFilled[.65, 2, 35],
ChartStyle -> 61, GridLines -> {None, Automatic}]
Comments
Post a Comment