Skip to content

Commit bffcbaf

Browse files
fix issue when there is multiple legends
1 parent 6739f29 commit bffcbaf

File tree

2 files changed

+134
-3
lines changed

2 files changed

+134
-3
lines changed

plotly/plotlyfig.m

+7-3
Original file line numberDiff line numberDiff line change
@@ -761,10 +761,14 @@ function validate(obj)
761761
end
762762

763763
% update legends
764-
for n = 1:obj.State.Figure.NumLegends
765-
if ~strcmpi(obj.PlotOptions.TreatAs, 'pie3')
766-
updateLegend(obj,n);
764+
if obj.State.Figure.NumLegends < 2
765+
for n = 1:obj.State.Figure.NumLegends
766+
if ~strcmpi(obj.PlotOptions.TreatAs, 'pie3')
767+
updateLegend(obj,n);
768+
end
767769
end
770+
else
771+
updateLegendMultipleAxes(obj,1);
768772
end
769773

770774
% update colorbars
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
function obj = updateLegendMultipleAxes(obj, legIndex)
2+
3+
% x: ...[DONE]
4+
% y: ...[DONE]
5+
% traceorder: ...[DONE]
6+
% font: ...[DONE]
7+
% bgcolor: ...[DONE]
8+
% bordercolor: ...[DONE]
9+
% borderwidth: ...[DONE]
10+
% xref: ...[DONE]
11+
% yref: ...[DONE]
12+
% xanchor: ...[DONE]
13+
% yanchor: ...[DONE]
14+
15+
%=========================================================================%
16+
%
17+
%-GET NECESSARY INFO FOR MULTIPLE LEGENDS-%
18+
%
19+
%=========================================================================%
20+
21+
for traceIndex = 1:obj.State.Figure.NumPlots
22+
allNames{traceIndex} = obj.data{traceIndex}.name;
23+
allShowLegens(traceIndex) = obj.data{traceIndex}.showlegend;
24+
obj.data{traceIndex}.showlegend = false;
25+
obj.data{traceIndex}.legendgroup = obj.data{traceIndex}.name;
26+
27+
axIndex = obj.getAxisIndex(obj.State.Plot(traceIndex).AssociatedAxis);
28+
[xSource, ySource] = findSourceAxis(obj, axIndex);
29+
xAxis = eval(sprintf('obj.layout.xaxis%d', xSource));
30+
yAxis = eval(sprintf('obj.layout.yaxis%d', xSource));
31+
32+
allDomain(traceIndex, 1) = max(xAxis.domain);
33+
allDomain(traceIndex, 2) = max(yAxis.domain);
34+
end
35+
36+
[~, groupIndex] = unique(allNames);
37+
38+
for traceIndex = groupIndex'
39+
obj.data{traceIndex}.showlegend = allShowLegens(traceIndex);
40+
end
41+
42+
%-------------------------------------------------------------------------%
43+
44+
%-STANDARDIZE UNITS-%
45+
legendUnits = get(obj.State.Legend(legIndex).Handle,'Units');
46+
fontUnits = get(obj.State.Legend(legIndex).Handle,'FontUnits');
47+
set(obj.State.Legend(legIndex).Handle,'Units','normalized');
48+
set(obj.State.Legend(legIndex).Handle,'FontUnits','points');
49+
50+
%-------------------------------------------------------------------------%
51+
52+
%-LEGEND DATA STRUCTURE-%
53+
legendData = get(obj.State.Legend(legIndex).Handle);
54+
55+
% only displays last legend as global Plotly legend
56+
obj.layout.legend = struct();
57+
58+
%-------------------------------------------------------------------------%
59+
60+
%-layout showlegend-%
61+
obj.layout.showlegend = strcmpi(legendData.Visible,'on');
62+
63+
%-------------------------------------------------------------------------%
64+
65+
%-legend (x,y) coordenates-%
66+
obj.layout.legend.x = 1.005 * max(allDomain(:,1));
67+
obj.layout.legend.y = 1.001 * max(allDomain(:,2));;
68+
69+
%-legend (x,y) refs-%
70+
obj.layout.legend.xref = 'paper';
71+
obj.layout.legend.yref = 'paper';
72+
73+
%-legend (x,y) anchors-%
74+
obj.layout.legend.xanchor = 'left';
75+
obj.layout.legend.yanchor = 'top';
76+
77+
%-------------------------------------------------------------------------%
78+
79+
if (strcmp(legendData.Box,'on') && strcmp(legendData.Visible, 'on'))
80+
81+
%---------------------------------------------------------------------%
82+
83+
%-legend traceorder-%
84+
obj.layout.legend.traceorder = 'normal';
85+
86+
%---------------------------------------------------------------------%
87+
88+
%-legend borderwidth-%
89+
obj.layout.legend.borderwidth = legendData.LineWidth;
90+
91+
%---------------------------------------------------------------------%
92+
93+
%-legend bordercolor-%
94+
col = 255*legendData.EdgeColor;
95+
obj.layout.legend.bordercolor = sprintf('rgb(%f,%f,%f)', col);
96+
97+
%---------------------------------------------------------------------%
98+
99+
%-legend bgcolor-%
100+
col = 255*legendData.Color;
101+
obj.layout.legend.bgcolor = sprintf('rgb(%f,%f,%f)', col);
102+
103+
%---------------------------------------------------------------------%
104+
105+
%-legend font size-%
106+
obj.layout.legend.font.size = legendData.FontSize;
107+
108+
%---------------------------------------------------------------------%
109+
110+
%-legend font family-%
111+
obj.layout.legend.font.family = matlab2plotlyfont(legendData.FontName);
112+
113+
%---------------------------------------------------------------------%
114+
115+
%-legend font colour-%
116+
col = 255*legendData.TextColor;
117+
obj.layout.legend.font.color = sprintf('rgb(%f,%f,%f)', col);
118+
119+
end
120+
121+
%-------------------------------------------------------------------------%
122+
123+
%-REVERT UNITS-%
124+
set(obj.State.Legend(legIndex).Handle,'Units',legendUnits);
125+
set(obj.State.Legend(legIndex).Handle,'FontUnits',fontUnits);
126+
127+
end

0 commit comments

Comments
 (0)