BarChart - Maintenir ChartElementFunction lors du changement de PlotRange
Nov 21 2020
Voici BarChart
quelques données
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"]

Lorsque j'ajoute, PlotRange
la couleur disparaît.
BarChart[data, ChartElementFunction -> "GradientScaleRectangle",
PlotRange -> {80, 120}]

Est-il possible de contourner ce problème?
Réponses
6 kglr Nov 21 2020 at 08:04
Nous pouvons utiliser une coutume ChartElementFunction
pour découper les 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]] &;
Exemples:
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]

Utilisez cEF["GradientScaleRectangle", "ColorScheme" -> "Rainbow"][data, plotrange]
pour obtenir:

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]

Mise à jour: une approche alternative utilisant des données d'entrée modifiées et des graduations personnalisées et un remplissage de plage de tracé:
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]}]

4 ChrisDegnen Nov 21 2020 at 07:21
data = {90, 91, 95, 103, 107, 102, 107, 105, 98, 96};
Show[BarChart[data,
ChartElementFunction -> "GradientScaleRectangle"],
PlotRange -> {80, 120}]

Tracer les couleurs bleu à orange en ajustant les données et en cochant les étiquettes.
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}]
