Skip to content

Commit 6cff239

Browse files
Refactor updateTernaryColorbar.m
1 parent b54a1d6 commit 6cff239

File tree

1 file changed

+73
-108
lines changed

1 file changed

+73
-108
lines changed

plotly/plotlyfig_aux/core/updateTernaryColorbar.m

Lines changed: 73 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,16 @@
3838
try
3939
colorbarData = obj.State.Colorbar(colorbarIndex).Handle;
4040
catch
41-
disp('could not extract ColorBar data');
41+
disp("could not extract ColorBar data");
4242
end
4343

4444
%-STANDARDIZE UNITS-%
4545
colorbarunits = colorbarData.Units;
46-
obj.State.Colorbar(colorbarIndex).Handle.Units = 'normalized';
46+
obj.State.Colorbar(colorbarIndex).Handle.Units = "normalized";
4747

4848
%-colorbar position-%
49-
colorbar.xanchor = 'left';
50-
colorbar.yanchor = 'bottom';
49+
colorbar.xanchor = "left";
50+
colorbar.yanchor = "bottom";
5151
colorbar.x = colorbarData.Position(1)*1.025;
5252
colorbar.y = colorbarData.Position(2);
5353

@@ -86,44 +86,32 @@
8686
titleFontUnits = colorbarTitleData.FontUnits;
8787
yLabelUnits = colorbarYLabelData.Units;
8888
yLabelFontUnits = colorbarYLabelData.FontUnits;
89-
colorbarTitle.Units = 'data';
90-
colorbarYLabel.Units = 'data';
91-
colorbarYLabel.FontUnits = 'points';
89+
colorbarTitle.Units = "data";
90+
colorbarYLabel.Units = "data";
91+
colorbarYLabel.FontUnits = "points";
9292

9393
if ~isHG2
9494
xLabelUnits = colorbarXLabelData.Units;
9595
xLabelFontUnits = colorbarXLabelData.FontUnits;
96-
colorbarTitle.FontUnits = 'points';
97-
colorbarXLabel.Units = 'data';
98-
colorbarXLabel.FontUnits = 'points';
96+
colorbarTitle.FontUnits = "points";
97+
colorbarXLabel.Units = "data";
98+
colorbarXLabel.FontUnits = "points";
9999
end
100100

101101
if ~isempty(colorbarTitleData.String)
102102
if colorbarTitleData.Rotation == 90
103-
colorbar.titleside = 'right';
103+
colorbar.titleside = "right";
104104
else
105-
colorbar.titleside = 'top';
105+
colorbar.titleside = "top";
106106
end
107107

108-
colorbar.titlefont.family = ...
109-
matlab2plotlyfont(colorbarTitleData.FontName);
110-
col = round(255*colorbarTitleData.Color);
111-
colorbar.titlefont.color = getStringColor(col);
112-
colorbar.titlefont.size = 1.20 * colorbarTitleData.FontSize;
108+
colorbar.titlefont = getFont(colorbarTitleData);
113109
elseif ~isempty(colorbarXLabelData.String)
114-
colorbar.titleside = 'right';
115-
colorbar.titlefont.family = ...
116-
matlab2plotlyfont(colorbarXLabelData.FontName);
117-
col = round(255*colorbarXLabelData.Color);
118-
colorbar.titlefont.color = getStringColor(col);
119-
colorbar.titlefont.size = 1.20 * colorbarXLabelData.FontSize;
110+
colorbar.titleside = "right";
111+
colorbar.titlefont = getFont(colorbarXLabelData);
120112
elseif ~isempty(colorbarYLabelData.String)
121-
colorbar.titleside = 'bottom';
122-
colorbar.titlefont.family = ...
123-
matlab2plotlyfont(colorbarYLabelData.FontName);
124-
col = round(255*colorbarYLabelData.Color);
125-
colorbar.titlefont.color = getStringColor(col);
126-
colorbar.titlefont.size = 1.20 * colorbarYLabelData.FontSize;
113+
colorbar.titleside = "bottom";
114+
colorbar.titlefont = getFont(colorbarYLabelData);
127115
end
128116

129117
%-REVERT UNITS-%
@@ -145,12 +133,12 @@
145133
* obj.layout.width, colorbarData.TickLength(1) ...
146134
* colorbarData.Position(4) * obj.layout.height));
147135

148-
colorbar.thicknessmode = 'fraction';
136+
colorbar.thicknessmode = "fraction";
149137
colorbar.thickness = colorbarData.Position(3);
150138
colorbar.tickwidth = lineWidth;
151139
colorbar.ticklen = tickLength;
152140

153-
colorbar.lenmode = 'fraction';
141+
colorbar.lenmode = "fraction";
154142
colorbar.len = colorbarData.Position(4)*1.025;
155143
colorbar.outlinewidth = lineWidth;
156144

@@ -159,16 +147,16 @@
159147

160148
%-coloration-%
161149
if isHG2
162-
col = round(255*colorbarData.Color);
150+
col = colorbarData.Color;
163151
else
164152
if orientVert
165-
col = round(255*colorbarData.YColor);
153+
col = colorbarData.YColor;
166154
else
167-
col = round(255*colorbarData.XColor);
155+
col = colorbarData.XColor;
168156
end
169157
end
170158

171-
colorbarColor = getStringColor(col);
159+
colorbarColor = getStringColor(round(255*col));
172160

173161
colorbar.outlinecolor = colorbarColor;
174162
colorbar.tickcolor = colorbarColor;
@@ -183,23 +171,21 @@
183171

184172
if isHG2
185173
if isempty(colorbarData.Ticks)
186-
%-hide tick labels-%
187-
colorbar.ticks = '';
174+
colorbar.ticks = "";
188175
colorbar.showticklabels = false;
189176
else
190177
%-tick direction-%
191178
switch colorbarData.TickDirection
192-
case 'in'
193-
colorbar.ticks = 'inside';
194-
case 'out'
195-
colorbar.ticks = 'outside';
179+
case "in"
180+
colorbar.ticks = "inside";
181+
case "out"
182+
colorbar.ticks = "outside";
196183
end
197-
if strcmp(colorbarData.TickLabelsMode,'auto')
184+
if strcmp(colorbarData.TickLabelsMode,"auto")
198185
colorbar.autotick = true;
199186
% nticks = max ticks (so + 1)
200187
colorbar.nticks = length(colorbarData.Ticks) + 1;
201188
else
202-
%-show tick labels-%
203189
if isempty(colorbarData.TickLabels)
204190
colorbar.showticklabels = false;
205191
else
@@ -211,65 +197,37 @@
211197
end
212198
else
213199
if orientVert
214-
if isempty(colorbarData.YTick)
215-
%-show tick labels-%
216-
colorbar.ticks = '';
217-
colorbar.showticklabels = false;
218-
else
219-
%-tick direction-%
220-
switch colorbarData.TickDir
221-
case 'in'
222-
colorbar.ticks = 'inside';
223-
case 'out'
224-
colorbar.ticks = 'outside';
225-
end
226-
if strcmp(colorbarData.YTickLabelMode, 'auto')
227-
colorbar.autotick = true;
228-
%-numticks-%
229-
% nticks = max ticks (so + 1)
230-
colorbar.nticks = length(colorbarData.YTick) + 1;
231-
else
232-
%-show tick labels-%
233-
if isempty(colorbarData.YTickLabel)
234-
colorbar.showticklabels = false;
235-
else
236-
colorbar.autotick = false;
237-
colorbar.tick0 = ...
238-
str2double(colorbarData.YTickLabel(1,:));
239-
colorbar.dtick = ...
240-
str2double(colorbarData.YTickLabel(2,:)) ...
241-
- str2double(colorbarData.YTickLabel(1,:));
242-
end
243-
end
244-
end
200+
tick = colorbarData.YTick;
201+
tickLabel = colorbarData.YTickLabel;
202+
tickLabelMode = colorbarData.YTickLabelMode;
203+
else
204+
tick = colorbarData.XTick;
205+
tickLabel = colorbarData.XTickLabel;
206+
tickLabelMode = colorbarData.XTickLabelMode;
207+
end
208+
209+
if isempty(tick)
210+
colorbar.ticks = "";
211+
colorbar.showticklabels = false;
245212
else
246-
if isempty(colorbarData.XTick)
247-
%-show tick labels-%
248-
colorbar.ticks = '';
249-
colorbar.showticklabels = false;
213+
%-tick direction-%
214+
switch colorbarData.TickDir
215+
case "in"
216+
colorbar.ticks = "inside";
217+
case "out"
218+
colorbar.ticks = "outside";
219+
end
220+
if strcmp(tickLabelMode, "auto")
221+
colorbar.autotick = true;
222+
colorbar.nticks = length(tick) + 1;
250223
else
251-
%-tick direction-%
252-
switch colorbarData.TickDir
253-
case 'in'
254-
colorbar.ticks = 'inside';
255-
case 'out'
256-
colorbar.ticks = 'outside';
257-
end
258-
if strcmp(colorbarData.XTickLabelMode,'auto')
259-
colorbar.autotick = true;
260-
colorbar.nticks = length(colorbarData.XTick) + 1;
224+
if isempty(tickLabel)
225+
colorbar.showticklabels = false;
261226
else
262-
%-show tick labels-%
263-
if isempty(colorbarData.XTickLabel)
264-
colorbar.showticklabels = false;
265-
else
266-
colorbar.autotick = false;
267-
colorbar.tick0 = ...
268-
str2double(colorbarData.XTickLabel(1,:));
269-
colorbar.dtick = ...
270-
str2double(colorbarData.XTickLabel(2,:)) ...
271-
- str2double(colorbarData.XTickLabel(1,:));
272-
end
227+
colorbar.autotick = false;
228+
colorbar.tick0 = str2double(tickLabel(1,:));
229+
colorbar.dtick = str2double(tickLabel(2,:)) ...
230+
- str2double(tickLabel(1,:));
273231
end
274232
end
275233
end
@@ -278,16 +236,16 @@
278236
%-colorbar bg-color-%
279237
if ~isHG2
280238
if ~ischar(colorbarData.Color)
281-
col = round(255*colorbarData.Color);
239+
col = colorbarData.Color;
282240
else
283-
col = round(255*figureData.Color);
241+
col = figureData.Color;
284242
end
285243

286-
obj.layout.plot_bgcolor = getStringColor(col);
244+
obj.layout.plot_bgcolor = getStringColor(round(255*col));
287245
end
288246

289247
%-ASSOCIATED DATA-%
290-
if isfield(colorbarData.UserData,'dataref')
248+
if isfield(colorbarData.UserData,"dataref")
291249
colorbarDataIndex = colorbarData.UserData.dataref;
292250
else
293251
colorbarDataIndex = findColorbarData(obj,colorbarIndex);
@@ -296,19 +254,18 @@
296254
if (nticks ~= 0)
297255
colorIndex = linspace(0, 1, nticks);
298256
colorData = linspace(0, 1, nticks-1);
299-
m = 1;
300257

258+
colorscale = cell(1:2*(nticks-1));
301259
for n = 1:nticks-1
302260
col = 1-colorData(n);
303-
colorscale{m} = {colorIndex(n), ...
261+
colorscale{2*n-1} = {colorIndex(n), ...
304262
getStringColor(round(255*[col, col, col]))};
305-
colorscale{m+1} = {colorIndex(n+1), ...
263+
colorscale{2*n} = {colorIndex(n+1), ...
306264
getStringColor(round(255*[col, col, col]))};
307-
m = 2*n+1;
308265
end
309266
obj.data{colorbarDataIndex}.marker.color = colorbarData.Ticks;
310267
else
311-
colorscale = {{0, 'rgb(255,255,255)'}, {1, 'rgb(0,0,0)'}};
268+
colorscale = {{0, "rgb(255,255,255)"}, {1, "rgb(0,0,0)"}};
312269
end
313270

314271
obj.data{colorbarDataIndex}.marker.colorscale = colorscale;
@@ -318,3 +275,11 @@
318275
%-REVERT UNITS-%
319276
obj.State.Colorbar(colorbarIndex).Handle.Units = colorbarunits;
320277
end
278+
279+
function out = getFont(labelData)
280+
out = struct( ...
281+
"family", matlab2plotlyfont(labelData.FontName), ...
282+
"color", getStringColor(round(255*labelData.Color)), ...
283+
"size", 1.20 * labelData.FontSize ...
284+
);
285+
end

0 commit comments

Comments
 (0)