Skip to content

Commit 6401625

Browse files
Refactor extractAxisData
1 parent 24f1c50 commit 6401625

File tree

1 file changed

+23
-63
lines changed

1 file changed

+23
-63
lines changed

plotly/plotlyfig_aux/helpers/extractAxisData.m

Lines changed: 23 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,7 @@
33
% axisData is the data extracted from the figure, axisName take the
44
% values "x" "y" or "z"
55

6-
%=====================================================================%
7-
%
8-
% AXIS INITIALIZATION
9-
%
10-
%=====================================================================%
11-
12-
%-general axis settings-%
13-
axisColor = round(255 * axisData.(axisName + "Color"));
14-
axisColor = getStringColor(axisColor);
6+
axisColor = getStringColor(round(255 * axisData.(axisName + "Color")));
157
lineWidth = max(1, ...
168
axisData.LineWidth*obj.PlotlyDefaults.AxisLineIncreaseFactor);
179

@@ -22,27 +14,28 @@
2214
exponentFormat = 0;
2315
end
2416

25-
axis.side = axisData.(axisName + "AxisLocation");
26-
axis.zeroline = false;
27-
axis.autorange = false;
28-
axis.linecolor = axisColor;
29-
axis.linewidth = lineWidth;
30-
axis.exponentformat = obj.PlotlyDefaults.ExponentFormat;
31-
32-
%-general tick settings-%
33-
tickRotation = axisData.(axisName + "TickLabelRotation");
34-
tickLength = min(obj.PlotlyDefaults.MaxTickLength,...
35-
max(axisData.TickLength(1)*axisData.Position(3)*obj.layout.width,...
17+
tickLength = min(obj.PlotlyDefaults.MaxTickLength, ...
18+
max(axisData.TickLength(1)*axisData.Position(3)*obj.layout.width, ...
3619
axisData.TickLength(1)*axisData.Position(4)*obj.layout.height));
3720

38-
axis.tickfont.size = axisData.FontSize;
39-
axis.tickfont.family = matlab2plotlyfont(axisData.FontName);
40-
axis.tickfont.color = axisColor;
41-
42-
axis.ticklen = tickLength;
43-
axis.tickcolor = axisColor;
44-
axis.tickwidth = lineWidth;
45-
axis.tickangle = -tickRotation;
21+
axis = struct(...
22+
"side", axisData.(axisName + "AxisLocation"), ...
23+
"zeroline", false, ...
24+
"autorange", false, ...
25+
"linecolor", axisColor, ...
26+
"linewidth", lineWidth, ...
27+
"exponentformat", obj.PlotlyDefaults.ExponentFormat, ...
28+
"tickfont", struct( ...
29+
"size", axisData.FontSize, ...
30+
"family", matlab2plotlyfont(axisData.FontName), ...
31+
"color", axisColor ...
32+
), ...
33+
"ticklen", tickLength, ...
34+
"tickcolor", axisColor, ...
35+
"tickwidth", lineWidth, ...
36+
"tickangle", -axisData.(axisName + "TickLabelRotation"), ...
37+
"type", axisData.(axisName + "Scale") ...
38+
);
4639

4740
switch axisData.TickDir
4841
case "in"
@@ -51,35 +44,22 @@
5144
axis.ticks = "outside";
5245
end
5346

54-
%-set axis grid-%
5547
isGrid = axisData.(axisName + "Grid");
5648
isMinorGrid = axisData.(axisName + "MinorGrid");
57-
5849
if strcmp(isGrid, "on") || strcmp(isMinorGrid, "on")
5950
axis.showgrid = true;
6051
axis.gridwidth = lineWidth;
6152
else
6253
axis.showgrid = false;
6354
end
6455

65-
%-axis grid color-%
6656
if isprop(axisData, "GridColor") && isprop(axisData, "GridAlpha")
6757
axis.gridcolor = getStringColor( ...
6858
round(255*axisData.GridColor), axisData.GridAlpha);
6959
else
7060
axis.gridcolor = axisColor;
7161
end
7262

73-
%-axis type-%
74-
axis.type = axisData.(axisName + "Scale");
75-
76-
%=====================================================================%
77-
%
78-
% SET TICK LABELS
79-
%
80-
%=====================================================================%
81-
82-
%-get tick label data-%
8363
tickLabels = axisData.(axisName + "TickLabel");
8464
tickValues = axisData.(axisName + "Tick");
8565

@@ -96,8 +76,7 @@
9676
end
9777
assert(isequal(numel(tickLabels),numel(tickValues)));
9878

99-
%-there is not tick label case-%
100-
if isempty(tickValues)
79+
if isempty(tickValues) % There are no tick labels
10180
axis.ticks = "";
10281
axis.showticklabels = false;
10382
axis.autorange = true;
@@ -108,8 +87,7 @@
10887
case "off"
10988
axis.mirror = false;
11089
end
111-
else %-there is tick labels case-%
112-
%-set tick values-%
90+
else % There are tick labels
11391
axis.showticklabels = true;
11492
axis.tickmode = "array";
11593

@@ -146,14 +124,8 @@
146124
axis.type = "date";
147125
if isprop(axisData, "XTickLabelMode") ...
148126
&& isequal(axisData.XTickLabelMode, "auto")
149-
% default matlab xticks are unreliable for datetime. eg.
150-
% fig = figure(visible="off");
151-
% dt = datetime(2013,3,2):datetime(2020,1,1);
152-
% plot(dt,randi([-10 10],numel(dt),1)');
153-
% isequal(numel(fig.Children.XTick),numel(fig.Children.XTickLabel)) % returns false
154127
axis.autotick = true;
155128
tickLabels = {};
156-
tickValues = []; %#ok<NASGU>
157129
end
158130

159131
elseif iscategorical(axisLim)
@@ -163,42 +135,31 @@
163135
axis.autorange = true;
164136
end
165137

166-
%-box setting-%
167138
switch axisData.Box
168139
case "on"
169140
axis.mirror = "ticks";
170141
case "off"
171142
axis.mirror = false;
172143
end
173144

174-
%-set tick labels by using tick texts-%
175145
if ~isempty(tickLabels)
176146
axis.ticktext = tickLabels;
177147
end
178148
end
179149

180-
%-axis direction-%
181150
axisDirection = axisData.(axisName + "Dir");
182151

183152
if strcmp(axisDirection, "reverse")
184153
axis.range = [axis.range(2) axis.range(1)];
185154
end
186155

187-
%=====================================================================%
188-
%
189-
% SET AXIS LABEL
190-
%
191-
%=====================================================================%
192-
193-
%-get label data-%
194156
label = axisData.(axisName + "Label");
195157
labelData = label;
196158

197159
%-STANDARDIZE UNITS-%
198160
fontunits = label.FontUnits;
199161
label.FontUnits = "points";
200162

201-
%-title label settings-%
202163
if ~isempty(labelData.String)
203164
axis.title = parseString(labelData.String,labelData.Interpreter);
204165
end
@@ -210,7 +171,6 @@
210171
%-REVERT UNITS-%
211172
label.FontUnits = fontunits;
212173

213-
%-set visibility conditions-%
214174
if strcmp(axisData.Visible, "on")
215175
axis.showline = true;
216176
else

0 commit comments

Comments
 (0)