BarChart - Maintain ChartElementFunction when changing PlotRange
We can use a custom ChartElementFunction
to clip the rectangles:
ClearAll[cEF]
cEF[cedf_: "GradientScaleRectangle", o: OptionsPattern[]][d_, pr_] :=
Block[{Charting`ChartStyleInformation},
Charting`ChartStyleInformation["BoundingBox"] =
{{.5, Length @ d + .5}, {pr[[1]], Max @ d}};
ChartElementDataFunction[cedf, o][{#[[1]], Clip[#[[2]], pr]}, ##2]] &;
Examples:
data = {90, 91, 95, 103, 107, 102, 107, 105, 98, 96};
plotrange = {80, 120};
BarChart[data,
ChartElementFunction -> cEF[][data, plotrange],
ChartLabels -> CharacterRange["A", "J"],
PlotRange -> plotrange,
ImagePadding -> 20]
Use cEF["GradientScaleRectangle", "ColorScheme" -> "Rainbow"][data, plotrange]
to get:
BarChart[data, ColorFunction -> "Rainbow",
ChartElementFunction ->
cEF["GlassRectangle", "GradientOrigin" -> Top][data, {75, 110}],
ChartLabels -> CharacterRange["A", "J"],
PlotRange -> {75, 110},
ImagePadding -> 20]
BarChart[data,
ChartStyle -> (Texture[ExampleData[{"TestImage", #}]] & /@ {"Lena", "Mandrill"}),
ChartElementFunction -> cEF["TextureRectangle"][data, {70, 110}],
ChartLabels -> CharacterRange["A", "J"],
PlotRange -> {70, 110},
ImagePadding -> 20, ImageSize -> Large]
Update: An alternative approach using modified input data and custom ticks and plot range padding:
plotrange = {80, 120};
data2 = data - plotrange[[1]];
plotrange2 = {0, - Subtract @@ plotrange};
padding = plotrange2[[2]] - Max @ data2;
BarChart[Thread[Tooltip[data2, data]],
ChartElementFunction -> "GradientScaleRectangle",
ChartLabels -> CharacterRange["A", "J"],
PlotRangePadding -> {{Automatic, Automatic}, {Automatic, padding}},
Ticks -> {Automatic, Charting`FindTicks[plotrange2, plotrange]}]
data = {90, 91, 95, 103, 107, 102, 107, 105, 98, 96};
Show[BarChart[data,
ChartElementFunction -> "GradientScaleRectangle"],
PlotRange -> {80, 120}]
Plotting the colours blue to orange by adjusting the data and tick labels.
data = {90, 91, 95, 103, 107, 102, 107, 105, 98, 96} - 80;
Show[BarChart[data,
ChartElementFunction -> "GradientScaleRectangle",
Ticks -> {Automatic,
{{0, 80}, {10, 90}, {20, 100}, {30, 110}, {40, 120}}}],
PlotRange -> {0, 40}]