# How to create a stacked BarChart with custom bar origins as function?

I want to create a simple stacked bar chart where the bars start a custom y-axis point and end at another y-axis point.

So far I have:

BarChart[{{2, 4}, {-2, 6}}, ChartLayout -> "Stacked"]


which gives:

However, I want a single bar on the left that originates at 2 and goes up to 4, and on the right a bar that starts at -2 and goes up to 6.

How can I achieve this? I also experimented around with the ChartElementFunction but I can not find how to specify a starting point of the bar.

Thanks for any help in advance.

I'm not sure whether this is documented or not. You can pass additional arguments to the ChartElementFunction like this:

r[{{xmin_, xmax_}, {ymin_, ymax_}}, y_, {origin_}] :=
Rectangle[{xmin, ymin + origin}, {xmax, ymax + origin}]

BarChart[{{1} -> 1, {1} -> 2, {2} -> 3}, ChartElementFunction -> r]


edit

Perhaps more elegant and somewhat more obscure:

r[a_, _, {origin_}] := Rectangle @@ Transpose[a + {0, origin}]

• Ah, now this is great. Didn't know I can pass meta data into the element function as a rule. Thanks very much! – Thomas Fankhauser Nov 19 '15 at 13:37
• me too (missing something*) – Kuba Nov 19 '15 at 13:51
• @Kuba He! :D ... – Dr. belisarius Nov 19 '15 at 13:52
• Very nice, you can write even {1 -> 1, 1 -> 2, 3 -> 3}! – ybeltukov Nov 19 '15 at 14:05
• and {2, 3, 4} -> 1 for a common origin :) – ybeltukov Nov 19 '15 at 14:12

ChartingRangeBarChart

The undocumented function ChartingRangeBarChart does exactly what is required. It takes the same options as BarChart.

ChartingRangeBarChart[{{2, 4}, {-2, 6}},
ChartElementFunction->"GlassRectangle", ChartStyle->97, ChartLabels->{"A","B"}]


ChartingRangeBarChart[RandomInteger[{-10, 10}, {20, 2}],
ChartElementFunction -> "GlassRectangle", ChartStyle-> 97,
ChartLabels -> CharacterRange["A","T"]]


The same result can be obtained using a custom ChartElementFunction as in belisarius's answer. However, this function needs to be modified to work properly with a non-default BarOrigin. ChartingRangeBarChart takes care of necessary changes automatically. For example, in the first example above, with the options BarOrigin -> Left and BarOrigin -> Right we get

The disadvantage of this alternative is that it works only for single data set of pairs. With multiple data sets, using a custom ChartElementFunction with appropriate modifications to account for the BarOrigin setting seems to be the only way. The function ceF` in this answer in a closely related Q/A does exactly that.