Skip to content

Fix empty m2json behaviour, fix colour issues #509

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 26 additions & 20 deletions plotly/plotly_aux/Test_m2json.m
Original file line number Diff line number Diff line change
Expand Up @@ -3,132 +3,138 @@
function testLowPrecisionInRange0to10(tc)
values = 1 + (1:5) + 0.234;
expected = "[2.234,3.234,4.234,5.234,6.234]";
tc.verifyEqual(string(m2json(values)), expected);
tc.verifyEqual(m2json(values), expected);
end

function testInRange0to10(tc)
values = 1 + (1:5) + 0.23456789;
expected = "[2.23456789,3.23456789,4.23456789,5.23456789," ...
+ "6.23456789]";
tc.verifyEqual(string(m2json(values)), expected);
tc.verifyEqual(m2json(values), expected);
end

function test2dArrayInRange0to10(tc)
values = 1 + (1:5) + (0:1)' + 0.234;
expected = "[[2.234,3.234,4.234,5.234,6.234]," ...
+ "[3.234,4.234,5.234,6.234,7.234]]";
tc.verifyEqual(string(m2json(values)), expected);
tc.verifyEqual(m2json(values), expected);
end

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

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

function testInRange1e14Plus0to1(tc)
values = 1e14 + (1:5) + 0.23456789;
expected = "[100000000000001,100000000000002,"...
+ "100000000000003,100000000000004,100000000000005]";
tc.verifyEqual(string(m2json(values)), expected);
tc.verifyEqual(m2json(values), expected);
end

function testInRange1e14Plus1e7Plus0to1(tc)
values = 1e14 + 1e7 + (1:5) + 0.23456789;
expected = "[100000010000001,100000010000002," ...
+ "100000010000003,100000010000004,100000010000005]";
tc.verifyEqual(string(m2json(values)), expected);
tc.verifyEqual(m2json(values), expected);
end

function testLogScaledVariables(tc)
values = 1e14 + 10.^(1:5) + 0.23456789;
expected = "[100000000000010,100000000000100," ...
+ "100000000001000,100000000010000,100000000100000]";
tc.verifyEqual(string(m2json(values)), expected);
tc.verifyEqual(m2json(values), expected);
end

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

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

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

function testInRangeMinus1e14Plus0to1(tc)
values = -1e14 + (1:5) + 0.23456789;
expected = "[-99999999999998.8,-99999999999997.8," ...
+ "-99999999999996.8,-99999999999995.8," ...
+ "-99999999999994.8]";
tc.verifyEqual(string(m2json(values)), expected);
tc.verifyEqual(m2json(values), expected);
end

function testEmpty(tc)
values = [];
expected = "[]";
tc.verifyEqual(m2json(values), expected);
end

function testCell(tc)
values = {1, "text", [1,2,3]};
expected = "[1, ""text"", [1,2,3]]";
tc.verifyEqual(string(m2json(values)), expected);
tc.verifyEqual(m2json(values), expected);
end

function testStruct(tc)
values = struct("a", 1, "b", "text");
expected = "{""a"" : 1, ""b"" : ""text""}";
tc.verifyEqual(string(m2json(values)), expected);
tc.verifyEqual(m2json(values), expected);
end

function testDatetime(tc)
value = datetime("2023-05-01 12:30:45");
expected = """2023-05-01 12:30:45""";
tc.verifyEqual(string(m2json(value)), expected);
tc.verifyEqual(m2json(value), expected);
end

function testDate(tc)
value = datetime("2023-05-01");
expected = """2023-05-01""";
tc.verifyEqual(string(m2json(value)), expected);
tc.verifyEqual(m2json(value), expected);
end

function testLogicalTrue(tc)
value = true;
expected = "true";
tc.verifyEqual(string(m2json(value)), expected);
tc.verifyEqual(m2json(value), expected);
end

function testLogicalFalse(tc)
value = false;
expected = "false";
tc.verifyEqual(string(m2json(value)), expected);
tc.verifyEqual(m2json(value), expected);
end

function testCharArray(tc)
value = 'Hello';
expected = """Hello""";
tc.verifyEqual(string(m2json(value)), expected);
tc.verifyEqual(m2json(value), expected);
end

function testString(tc)
value = "World";
expected = """World""";
tc.verifyEqual(string(m2json(value)), expected);
tc.verifyEqual(m2json(value), expected);
end
end
end
8 changes: 5 additions & 3 deletions plotly/plotly_aux/m2json.m
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
elseif iscell(val)
valstr = cell2json(val);
elseif isa(val, "numeric")
if isempty(val)
valstr = "[]";
return;
end
sz = size(val);
if isa(val,"single")
numDigits = 7;
Expand All @@ -25,8 +29,6 @@
end
if length(val)>1
valstr = "[" + valstr + "]";
elseif isempty(val)
valstr = "[]";
end
valstr = strrep(valstr,"-Inf", "null");
valstr = strrep(valstr, "Inf", "null");
Expand All @@ -38,7 +40,7 @@
valstr = cell2json(cellstr(val));
else
val = checkescape(val); % add escape characters
valstr = sprintf('"%s"', val);
valstr = sprintf("""%s""", val);
end
elseif islogical(val)
if val
Expand Down
6 changes: 3 additions & 3 deletions plotly/plotlyfig_aux/core/updateAnnotation.m
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,9 @@


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

%---------------------------------------------------------------------%

Expand Down Expand Up @@ -181,7 +181,7 @@

%-border color-%
if ~ischar(text_data.EdgeColor)
col = 255*text_data.EdgeColora;
col = round(255*text_data.EdgeColora);
obj.layout.annotations{anIndex}.bordercolor = ...
sprintf("rgb(%d,%d,%d)", col);
else
Expand Down
24 changes: 12 additions & 12 deletions plotly/plotlyfig_aux/core/updateColorbar.m
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@
outlineColor = [0 0 0];
else
if colorbarData.Position(4) > colorbarData.Position(3)
outlineColor = 255*colorbarData.YColor;
outlineColor = round(255*colorbarData.YColor);
else
outlineColor = 255*colorbarData.XColor;
outlineColor = round(255*colorbarData.XColor);
end
end

outlineColor = sprintf('rgb(%f,%f,%f)', outlineColor);
outlineColor = sprintf("rgb(%d,%d,%d)", outlineColor);
lineWidth = colorbarData.LineWidth ...
* obj.PlotlyDefaults.AxisLineIncreaseFactor;
tickLength = min(obj.PlotlyDefaults.MaxTickLength, ...
Expand Down Expand Up @@ -147,8 +147,8 @@
end

titleFontSize = 1.20 * colorbarTitleData.FontSize;
titleFontColor = ...
sprintf('rgb(%f,%f,%f)', 255*colorbarTitleData.Color);
titleFontColor = sprintf("rgb(%d,%d,%d)", ...
round(255*colorbarTitleData.Color));
titleFontFamily = matlab2plotlyfont(colorbarTitleData.FontName);

elseif ~isempty(colorbarXLabelData.String)
Expand All @@ -157,8 +157,8 @@

titleSide = 'right';
titleFontSize = 1.20 * colorbarXLabelData.FontSize;
titleFontColor = ...
sprintf('rgb(%f,%f,%f)', 255*colorbarXLabelData.Color);
titleFontColor = sprintf("rgb(%d,%d,%d)", ...
round(255*colorbarXLabelData.Color));
titleFontFamily = matlab2plotlyfont(colorbarXLabelData.FontName);

elseif ~isempty(colorbarYLabelData.String)
Expand All @@ -167,8 +167,8 @@

titleSide = 'bottom';
titleFontSize = 1.20 * colorbarYLabelData.FontSize;
titleFontColor = ...
sprintf('rgb(%f,%f,%f)', 255*colorbarYLabelData.Color);
titleFontColor = sprintf("rgb(%d,%d,%d)", ...
round(255*colorbarYLabelData.Color));
titleFontFamily = matlab2plotlyfont(colorbarYLabelData.FontName);

else
Expand Down Expand Up @@ -244,12 +244,12 @@
%-colorbar bg-color-%
if ~isHG2
if ~ischar(colorbarData.Color)
bgColor = 255*colorbarData.Color;
bgColor = round(255*colorbarData.Color);
else
bgColor = 255*figureData.Color;
bgColor = round(255*figureData.Color);
end

obj.layout.plot_bgcolor = sprintf('rgb(%f,%f,%f', bgColor);
obj.layout.plot_bgcolor = sprintf("rgb(%d,%d,%d)", bgColor);
end

%---------------------------------------------------------------------%
Expand Down
67 changes: 8 additions & 59 deletions plotly/plotlyfig_aux/core/updateLegend.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,83 +23,32 @@
% only displays last legend as global Plotly legend
obj.layout.legend = struct();

%---------------------------------------------------------------------%

%-layout showlegend-%
obj.layout.showlegend = strcmpi(legend_data.Visible,'on');

%---------------------------------------------------------------------%

%-legend x-%
obj.layout.legend.x = legend_data.Position(1);

%---------------------------------------------------------------------%

%-legend xref-%
obj.layout.legend.xref = 'paper';

%---------------------------------------------------------------------%

%-legend xanchor-%
obj.layout.legend.xanchor = 'left';

%---------------------------------------------------------------------%

%-legend y-%
obj.layout.legend.y = legend_data.Position(2);

%---------------------------------------------------------------------%

%-legend yref-%
obj.layout.legend.yref = 'paper';

%---------------------------------------------------------------------%

%-legend yanchor-%
obj.layout.legend.yanchor = 'bottom';

%---------------------------------------------------------------------%

if (strcmp(legend_data.Box,'on') && strcmp(legend_data.Visible, 'on'))
%-legend traceorder-%
if (strcmp(legend_data.Box, 'on') && strcmp(legend_data.Visible, 'on'))
obj.layout.legend.traceorder = 'normal';

%-----------------------------------------------------------------%

%-legend borderwidth-%
obj.layout.legend.borderwidth = legend_data.LineWidth;

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

%-legend bordercolor-%
col = 255*legend_data.EdgeColor;
obj.layout.legend.bordercolor = sprintf("rgb(%f,%f,%f)", col);
col = round(255*legend_data.Color);
obj.layout.legend.bgcolor = sprintf("rgb(%d,%d,%d)", col);

%-----------------------------------------------------------------%

%-legend bgcolor-%
col = 255*legend_data.Color;
obj.layout.legend.bgcolor = sprintf("rgb(%f,%f,%f)", col);

%-----------------------------------------------------------------%

%-legend font size-%
obj.layout.legend.font.size = legend_data.FontSize;

%-----------------------------------------------------------------%

%-legend font family-%
obj.layout.legend.font.family = ...
matlab2plotlyfont(legend_data.FontName);

%-----------------------------------------------------------------%

%-legend font colour-%
col = 255*legend_data.TextColor;
obj.layout.legend.font.color = sprintf("rgb(%f,%f,%f)", col);
end

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

%-REVERT UNITS-%
set(obj.State.Legend(legIndex).Handle,'Units',legendunits);
Expand Down
Loading