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

Skip to content

Commit 6ed9a70

Browse files
Refactor updateContourgroup
1 parent 5e3a4f3 commit 6ed9a70

File tree

3 files changed

+81
-98
lines changed

3 files changed

+81
-98
lines changed

plotly/Test_plotlyfig.m

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -871,13 +871,13 @@ function testContourPlotData(tc)
871871

872872
tc.verifyNumElements(p.data, 1);
873873
tc.verifyEqual(rmfield(p.data{1}, "colorscale"), struct( ...
874-
"type", 'contour', ...
875-
"xaxis", 'x1', ...
876-
"yaxis", 'y1', ...
874+
"type", "contour", ...
875+
"xaxis", "x1", ...
876+
"yaxis", "y1", ...
877877
"name", '', ...
878878
"visible", true, ...
879-
"xtype", 'array', ...
880-
"ytype", 'array', ...
879+
"xtype", "array", ...
880+
"ytype", "array", ...
881881
"x", range, ...
882882
"y", range', ...
883883
"z", zVals, ...
@@ -886,7 +886,7 @@ function testContourPlotData(tc)
886886
"start", -0.8, ...
887887
"end", 0.8, ...
888888
"size", 0.2, ...
889-
"coloring", 'lines', ...
889+
"coloring", "lines", ...
890890
"showlines", true ...
891891
), ...
892892
"zauto", false, ...
@@ -896,7 +896,7 @@ function testContourPlotData(tc)
896896
"reversescale", false, ...
897897
"line", struct( ...
898898
"width", 0.75, ...
899-
"dash", 'solid', ...
899+
"dash", "solid", ...
900900
"color", "rgba(0,0,0,0)", ...
901901
"smoothing", 0 ...
902902
), ...

plotly/plotlyfig_aux/core/updateData.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@
120120
elseif ismember("terncontour", lower(obj.PlotOptions.TreatAs))
121121
updateTernaryContour(obj, dataIndex);
122122
else
123-
updateContourgroup(obj,dataIndex);
123+
obj.data{dataIndex} = updateContourgroup(obj,dataIndex);
124124
end
125125
case "functioncontour"
126126
obj.data{dataIndex} = updateFunctionContour(obj,dataIndex);
Lines changed: 73 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
function obj = updateContourgroup(obj,plotIndex)
1+
function data = updateContourgroup(obj,plotIndex)
22
%-INITIALIZATIONS-%
33

44
axIndex = obj.getAxisIndex(obj.State.Plot(plotIndex).AssociatedAxis);
@@ -21,137 +21,120 @@
2121
contourEnd = plotData.TextList(end);
2222
contourSize = mean(diff(plotData.TextList));
2323

24-
if length(plotData.TextList) == 1
24+
if isscalar(plotData.TextList)
2525
contourStart = plotData.TextList(1) - 1e-3;
2626
contourEnd = plotData.TextList(end) + 1e-3;
2727
contourSize = 2e-3;
2828
end
2929

30-
%-set trace-%
31-
obj.data{plotIndex}.type = 'contour';
32-
obj.data{plotIndex}.xaxis = sprintf('x%d', xSource);
33-
obj.data{plotIndex}.yaxis = sprintf('y%d', ySource);
34-
obj.data{plotIndex}.name = plotData.DisplayName;
35-
obj.data{plotIndex}.visible = strcmp(plotData.Visible, 'on');
36-
obj.data{plotIndex}.xtype = 'array';
37-
obj.data{plotIndex}.ytype = 'array';
38-
39-
%-set trace data-%
40-
obj.data{plotIndex}.x = xData;
41-
obj.data{plotIndex}.y = yData;
42-
obj.data{plotIndex}.z = zData;
43-
44-
%-set contour levels-%
45-
obj.data{plotIndex}.autocontour = false;
46-
obj.data{plotIndex}.contours.start = contourStart;
47-
obj.data{plotIndex}.contours.end = contourEnd;
48-
obj.data{plotIndex}.contours.size = contourSize;
49-
50-
%-set trace coloring-%
51-
obj.data{plotIndex}.zauto = false;
52-
obj.data{plotIndex}.zmin = axisData.CLim(1);
53-
obj.data{plotIndex}.zmax = axisData.CLim(2);
54-
obj.data{plotIndex}.showscale = false;
55-
obj.data{plotIndex}.reversescale = false;
56-
obj.data{plotIndex}.colorscale = getColorScale(plotData, axisData);
57-
58-
if strcmp(plotData.Fill, 'off')
59-
obj.data{plotIndex}.contours.coloring = 'lines';
30+
data.type = "contour";
31+
data.xaxis = "x" + xSource;
32+
data.yaxis = "y" + ySource;
33+
data.name = plotData.DisplayName;
34+
data.visible = plotData.Visible == "on";
35+
data.xtype = "array";
36+
data.ytype = "array";
37+
38+
data.x = xData;
39+
data.y = yData;
40+
data.z = zData;
41+
42+
data.autocontour = false;
43+
data.contours.start = contourStart;
44+
data.contours.end = contourEnd;
45+
data.contours.size = contourSize;
46+
47+
data.zauto = false;
48+
data.zmin = axisData.CLim(1);
49+
data.zmax = axisData.CLim(2);
50+
data.showscale = false;
51+
data.reversescale = false;
52+
data.colorscale = getColorScale(plotData, axisData);
53+
54+
if plotData.Fill == "off"
55+
data.contours.coloring = "lines";
6056
else
61-
obj.data{plotIndex}.contours.coloring = 'fill';
57+
data.contours.coloring = "fill";
6258
end
6359

6460
%-set contour line-%
65-
if ~strcmp(plotData.LineStyle, 'none')
66-
obj.data{plotIndex}.contours.showlines = true;
67-
obj.data{plotIndex}.line = getContourLine(plotData);
61+
if plotData.LineStyle ~= "none"
62+
data.contours.showlines = true;
63+
data.line = getContourLine(plotData);
6864
else
69-
obj.data{plotIndex}.contours.showlines = false;
65+
data.contours.showlines = false;
7066
end
7167

7268
%-set contour label-%
73-
if strcmpi(plotData.ShowText, 'on')
74-
obj.data{plotIndex}.contours.showlabels = true;
75-
obj.data{plotIndex}.contours.labelfont = getLabelFont(axisData);
69+
if lower(plotData.ShowText) == "on"
70+
data.contours.showlabels = true;
71+
data.contours.labelfont = getLabelFont(axisData);
7672
end
7773

7874
%-set trace legend-%
79-
obj.data{plotIndex}.showlegend = getShowLegend(plotData);
75+
data.showlegend = getShowLegend(plotData);
8076
end
8177

8278
function contourLine = getContourLine(plotData)
83-
%-initializations-%
84-
lineStyle = plotData.LineStyle;
85-
lineWidth = 1.5*plotData.LineWidth;
86-
lineColor = plotData.LineColor;
87-
88-
%-line color-%
89-
if isnumeric(lineColor)
90-
lineColor = getStringColor(round(255*lineColor));
79+
if isnumeric(plotData.LineColor)
80+
lineColor = getStringColor(round(255*plotData.LineColor));
9181
else
9282
lineColor = "rgba(0,0,0,0)";
9383
end
9484

95-
%-line dash-%
96-
switch lineStyle
97-
case '-'
98-
lineStyle = 'solid';
99-
case '--'
100-
lineStyle = 'dash';
101-
case ':'
102-
lineStyle = 'dot';
103-
case '-.'
104-
lineStyle = 'dashdot';
85+
switch plotData.LineStyle
86+
case "-"
87+
lineStyle = "solid";
88+
case "--"
89+
lineStyle = "dash";
90+
case ":"
91+
lineStyle = "dot";
92+
case "-."
93+
lineStyle = "dashdot";
10594
end
10695

107-
%-return-%
108-
contourLine.width = lineWidth;
109-
contourLine.dash = lineStyle;
110-
contourLine.color = lineColor;
111-
contourLine.smoothing = 0;
96+
contourLine = struct( ...
97+
"width", 1.5*plotData.LineWidth, ...
98+
"dash", lineStyle, ...
99+
"color", lineColor, ...
100+
"smoothing", 0 ...
101+
);
112102
end
113103

114104
function colorScale = getColorScale(plotData, axisData)
115-
%-initializations-%
116105
cMap = axisData.Colormap;
117106
nColors = size(cMap, 1);
118107
isBackground = any(plotData.ZData(:) < plotData.TextList(1));
119108
nContours = length(plotData.TextList);
120109
cScaleInd = linspace(0,1, nContours);
121-
if nContours==1
110+
if nContours == 1
122111
cScaleInd = 0.5;
123112
end
124-
cMapInd = floor( (nColors-1)*cScaleInd ) + 1;
113+
cMapInd = floor((nColors-1)*cScaleInd) + 1;
125114

126-
%-colorscale-%
127-
if strcmp(plotData.Fill, 'on')
115+
if plotData.Fill == "on"
116+
colorScale = cell(1, nContours);
117+
colors = cMap(cMapInd, :);
128118
if isBackground
129-
colorScale{1} = {0, getStringColor(round(255*ones(1,3)))};
130-
cScaleInd = linspace(1/nContours, 1, nContours);
131-
end
132-
for n = 1:nContours
133-
m = n;
134-
if isBackground
135-
m = n+1;
136-
end
137-
stringColor = getStringColor(round(255*cMap(cMapInd(n), :)));
138-
colorScale{m} = {cScaleInd(n), stringColor};
119+
colorScale = cell(1, nContours+1);
120+
colors = [ones(1,3); colors];
121+
cScaleInd = linspace(0, 1, nContours+1);
139122
end
140123
else
124+
colors = cMap;
125+
colorScale = cell(1,nColors);
141126
cScaleInd = rescale(1:nColors, 0, 1);
142-
for n = 1:nColors
143-
stringColor = getStringColor(round(255*cMap(n,:)));
144-
colorScale{n} = {cScaleInd(n), stringColor};
145-
end
127+
end
128+
for n = 1:numel(colorScale)
129+
stringColor = getStringColor(round(255*colors(n,:)));
130+
colorScale{n} = {cScaleInd(n), stringColor};
146131
end
147132
end
148133

149134
function labelFont = getLabelFont(axisData)
150-
labelColor = getStringColor(round(255*axisData.XAxis.Color));
151-
labelSize = axisData.XAxis.FontSize;
152-
labelFamily = matlab2plotlyfont(axisData.XAxis.FontName);
153-
154-
labelFont.color = labelColor;
155-
labelFont.size = labelSize;
156-
labelFont.family = labelFamily;
135+
labelFont = struct( ...
136+
"color", getStringColor(round(255*axisData.XAxis.Color)), ...
137+
"size", axisData.XAxis.FontSize, ...
138+
"family", matlab2plotlyfont(axisData.XAxis.FontName) ...
139+
);
157140
end

0 commit comments

Comments
 (0)