6
$\begingroup$

Here is a BarChart of some data

data = {90, 91, 95, 103, 107, 102, 107, 105, 98, 96}

{90, 91, 95, 103, 107, 102, 107, 105, 98, 96}

BarChart[data, ChartElementFunction -> "GradientScaleRectangle"]

enter image description here

When I add PlotRange the color goes away.

BarChart[data, ChartElementFunction -> "GradientScaleRectangle", 
 PlotRange -> {80, 120}]

enter image description here

Is it possible to work around this?

$\endgroup$
6
$\begingroup$

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]

enter image description here

Use cEF["GradientScaleRectangle", "ColorScheme" -> "Rainbow"][data, plotrange] to get:

enter image description here

BarChart[data, ColorFunction -> "Rainbow", 
 ChartElementFunction -> 
    cEF["GlassRectangle", "GradientOrigin" -> Top][data, {75, 110}], 
 ChartLabels -> CharacterRange["A", "J"],  
 PlotRange -> {75, 110}, 
 ImagePadding -> 20]

enter image description here

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]

enter image description here

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]}]

enter image description here

| improve this answer | |
$\endgroup$
  • $\begingroup$ Results look great! I can more or less follow your code but am lost at the last part. The documention has two examples of ChartElementDataFunction in the BarChart documention but is not mentioned in Details or Options. Could you expound on the arguments in this experssion? ChartElementDataFunction[cedf, o][{#[[1]], Clip[#[[2]], pr]}, ##2]. In particular ##2, #1 and #2. Thank you $\endgroup$ – Jack LaVigne Nov 21 at 15:00
  • $\begingroup$ @JackLaVigne, cEF can also be wriiten as ClearAll[cEF2]; cEF2[cedf_: "GradientScaleRectangle", o : OptionsPattern[]][d_, pr_][ box : {{xmin_, xmax_}, {ymin_, ymax_}}, value_, metadata___] := (Charting`ChartStyleInformation[ "BoundingBox"] = {{.5, Length@d + .5}, {pr[[1]], Max@d}}; ChartElementDataFunction[cedf, o][{box[[1]], Clip[box[[2]], pr]}, val, metadata]), which, I hope, clarifies the correspondence between the slots (#1 <-> box, #2 <-> value, and ##2 <-> second and later arguments) . $\endgroup$ – kglr Nov 21 at 16:14
  • $\begingroup$ Thank you, I understand better now. Something odd that happens to me with either form is I get an error "Set::write: Tag ChartingChartStyleInformation in ChartingChartStyleInformation[BoundingBox] is Protected." after having defined cEF and run BarChart[ data, ChartElementFunction -> cEF[][data, {80, 120}], PlotRange -> {80, 120} ] the first time. The picture is good but the error appears. The second time I run it the error is absent. Does that happen to you? $\endgroup$ – Jack LaVigne Nov 21 at 19:39
  • $\begingroup$ @JackLaVigne, updated with a variant that does not produce an error message. $\endgroup$ – kglr Nov 21 at 19:47
4
$\begingroup$
data = {90, 91, 95, 103, 107, 102, 107, 105, 98, 96};
Show[BarChart[data,
  ChartElementFunction -> "GradientScaleRectangle"],
 PlotRange -> {80, 120}]

enter image description here

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}]

enter image description here

| improve this answer | |
$\endgroup$
  • 1
    $\begingroup$ Thank you Chris. Now colors show up but the color range of the upper quarter of the plot. Would prefer the colors to be be blue to orange. $\endgroup$ – Jack LaVigne Nov 21 at 0:25
  • $\begingroup$ Perfect. Simple subtraction on the data and Tick modifications works great. When I put a Frame around it there was an initial failure. Discovered that with Frame must modify FrameTicks (seems straightforward). Thank you. $\endgroup$ – Jack LaVigne Nov 21 at 15:07

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.