Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4966523

Browse files
Refactor plotlyfig
1 parent 27aeb3b commit 4966523

File tree

1 file changed

+67
-84
lines changed

1 file changed

+67
-84
lines changed

plotly/plotlyfig.m

Lines changed: 67 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -35,28 +35,28 @@
3535
obj.UserData.Verbose = true;
3636

3737
obj.PlotOptions = struct( ...
38-
'CleanFeedTitle', true, ...
39-
'FileName', '', ...
40-
'FileOpt', 'new', ...
41-
'WorldReadable', true, ...
42-
'ShowURL', true, ...
43-
'OpenURL', true, ...
44-
'Strip', false, ...
45-
'WriteFile', true, ...
46-
'Visible', 'on', ...
47-
'TriangulatePatch', false, ...
48-
'StripMargins', false, ...
49-
'TreatAs', {{'_'}}, ...
50-
'Image3D', false, ...
51-
'ContourProjection', false, ...
52-
'AxisEqual', false, ...
53-
'AspectRatio', [], ...
54-
'CameraEye', [], ...
55-
'is_headmap_axis', false, ...
56-
'FrameDuration', 1, ... % in ms.
57-
'FrameTransitionDuration', 0, ... % in ms.
58-
'geoRenderType', 'geo', ...
59-
'DomainFactor', [1 1 1 1] ...
38+
"CleanFeedTitle", true, ...
39+
"FileName", '', ...
40+
"FileOpt", 'new', ...
41+
"WorldReadable", true, ...
42+
"ShowURL", true, ...
43+
"OpenURL", true, ...
44+
"Strip", false, ...
45+
"WriteFile", true, ...
46+
"Visible", 'on', ...
47+
"TriangulatePatch", false, ...
48+
"StripMargins", false, ...
49+
"TreatAs", {{'_'}}, ...
50+
"Image3D", false, ...
51+
"ContourProjection", false, ...
52+
"AxisEqual", false, ...
53+
"AspectRatio", [], ...
54+
"CameraEye", [], ...
55+
"is_headmap_axis", false, ...
56+
"FrameDuration", 1, ... % in ms.
57+
"FrameTransitionDuration", 0, ... % in ms.
58+
"geoRenderType", 'geo', ...
59+
"DomainFactor", [1 1 1 1] ...
6060
);
6161

6262
% offline options
@@ -83,46 +83,46 @@
8383
end
8484

8585
obj.PlotlyDefaults = struct( ...
86-
'MinTitleMargin', 10, ...
87-
'TitleHeight', 0.01, ...
88-
'TitleFontSizeIncrease', 40, ...
89-
'FigureIncreaseFactor', 1.5, ...
90-
'AxisLineIncreaseFactor', 1.5, ...
91-
'MarginPad', 0, ...
92-
'MaxTickLength', 20, ...
93-
'ExponentFormat', 'none', ...
94-
'ErrorbarWidth', 6, ...
95-
'ShowBaselineLegend', false, ...
96-
'Bargap', 0, ...
97-
'CaptionMarginIncreaseFactor', 1.2, ...
98-
'MinCaptionMargin', 80, ...
99-
'IsLight', false, ...
100-
'isGeoaxis', false, ...
101-
'isTernary', false ...
86+
"MinTitleMargin", 10, ...
87+
"TitleHeight", 0.01, ...
88+
"TitleFontSizeIncrease", 40, ...
89+
"FigureIncreaseFactor", 1.5, ...
90+
"AxisLineIncreaseFactor", 1.5, ...
91+
"MarginPad", 0, ...
92+
"MaxTickLength", 20, ...
93+
"ExponentFormat", 'none', ...
94+
"ErrorbarWidth", 6, ...
95+
"ShowBaselineLegend", false, ...
96+
"Bargap", 0, ...
97+
"CaptionMarginIncreaseFactor", 1.2, ...
98+
"MinCaptionMargin", 80, ...
99+
"IsLight", false, ...
100+
"isGeoaxis", false, ...
101+
"isTernary", false ...
102102
);
103103

104104
obj.State = struct( ...
105-
'Axis', [], ...
106-
'Plot', [], ...
107-
'Text', [], ...
108-
'Legend', [], ...
109-
'Colorbar', [], ...
105+
"Axis", [], ...
106+
"Plot", [], ...
107+
"Text", [], ...
108+
"Legend", [], ...
109+
"Colorbar", [], ...
110110
... % figure object management
111-
'Figure', struct( ...
112-
'NumAxes', 0, ...
113-
'NumPlots', 0, ...
114-
'NumLegends', 0, ...
115-
'NumColorbars', 0, ...
116-
'NumTexts', 0 ...
111+
"Figure", struct( ...
112+
"NumAxes", 0, ...
113+
"NumPlots", 0, ...
114+
"NumLegends", 0, ...
115+
"NumColorbars", 0, ...
116+
"NumTexts", 0 ...
117117
) ...
118118
);
119119

120120
obj.PlotlyReference = [];
121121

122122
obj.InitialState = struct( ...
123-
'Username', obj.UserData.Username, ...
124-
'ApiKey', obj.UserData.ApiKey, ...
125-
'PlotlyDomain', obj.UserData.PlotlyDomain ...
123+
"Username", obj.UserData.Username, ...
124+
"ApiKey", obj.UserData.ApiKey, ...
125+
"PlotlyDomain", obj.UserData.PlotlyDomain ...
126126
);
127127

128128
[fig_han,updatekey,noFig] = obj.parseInputs(varargin);
@@ -149,14 +149,9 @@
149149
end
150150

151151
if ~noFig
152-
% add figure listeners
153152
addlistener(obj.State.Figure.Handle,'Visible','PostSet',@(src,event)updateFigureVisible(obj,src,event));
154153
addlistener(obj.State.Figure.Handle,'Name','PostSet',@(src,event)updateFigureName(obj,src,event));
155-
156-
% add plot options listeners
157154
addlistener(obj,'PlotOptions','PostSet',@(src,event)updatePlotOptions(obj,src,event));
158-
159-
% add user data listeners
160155
addlistener(obj,'UserData','PostSet',@(src,event)updateUserData(obj,src,event));
161156
end
162157
end
@@ -193,11 +188,7 @@
193188

194189
% strip the style keys from data
195190
for d = 1:length(obj.data)
196-
if ( ...
197-
strcmpi(obj.data{d}.type, 'scatter') || ...
198-
strcmpi(obj.data{d}.type, 'contour') || ...
199-
strcmpi(obj.data{d}.type, 'bar') ...
200-
)
191+
if contains(lower(obj.data{d}.type), ["scatter" "contour" "bar"])
201192
return
202193
end
203194
obj.data{d} = obj.stripkeys(obj.data{d}, obj.data{d}.type, 'style');
@@ -305,20 +296,20 @@ function validate(obj)
305296

306297
%----ADD A CUSTOM CAPTION-----%
307298
function obj = add_caption(obj, caption_string, varargin)
308-
caption.text = caption_string;
309-
310-
% defaults
311-
caption.xref = 'paper';
312-
caption.yref = 'paper';
313-
caption.xanchor = 'left';
314-
caption.yanchor = 'top';
315-
caption.x = 0.1;
316-
caption.y = -0.05;
317-
caption.showarrow = false;
299+
caption = struct( ...
300+
"text", caption_string, ...
301+
"xref", "paper", ...
302+
"yref", "paper", ...
303+
"xanchor", "left", ...
304+
"yanchor", "top", ...
305+
"x", 0.1, ...
306+
"y", -0.05, ...
307+
"showarrow", false ...
308+
);
318309

319310
% inject any custom annotation specs
320311
for n = 1:2:length(varargin)
321-
caption = setfield(caption, varargin{n}, varargin{n+1});
312+
caption.(varargin{n}) = varargin{n+1};
322313
end
323314

324315
% adjust the bottom margin
@@ -355,7 +346,6 @@ function validate(obj)
355346
% validate keys
356347
validate(obj);
357348

358-
% handle filename
359349
handleFileName(obj);
360350

361351
% handle title (for feed)
@@ -497,10 +487,10 @@ function validate(obj)
497487
plots = findobj(ax(axrev),'-not','Type','Text','-not','Type','axes','-depth',1);
498488

499489
% get number of nbars for pie3
500-
if ismember('pie3', lower(obj.PlotOptions.TreatAs))
490+
if lower(obj.PlotOptions.TreatAs) == "pie3"
501491
obj.PlotOptions.nbars{a} = 0;
502492
for i = 1:length(plots)
503-
if ismember('surface', lower(obj.PlotOptions.TreatAs))
493+
if lower(obj.PlotOptions.TreatAs) == "surface"
504494
obj.PlotOptions.nbars{a} = obj.PlotOptions.nbars{a} + 1;
505495
end
506496
end
@@ -606,16 +596,13 @@ function validate(obj)
606596

607597
%--------------------UPDATE PLOTLY FIGURE---------------------%
608598

609-
% reset dataget(obj.State.Figure.Handle,'Children')
610599
obj.data = {};
611600
obj.PlotOptions.nPlots = obj.State.Figure.NumPlots;
612601
obj.PlotlyDefaults.anIndex = obj.State.Figure.NumTexts;
613602

614-
% reset layout
615603
obj.layout = struct();
616604
obj.layout.annotations = {};
617605

618-
% update figure
619606
updateFigure(obj);
620607

621608
% update axes
@@ -625,7 +612,6 @@ function validate(obj)
625612
end
626613
if ~obj.PlotlyDefaults.isMultipleYAxes(n)
627614
updateAxis(obj,n);
628-
629615
else
630616
for yax = 1:2
631617
updateAxisMultipleYAxes(obj,n,yax);
@@ -660,23 +646,20 @@ function validate(obj)
660646
end
661647
end
662648

663-
% update tiled layout annotations
664649
if isTiledLayout
665650
updateTiledLayoutAnnotation(obj, tiledLayoutStruct);
666651
end
667652

668-
% update legends
669653
if obj.State.Figure.NumLegends < 2
670654
for n = 1:obj.State.Figure.NumLegends
671-
if ~strcmpi(obj.PlotOptions.TreatAs, 'pie3')
655+
if lower(obj.PlotOptions.TreatAs) ~= "pie3"
672656
updateLegend(obj,n);
673657
end
674658
end
675659
else
676660
updateLegendMultipleAxes(obj,1);
677661
end
678662

679-
% update colorbars
680663
for n = 1:obj.State.Figure.NumColorbars
681664
if ~obj.PlotlyDefaults.isTernary
682665
updateColorbar(obj,n);

0 commit comments

Comments
 (0)