-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathresizeLegend.m
More file actions
331 lines (294 loc) · 10.9 KB
/
Copy pathresizeLegend.m
File metadata and controls
331 lines (294 loc) · 10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
function [HLeg,HLegBack,HAxBack] = resizeLegend(varargin)
% Goes to extreme lengths to create a shortened legend.
%
% @Synopsis
% resizeLegend()
% Does the above on the current axes for all plot elements.
%
% resizeLegend(HAx)
% Creates the legend on the given axes.
%
% resizeLegend([HAx],'Name','Value')
% Let's you adjust several other parameters.
%
% [HLeg,HLegBack,HAxBack] = resizeLegend(...)
% Returns the handles to the foreground legend and the background axes and
% legend.
%
% @Input
% HAx (optional)
% A valid handle to an existing axes. If empty, the default is used.
% DEFAULT: gca()
%
% 'LegendEntries',LegendEntries (Name/Value)
% A cellstring with the display names, an array of handles for the plot
% components which should be entered in the legend (as used by
% LEGEND(entries)), simply 'show' (as used in LEGEND('show') or empty (in
% which case the current legend is used as source for the entries).
% DEFAULT: 'show' (all plot components)
%
% 'Scale',Scale (Name/Value)
% A scalar factor >0 and <1 for which the legend entries should be shortened
% (e.g. if a line is present in the legend (X1,X2) -> Scale*(X1,X2)). Values
% below 0.3 will probably result in too short line segments.
% DEFAULT: 0.5
%
% 'LegendProperties',LegendProperties (Name/Value)
% A scalar struct with legend properties as passed to SET(HLegend,Props).
% The struct must not contain the fields 'Box', 'Orientation', 'Parent',
% 'String' (use the input argument 'LegendEntries' instead) or 'Visible'. It
% must also not contain a 'Position' and a 'Location' property
% simultanously. If you specify the 'Position' property keep in mind that
% the width and height of the legend will change and exact positioning
% w.r.t. the righ/top corner will not be kept.
% DEFAULT: struct()
%
% @Output
% HLeg
% A handle to the created legend.
%
% HLegBack
% A handle to the axes used as background for the legend. Its size is the
% resized legend. Its 'Tag' property is set to 'LegendBackground'.
%
% HAxBack
% A handle to the axes used as background for the front-axes. Use this for
% displaying a grid. Its 'Tag' property is set to 'AxesBackground'.
%
% @Remarks
% - All input parameters can be passed as a struct (see
% INPUTPARSER.STRUCTEXPAND).
% - Minor & major grid have to be set on back-axis.
% - For exact positioning, use HAxBack after calling RESIZELEGEND.
%
% @Dependencies
% none;
%
% See also LEGEND
%
% ML-FEX ID:58914
%
% @Changelog
% 2016-08-29 (DJM): Beta release.
% 2016-09-08 (DJM):
% - Now correclty treats legend entries of groups.
% - Now correclty identifies the legend with mulitple plots in a figure.
% - Does not change label font weights anymore.
ArgsOut = {'HLeg', 'HLegBack', 'HAxBack'};
nArgOut = nargout();
%% Check inputs
% Variable : Type : Acceptable Values : Default
% -----------------:----------:-------------------------:--------
% HAx : Optional : valid axes handle : gca()
% LegendEntries : Name/Val : nonempty : 'show'
% Scale : Name/Val : numeric scalar in (0,1) : 0.5
% LegendProperties : Name/Val : struct nonempty : struct()
%Create INPUTPARSER with the passing of name/value arguments as struct enabled.
IP = inputParser();
IP.StructExpand = true;
%Validation functions.
validateHandleValid = @(X)any([isempty(X) ~isempty(X)&ishandle(X)]);
validateStructScaNonempt = @(X)validateattributes(X,{'struct'},{'scalar','nonempty'});
validateNumScaInRange = @(X)validateattributes(X,{'numeric'},{'scalar','>',0,'<',1});
%Define inputs.
IP.addOptional( 'HAx' ,[] ,validateHandleValid);
IP.addParameter('LegendEntries' ,'show');
IP.addParameter('Scale' ,0.5 ,validateNumScaInRange);
IP.addParameter('LegendProperties',struct(),validateStructScaNonempt);
%Parse and validate all input arguments.
IP.parse(varargin{:});
%Assign optional & param/val arguments.
HAx = IP.Results.HAx;
LegEntries = IP.Results.LegendEntries;
Scale = IP.Results.Scale;
LegProps = IP.Results.LegendProperties;
%Check axes & figure handle.
if isempty(HAx)
HAx = gca();
end
HFig = HAx.Parent;
%Check if legend entries is cellstring.
if ~strcmpi(LegEntries,'show')
if iscellstr(LegEntries)
assert(all(cellfun(@(X) ischar(X),LegEntries)), ...
'Legend entries must be a cell array of strings or an array of handles!');
elseif isempty(LegEntries)
HLeg = findobj(HFig.Children,'Type','legend');
assert(ishghandle(HLeg), ['When legend entries are empty, the figure ' ...
'must have a valid legend!']);
LegEntries = HLeg.String;
delete(HLeg);
else
assert(all(ishghandle(LegEntries)), ['Legend entries must be a cell ' ...
'array of strings or an array of graphics object handles!']);
end
end
%Check legend properties
for Field = {'Box','Orientation','Parent','String','Visible'}
assert(~isfield(LegProps,Field{:}), ...
'The ''%s'' property must not be specified in ''LegendProperties''!', ...
Field{:});
end
assert(~(isfield(LegProps,'Position') & isfield(LegProps,'Location')), ...
['The ''Position'' and the ''Location'' property must not both be ' ...
'specified in ''LegendProperties''!']);
LegProps.Box = 'off';
LegProps.Parent = HFig;
LegProps.Visible = 'off';
%% Create background axes
HAxBack = copyobj(HAx,HFig);
uistack(HAxBack,'bottom');
cla(HAxBack);
set(HAxBack, 'Box','off', 'NextPlot','add', 'Tag','AxesBackground', ...
'TickLength',[0 0], 'Title',text(), ...
'XColor','none','YColor','none','ZColor','none', ...
'XLabel',text(),'YLabel',text(),'ZLabel',text(), ...
'XTickLabel','','YTickLabel','','ZTickLabel','');
%Make foreground axes transparent.
set(HAx,'Color','none','GridColor','none');
grid(HAx,'off');
%% Create legend
[HLeg,HIcons] = legend(LegEntries);
set(HLeg, LegProps);
%Get handles to the legend elements (use '-not' since ML creates a second, empty
%line segment for entries with no markers; thx to Sandro).
HLines = findobj(HIcons,'Type','line', '-and','Marker' ,'none', ...
'-not','LineStyle','none');
HMarkers = findobj(HIcons,'Type','line', '-not','Marker' ,'none', ...
'-and','LineStyle','none');
HTexts = findobj(HIcons,'Type','text');
%Check if HIcons contains a group of objects, in which case the tag of the
%lines/markers are empty.
if ~isempty(HLines)
if isempty([HLines.Tag])
set(HLines,{'Tag'},get(HTexts,'String'));
end
end
if ~isempty(HMarkers)
if isempty([HMarkers.Tag])
set(HMarkers,{'Tag'},get(HTexts,'String'));
end
end
%Store handles in the UserData.
HLeg.UserData.HAxes = HAx;
HLeg.UserData.HLines = HLines;
HLeg.UserData.HTexts = HTexts;
HLeg.UserData.HMarkers = HMarkers;
%% Resize the legend elements
nEntries = length(HLeg.String);
MarkerTags = get(HMarkers,'Tag');
for i = 1:nEntries
if i <= length(HLines)
HLines(i).XData = Scale*HLines(i).XData;
IsMarker = ismember(MarkerTags, HLines(i).Tag);
else
IsMarker = false(size(MarkerTags));
end
if i <= length(HMarkers)
if any(IsMarker)
HMarkers(i).XData = mean(HLines(IsMarker).XData);
else
HMarkers(i).XData = Scale*HMarkers(i).XData;
end
end
if i <= length(HTexts)
HTexts(i).Position(1) = Scale*HTexts(i).Position(1);
end
end
%Compute the correct size of the resized legend.
Size = getLegSize(HLeg);
%% Create legend background
%Create legend background as third axes.
HLegBack = copyobj(HAxBack,HFig);
set(HLegBack, 'Box','off',...
'GridColor','none',...
'Units','normalized',...
'Position',Size,...
'Tag','LegendBackground',...
'Visible','off',...
'XColor','none',...
'YColor','none',...
'ZColor','none');
grid(HLegBack,'off');
uistack(HLegBack,'bottom');
uistack(HLegBack,'up',1);
%Store handle in the UserData.
HLeg.UserData.HLegBack = HLegBack;
%% Link properties
set([HAx;HAxBack;HLeg],'Units','normalized');
%Add callback functions for size recomputation if changed.
HFig.SizeChangedFcn = @sizeChangedCallback;
addlistener(HLeg,'Position','PostSet',@sizeChangedCallback);
linkprop([HAx;HAxBack],{'ActivePositionProperty',...
'GridLineStyle',...
'LineWidth',...
'MinorGridLineStyle',...
'OuterPosition',...
'Parent',...
'Position',...
'Units',...
'View',...
'XAxisLocation','YAxisLocation',...
'XDir','YDir','ZDir',...
'XLim','YLim','ZLim',...
'XMinorTick','YMinorTick','ZMinorTick',...
'XScale','YScale','ZScale',...
'XTick','YTick','ZTick'});
linkprop([HLeg;HLegBack], {'LineWidth','Units','Visible'});
%Make legend visible.
set([HLeg;HLegBack], 'Visible','on');
%% Check output
if nArgOut < length(ArgsOut)
clearvars(ArgsOut{nArgOut+1:end});
end
end %RESIZELEGEND
%% SUBFUNCTION: getLegSize
function Size = getLegSize(HLeg)
% Computes the minimum size of the legend.
% @Input
% HLeg (required): Handle to the legend.
%
% @Output
% Size: New size in normalized units w.r.t. the figure as used in the
% 'Position' property of the legend.
%Set units to points to accomodate margin.
HFig = HLeg.Parent;
Units = get([HFig;HLeg],'Units');
set([HFig;HLeg],'Units','points');
ExtentsInPts = vertcat(HLeg.UserData.HTexts.Extent);
XSiz = inf;
if ~isempty(HLeg.UserData.HLines)
XSiz = min([HLeg.UserData.HLines.XData, XSiz]);
end
if ~isempty(HLeg.UserData.HMarkers)
XSiz = min([HLeg.UserData.HMarkers.XData, XSiz]);
end
if ~isfinite(XSiz)
XSiz = 0;
end
Size = [XSiz ... %Min of lines & markers
ExtentsInPts(end,2) ... %Extent of last entry
max(sum(ExtentsInPts(:,[1 3]),2)) ... %Max extent of all entries
sum(ExtentsInPts(1,[2 4]),2)]; %Max extent of first entry
%Convert reference frame to full axes & adjust for margin.
Size = Size.*HLeg.Position([3 4 3 4]);
Size(1:2) = HLeg.Position(1:2) + Size(1:2) ...
- max([HLeg.UserData.HTexts.Margin])/2;
%Convert units to normalized w.r.t. the figure.
Size = Size./HLeg.Parent.Position([3 4 3 4]);
set([HFig;HLeg],{'Units'},Units);
end %GETLEGSIZE
%% SUBFUNCTION: sizeChangedCallback
function sizeChangedCallback(~,~)
% Recomputes the position of the legend and its background if they have changed.
HFig = gcf;
HLegs = findobj(HFig,'Type','legend');
for i = 1:length(HLegs)
if ~isempty(HLegs(i).UserData) && isequal(gca,HLegs(i).UserData.HAxes)
Units = get(HLegs(i).UserData.HLegBack, 'Units');
set(HLegs(i).UserData.HLegBack, 'Units','normalized');
HLegs(i).UserData.HLegBack.Position = getLegSize(HLegs(i));
set(HLegs(i).UserData.HLegBack, 'Units',Units);
end
end
end %SIZECHANGEDCALLBACK