Skip to content

Commit 4d07882

Browse files
Merge pull request #509 from plotly/fix-empty-m2json
Fix empty m2json behaviour, fix colour issues
2 parents 8063131 + 4f90ad5 commit 4d07882

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+405
-473
lines changed

Diff for: plotly/plotly_aux/Test_m2json.m

+26-20
Original file line numberDiff line numberDiff line change
@@ -3,132 +3,138 @@
33
function testLowPrecisionInRange0to10(tc)
44
values = 1 + (1:5) + 0.234;
55
expected = "[2.234,3.234,4.234,5.234,6.234]";
6-
tc.verifyEqual(string(m2json(values)), expected);
6+
tc.verifyEqual(m2json(values), expected);
77
end
88

99
function testInRange0to10(tc)
1010
values = 1 + (1:5) + 0.23456789;
1111
expected = "[2.23456789,3.23456789,4.23456789,5.23456789," ...
1212
+ "6.23456789]";
13-
tc.verifyEqual(string(m2json(values)), expected);
13+
tc.verifyEqual(m2json(values), expected);
1414
end
1515

1616
function test2dArrayInRange0to10(tc)
1717
values = 1 + (1:5) + (0:1)' + 0.234;
1818
expected = "[[2.234,3.234,4.234,5.234,6.234]," ...
1919
+ "[3.234,4.234,5.234,6.234,7.234]]";
20-
tc.verifyEqual(string(m2json(values)), expected);
20+
tc.verifyEqual(m2json(values), expected);
2121
end
2222

2323
function testLowPrecisionInRange1e6to1e5(tc)
2424
values = 1e-6 * (1 + (1:5) + 0.234);
2525
expected = "[2.234e-06,3.234e-06,4.234e-06,5.234e-06," ...
2626
+ "6.234e-06]";
27-
tc.verifyEqual(string(m2json(values)), expected);
27+
tc.verifyEqual(m2json(values), expected);
2828
end
2929

3030
function testInRange1e6to1e5(tc)
3131
values = 1e-6 * (1 + (1:5) + 0.23456789);
3232
expected = "[2.23456789e-06,3.23456789e-06,4.23456789e-06," ...
3333
+ "5.23456789e-06,6.23456789e-06]";
34-
tc.verifyEqual(string(m2json(values)), expected);
34+
tc.verifyEqual(m2json(values), expected);
3535
end
3636

3737
function testInRange1e14Plus0to1(tc)
3838
values = 1e14 + (1:5) + 0.23456789;
3939
expected = "[100000000000001,100000000000002,"...
4040
+ "100000000000003,100000000000004,100000000000005]";
41-
tc.verifyEqual(string(m2json(values)), expected);
41+
tc.verifyEqual(m2json(values), expected);
4242
end
4343

4444
function testInRange1e14Plus1e7Plus0to1(tc)
4545
values = 1e14 + 1e7 + (1:5) + 0.23456789;
4646
expected = "[100000010000001,100000010000002," ...
4747
+ "100000010000003,100000010000004,100000010000005]";
48-
tc.verifyEqual(string(m2json(values)), expected);
48+
tc.verifyEqual(m2json(values), expected);
4949
end
5050

5151
function testLogScaledVariables(tc)
5252
values = 1e14 + 10.^(1:5) + 0.23456789;
5353
expected = "[100000000000010,100000000000100," ...
5454
+ "100000000001000,100000000010000,100000000100000]";
55-
tc.verifyEqual(string(m2json(values)), expected);
55+
tc.verifyEqual(m2json(values), expected);
5656
end
5757

5858
function testLowPrecisionInRangeMinus10to0(tc)
5959
values = -(1 + (1:5) + 0.234);
6060
expected = "[-2.234,-3.234,-4.234,-5.234,-6.234]";
61-
tc.verifyEqual(string(m2json(values)), expected);
61+
tc.verifyEqual(m2json(values), expected);
6262
end
6363

6464
function testInRangeMinus10to0(tc)
6565
values = -(1 + (1:5) + 0.23456789);
6666
expected = "[-2.23456789,-3.23456789,-4.23456789," ...
6767
+ "-5.23456789,-6.23456789]";
68-
tc.verifyEqual(string(m2json(values)), expected);
68+
tc.verifyEqual(m2json(values), expected);
6969
end
7070

7171
function testInRangeMinus1e5toMinus1e6(tc)
7272
values = -1e-6 * (1 + (1:5) + 0.23456789);
7373
expected = "[-2.23456789e-06,-3.23456789e-06," ...
7474
+ "-4.23456789e-06,-5.23456789e-06,-6.23456789e-06]";
75-
tc.verifyEqual(string(m2json(values)), expected);
75+
tc.verifyEqual(m2json(values), expected);
7676
end
7777

7878
function testInRangeMinus1e14Plus0to1(tc)
7979
values = -1e14 + (1:5) + 0.23456789;
8080
expected = "[-99999999999998.8,-99999999999997.8," ...
8181
+ "-99999999999996.8,-99999999999995.8," ...
8282
+ "-99999999999994.8]";
83-
tc.verifyEqual(string(m2json(values)), expected);
83+
tc.verifyEqual(m2json(values), expected);
84+
end
85+
86+
function testEmpty(tc)
87+
values = [];
88+
expected = "[]";
89+
tc.verifyEqual(m2json(values), expected);
8490
end
8591

8692
function testCell(tc)
8793
values = {1, "text", [1,2,3]};
8894
expected = "[1, ""text"", [1,2,3]]";
89-
tc.verifyEqual(string(m2json(values)), expected);
95+
tc.verifyEqual(m2json(values), expected);
9096
end
9197

9298
function testStruct(tc)
9399
values = struct("a", 1, "b", "text");
94100
expected = "{""a"" : 1, ""b"" : ""text""}";
95-
tc.verifyEqual(string(m2json(values)), expected);
101+
tc.verifyEqual(m2json(values), expected);
96102
end
97103

98104
function testDatetime(tc)
99105
value = datetime("2023-05-01 12:30:45");
100106
expected = """2023-05-01 12:30:45""";
101-
tc.verifyEqual(string(m2json(value)), expected);
107+
tc.verifyEqual(m2json(value), expected);
102108
end
103109

104110
function testDate(tc)
105111
value = datetime("2023-05-01");
106112
expected = """2023-05-01""";
107-
tc.verifyEqual(string(m2json(value)), expected);
113+
tc.verifyEqual(m2json(value), expected);
108114
end
109115

110116
function testLogicalTrue(tc)
111117
value = true;
112118
expected = "true";
113-
tc.verifyEqual(string(m2json(value)), expected);
119+
tc.verifyEqual(m2json(value), expected);
114120
end
115121

116122
function testLogicalFalse(tc)
117123
value = false;
118124
expected = "false";
119-
tc.verifyEqual(string(m2json(value)), expected);
125+
tc.verifyEqual(m2json(value), expected);
120126
end
121127

122128
function testCharArray(tc)
123129
value = 'Hello';
124130
expected = """Hello""";
125-
tc.verifyEqual(string(m2json(value)), expected);
131+
tc.verifyEqual(m2json(value), expected);
126132
end
127133

128134
function testString(tc)
129135
value = "World";
130136
expected = """World""";
131-
tc.verifyEqual(string(m2json(value)), expected);
137+
tc.verifyEqual(m2json(value), expected);
132138
end
133139
end
134140
end

Diff for: plotly/plotly_aux/m2json.m

+5-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
elseif iscell(val)
55
valstr = cell2json(val);
66
elseif isa(val, "numeric")
7+
if isempty(val)
8+
valstr = "[]";
9+
return;
10+
end
711
sz = size(val);
812
if isa(val,"single")
913
numDigits = 7;
@@ -25,8 +29,6 @@
2529
end
2630
if length(val)>1
2731
valstr = "[" + valstr + "]";
28-
elseif isempty(val)
29-
valstr = "[]";
3032
end
3133
valstr = strrep(valstr,"-Inf", "null");
3234
valstr = strrep(valstr, "Inf", "null");
@@ -38,7 +40,7 @@
3840
valstr = cell2json(cellstr(val));
3941
else
4042
val = checkescape(val); % add escape characters
41-
valstr = sprintf('"%s"', val);
43+
valstr = sprintf("""%s""", val);
4244
end
4345
elseif islogical(val)
4446
if val

Diff for: plotly/plotlyfig_aux/core/updateAnnotation.m

+3-3
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,9 @@
141141

142142

143143
%-font color-%
144-
col = 255*text_data.Color;
144+
col = round(255*text_data.Color);
145145
obj.layout.annotations{anIndex}.font.color = ...
146-
sprintf("rgba(%d,%d,%d)", col);
146+
sprintf("rgb(%d,%d,%d)", col);
147147

148148
%---------------------------------------------------------------------%
149149

@@ -181,7 +181,7 @@
181181

182182
%-border color-%
183183
if ~ischar(text_data.EdgeColor)
184-
col = 255*text_data.EdgeColora;
184+
col = round(255*text_data.EdgeColora);
185185
obj.layout.annotations{anIndex}.bordercolor = ...
186186
sprintf("rgb(%d,%d,%d)", col);
187187
else

Diff for: plotly/plotlyfig_aux/core/updateColorbar.m

+12-12
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,13 @@
5252
outlineColor = [0 0 0];
5353
else
5454
if colorbarData.Position(4) > colorbarData.Position(3)
55-
outlineColor = 255*colorbarData.YColor;
55+
outlineColor = round(255*colorbarData.YColor);
5656
else
57-
outlineColor = 255*colorbarData.XColor;
57+
outlineColor = round(255*colorbarData.XColor);
5858
end
5959
end
6060

61-
outlineColor = sprintf('rgb(%f,%f,%f)', outlineColor);
61+
outlineColor = sprintf("rgb(%d,%d,%d)", outlineColor);
6262
lineWidth = colorbarData.LineWidth ...
6363
* obj.PlotlyDefaults.AxisLineIncreaseFactor;
6464
tickLength = min(obj.PlotlyDefaults.MaxTickLength, ...
@@ -147,8 +147,8 @@
147147
end
148148

149149
titleFontSize = 1.20 * colorbarTitleData.FontSize;
150-
titleFontColor = ...
151-
sprintf('rgb(%f,%f,%f)', 255*colorbarTitleData.Color);
150+
titleFontColor = sprintf("rgb(%d,%d,%d)", ...
151+
round(255*colorbarTitleData.Color));
152152
titleFontFamily = matlab2plotlyfont(colorbarTitleData.FontName);
153153

154154
elseif ~isempty(colorbarXLabelData.String)
@@ -157,8 +157,8 @@
157157

158158
titleSide = 'right';
159159
titleFontSize = 1.20 * colorbarXLabelData.FontSize;
160-
titleFontColor = ...
161-
sprintf('rgb(%f,%f,%f)', 255*colorbarXLabelData.Color);
160+
titleFontColor = sprintf("rgb(%d,%d,%d)", ...
161+
round(255*colorbarXLabelData.Color));
162162
titleFontFamily = matlab2plotlyfont(colorbarXLabelData.FontName);
163163

164164
elseif ~isempty(colorbarYLabelData.String)
@@ -167,8 +167,8 @@
167167

168168
titleSide = 'bottom';
169169
titleFontSize = 1.20 * colorbarYLabelData.FontSize;
170-
titleFontColor = ...
171-
sprintf('rgb(%f,%f,%f)', 255*colorbarYLabelData.Color);
170+
titleFontColor = sprintf("rgb(%d,%d,%d)", ...
171+
round(255*colorbarYLabelData.Color));
172172
titleFontFamily = matlab2plotlyfont(colorbarYLabelData.FontName);
173173

174174
else
@@ -244,12 +244,12 @@
244244
%-colorbar bg-color-%
245245
if ~isHG2
246246
if ~ischar(colorbarData.Color)
247-
bgColor = 255*colorbarData.Color;
247+
bgColor = round(255*colorbarData.Color);
248248
else
249-
bgColor = 255*figureData.Color;
249+
bgColor = round(255*figureData.Color);
250250
end
251251

252-
obj.layout.plot_bgcolor = sprintf('rgb(%f,%f,%f', bgColor);
252+
obj.layout.plot_bgcolor = sprintf("rgb(%d,%d,%d)", bgColor);
253253
end
254254

255255
%---------------------------------------------------------------------%

Diff for: plotly/plotlyfig_aux/core/updateLegend.m

+8-59
Original file line numberDiff line numberDiff line change
@@ -23,83 +23,32 @@
2323
% only displays last legend as global Plotly legend
2424
obj.layout.legend = struct();
2525

26-
%---------------------------------------------------------------------%
27-
28-
%-layout showlegend-%
2926
obj.layout.showlegend = strcmpi(legend_data.Visible,'on');
30-
31-
%---------------------------------------------------------------------%
32-
33-
%-legend x-%
3427
obj.layout.legend.x = legend_data.Position(1);
35-
36-
%---------------------------------------------------------------------%
37-
38-
%-legend xref-%
3928
obj.layout.legend.xref = 'paper';
40-
41-
%---------------------------------------------------------------------%
42-
43-
%-legend xanchor-%
4429
obj.layout.legend.xanchor = 'left';
45-
46-
%---------------------------------------------------------------------%
47-
48-
%-legend y-%
4930
obj.layout.legend.y = legend_data.Position(2);
50-
51-
%---------------------------------------------------------------------%
52-
53-
%-legend yref-%
5431
obj.layout.legend.yref = 'paper';
55-
56-
%---------------------------------------------------------------------%
57-
58-
%-legend yanchor-%
5932
obj.layout.legend.yanchor = 'bottom';
6033

61-
%---------------------------------------------------------------------%
6234

63-
if (strcmp(legend_data.Box,'on') && strcmp(legend_data.Visible, 'on'))
64-
%-legend traceorder-%
35+
if (strcmp(legend_data.Box, 'on') && strcmp(legend_data.Visible, 'on'))
6536
obj.layout.legend.traceorder = 'normal';
66-
67-
%-----------------------------------------------------------------%
68-
69-
%-legend borderwidth-%
7037
obj.layout.legend.borderwidth = legend_data.LineWidth;
7138

72-
%-----------------------------------------------------------------%
39+
col = round(255*legend_data.EdgeColor);
40+
obj.layout.legend.bordercolor = sprintf("rgb(%d,%d,%d)", col);
7341

74-
%-legend bordercolor-%
75-
col = 255*legend_data.EdgeColor;
76-
obj.layout.legend.bordercolor = sprintf("rgb(%f,%f,%f)", col);
42+
col = round(255*legend_data.Color);
43+
obj.layout.legend.bgcolor = sprintf("rgb(%d,%d,%d)", col);
7744

78-
%-----------------------------------------------------------------%
79-
80-
%-legend bgcolor-%
81-
col = 255*legend_data.Color;
82-
obj.layout.legend.bgcolor = sprintf("rgb(%f,%f,%f)", col);
83-
84-
%-----------------------------------------------------------------%
85-
86-
%-legend font size-%
8745
obj.layout.legend.font.size = legend_data.FontSize;
88-
89-
%-----------------------------------------------------------------%
90-
91-
%-legend font family-%
9246
obj.layout.legend.font.family = ...
9347
matlab2plotlyfont(legend_data.FontName);
94-
95-
%-----------------------------------------------------------------%
96-
97-
%-legend font colour-%
98-
col = 255*legend_data.TextColor;
99-
obj.layout.legend.font.color = sprintf("rgb(%f,%f,%f)", col);
100-
end
10148

102-
%---------------------------------------------------------------------%
49+
col = round(255*legend_data.TextColor);
50+
obj.layout.legend.font.color = sprintf("rgb(%d,%d,%d)", col);
51+
end
10352

10453
%-REVERT UNITS-%
10554
set(obj.State.Legend(legIndex).Handle,'Units',legendunits);

0 commit comments

Comments
 (0)