Skip to content

Commit 0bbd12a

Browse files
Merge pull request #498 from plotly/condense-grouped-bar-plots
Condense spacing in grouped 'bar' plots
2 parents 17511e4 + 01d761d commit 0bbd12a

File tree

1 file changed

+34
-32
lines changed

1 file changed

+34
-32
lines changed

plotly/plotlyfig_aux/handlegraphics/updateBar.m

+34-32
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
function obj = updateBar(obj,barIndex)
2-
32
% x: ...[DONE]
43
% y: ...[DONE]
54
% name: ...[DONE]
@@ -42,7 +41,7 @@
4241
% outliercolor: ...[NA]
4342
% outlierwidth: ...[NA]
4443

45-
%-------------------------------------------------------------------------%
44+
%---------------------------------------------------------------------%
4645

4746
%-AXIS INDEX-%
4847
axIndex = obj.getAxisIndex(obj.State.Plot(barIndex).AssociatedAxis);
@@ -53,80 +52,83 @@
5352
%-CHECK FOR MULTIPLE AXES-%
5453
[xSource, ySource] = findSourceAxis(obj, axIndex);
5554

56-
%-------------------------------------------------------------------------%
55+
%---------------------------------------------------------------------%
5756

5857
%-associate axis-%
59-
obj.data{barIndex}.xaxis = sprintf('x%d', xSource);
60-
obj.data{barIndex}.yaxis = sprintf('y%d', ySource);
58+
obj.data{barIndex}.xaxis = "x" + xSource;
59+
obj.data{barIndex}.yaxis = "y" + ySource;
6160

62-
%-------------------------------------------------------------------------%
61+
%---------------------------------------------------------------------%
6362

6463
%-set trace-%
65-
obj.data{barIndex}.type = 'bar';
64+
obj.data{barIndex}.type = "bar";
6665
obj.data{barIndex}.name = barData.DisplayName;
67-
obj.data{barIndex}.visible = strcmp(barData.Visible,'on');
66+
obj.data{barIndex}.visible = strcmp(barData.Visible,"on");
6867

69-
%-------------------------------------------------------------------------%
68+
%---------------------------------------------------------------------%
7069

7170
%-set plot data-%
7271
xData = barData.XData;
7372
yData = barData.YData;
7473

75-
if isduration(xData) || isdatetime(xData), xData = datenum(xData); end
76-
if isduration(yData) || isdatetime(yData), yData = datenum(yData); end
74+
if isduration(xData) || isdatetime(xData)
75+
xData = datenum(xData);
76+
end
77+
if isduration(yData) || isdatetime(yData)
78+
yData = datenum(yData);
79+
end
7780

7881
switch barData.Horizontal
79-
case 'off'
80-
obj.data{barIndex}.orientation = 'v';
82+
case "off"
83+
obj.data{barIndex}.orientation = "v";
8184
obj.data{barIndex}.x = xData;
8285
obj.data{barIndex}.y = yData;
83-
84-
case 'on'
85-
obj.data{barIndex}.orientation = 'h';
86+
case "on"
87+
obj.data{barIndex}.orientation = "h";
8688
obj.data{barIndex}.x = yData;
8789
obj.data{barIndex}.y = xData;
8890
end
8991

90-
%-------------------------------------------------------------------------%
92+
%---------------------------------------------------------------------%
9193

9294
%-trace settings-%
9395
markerline = extractAreaLine(barData);
9496

9597
obj.data{barIndex}.marker = extractAreaFace(barData);
9698
obj.data{barIndex}.marker.line = markerline;
9799

98-
%-------------------------------------------------------------------------%
100+
%---------------------------------------------------------------------%
99101

100102
%-layout settings-%
101103
obj.layout.bargroupgap = 1-barData.BarWidth;
102104

103-
try
104-
obj.layout.bargap = obj.layout.bargap + 0.0625;
105-
catch
106-
obj.layout.bargap = 0.0625;
105+
bars = findobj(obj.State.Plot(barIndex).AssociatedAxis.Children, ...
106+
"Type", "Bar");
107+
nBar = sum({bars.BarLayout}=="grouped");
108+
if nBar > 1
109+
obj.layout.bargap = 0.2;
110+
else
111+
obj.layout.bargap = 0;
107112
end
108113

109114
switch barData.BarLayout
110-
case 'grouped'
111-
obj.layout.barmode = 'group';
112-
case 'stacked'
113-
obj.layout.barmode = 'relative';
115+
case "grouped"
116+
obj.layout.barmode = "group";
117+
case "stacked"
118+
obj.layout.barmode = "relative";
114119
end
115120

116-
%-------------------------------------------------------------------------%
121+
%---------------------------------------------------------------------%
117122

118123
%-bar showlegend-%
119124
leg = barData.Annotation;
120125
legInfo = leg.LegendInformation;
121126

122127
switch legInfo.IconDisplayStyle
123-
case 'on'
128+
case "on"
124129
showleg = true;
125-
case 'off'
130+
case "off"
126131
showleg = false;
127132
end
128-
129133
obj.data{barIndex}.showlegend = showleg;
130-
131-
%-------------------------------------------------------------------------%
132134
end

0 commit comments

Comments
 (0)