Skip to content

Commit 058dc42

Browse files
Refactor updateHeatmap
1 parent 6ed9a70 commit 058dc42

File tree

3 files changed

+47
-57
lines changed

3 files changed

+47
-57
lines changed

plotly/Test_plotlyfig.m

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,24 +1056,24 @@ function testHeatmapData(tc)
10561056

10571057
tc.verifyNumElements(p.data, 1);
10581058
tc.verifyEqual(rmfield(p.data{1}, "colorscale"), struct( ...
1059-
"type", 'heatmap', ...
1059+
"type", "heatmap", ...
10601060
"x", {num2cell(num2str((1:5)'))}, ...
10611061
"y", {num2cell(num2str(flip(1:5)'))}, ...
10621062
"z", flip(data), ...
10631063
"connectgaps", false, ...
10641064
"hoverongaps", false, ...
1065-
"hoverinfo", 'text', ...
1065+
"hoverinfo", "text", ...
10661066
"text", flip(data), ...
10671067
"hoverlabel", struct( ...
1068-
"bgcolor", 'white' ...
1068+
"bgcolor", "white" ...
10691069
), ...
10701070
"showscale", true, ...
10711071
"colorbar", struct( ...
10721072
"x", 0.87, ...
10731073
"y", 0.52, ...
10741074
"ypad", 55, ...
10751075
"xpad", 0, ...
1076-
"outlinecolor", 'rgb(150,150,150)' ...
1076+
"outlinecolor", "rgb(150,150,150)" ...
10771077
), ...
10781078
"visible", true, ...
10791079
"opacity", 0.9500, ...

plotly/plotlyfig_aux/core/updateData.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
case "wordcloud"
4444
updateWordcloud(obj, dataIndex);
4545
case "heatmap"
46-
updateHeatmap(obj, dataIndex);
46+
obj.data{dataIndex} = updateHeatmap(obj, dataIndex);
4747
case "image"
4848
if ~obj.PlotOptions.Image3D
4949
obj.data{dataIndex} = updateImage(obj, dataIndex);
Lines changed: 42 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,71 @@
1-
function obj = updateHeatmap(obj,heatIndex)
1+
function data = updateHeatmap(obj,heatIndex)
22
%-HEATMAP DATA STRUCTURE- %
33
heat_data = obj.State.Plot(heatIndex).Handle;
44

5-
%-heatmap type-%
6-
obj.data{heatIndex}.type = 'heatmap';
5+
data.type = "heatmap";
76

8-
%-format data-%
9-
xdata = heat_data.XDisplayData;
10-
ydata = heat_data.YDisplayData(end:-1:1, :);
117
cdata = heat_data.ColorDisplayData(end:-1:1, :);
128

13-
obj.data{heatIndex}.x = xdata;
14-
obj.data{heatIndex}.y = ydata;
15-
obj.data{heatIndex}.z = cdata;
16-
obj.data{heatIndex}.connectgaps = false;
17-
obj.data{heatIndex}.hoverongaps = false;
9+
data.x = heat_data.XDisplayData;
10+
data.y = heat_data.YDisplayData(end:-1:1, :);
11+
data.z = cdata;
12+
data.connectgaps = false;
13+
data.hoverongaps = false;
1814

19-
%-heatmap colorscale-%
2015
cmap = heat_data.Colormap;
2116
len = length(cmap)-1;
22-
23-
for c = 1: length(cmap)
24-
col = round(255 * cmap(c, :));
25-
obj.data{heatIndex}.colorscale{c} = ...
26-
{(c-1)/len, sprintf("rgb(%d,%d,%d)", col)};
17+
for c = 1:length(cmap)
18+
col = round(255*cmap(c, :));
19+
data.colorscale{c} = {(c-1)/len, getStringColor(col)};
2720
end
2821

29-
%-setting plot-%
30-
obj.data{heatIndex}.hoverinfo = 'text';
31-
obj.data{heatIndex}.text = heat_data.ColorData(end:-1:1, :);
32-
obj.data{heatIndex}.hoverlabel.bgcolor = 'white';
22+
data.hoverinfo = "text";
23+
data.text = heat_data.ColorData(end:-1:1, :);
24+
data.hoverlabel.bgcolor = "white";
3325

34-
%-show colorbar-%
35-
obj.data{heatIndex}.showscale = false;
36-
if strcmpi(heat_data.ColorbarVisible, 'on')
37-
obj.data{heatIndex}.showscale = true;
38-
obj.data{heatIndex}.colorbar.x = 0.87;
39-
obj.data{heatIndex}.colorbar.y = 0.52;
40-
obj.data{heatIndex}.colorbar.ypad = 55;
41-
obj.data{heatIndex}.colorbar.xpad = obj.PlotlyDefaults.MarginPad;
42-
obj.data{heatIndex}.colorbar.outlinecolor = 'rgb(150,150,150)';
26+
data.showscale = false;
27+
if lower(heat_data.ColorbarVisible) == "on"
28+
data.showscale = true;
29+
data.colorbar = struct( ...
30+
"x", 0.87, ...
31+
"y", 0.52, ...
32+
"ypad", 55, ...
33+
"xpad", obj.PlotlyDefaults.MarginPad, ...
34+
"outlinecolor", "rgb(150,150,150)" ...
35+
);
4336
end
4437

45-
%-hist visible-%
46-
obj.data{heatIndex}.visible = strcmp(heat_data.Visible,'on');
47-
obj.data{heatIndex}.opacity = 0.95;
38+
data.visible = heat_data.Visible == "on";
39+
data.opacity = 0.95;
4840

4941
%-setting annotation text-%
50-
c = 1;
5142
maxcol = max(cdata(:));
52-
53-
for n = 1:size(cdata, 2)
54-
for m = 1:size(cdata, 1)
55-
%-text-%
56-
ann{c}.text = num2str(round(cdata(m,n), 2));
57-
ann{c}.x = n-1;
58-
ann{c}.y = m-1;
59-
ann{c}.showarrow = false;
60-
61-
%-font-%
62-
ann{c}.font.size = heat_data.FontSize*1.15;
63-
ann{c}.font.family = matlab2plotlyfont(heat_data.FontName);
64-
if cdata(m,n) < 0.925*maxcol
43+
m = size(cdata, 2);
44+
n = size(cdata, 1);
45+
annotations = cell(1,m*n);
46+
for i = 1:m
47+
for j = 1:n
48+
ann.text = num2str(round(cdata(j,i), 2));
49+
ann.x = i-1;
50+
ann.y = j-1;
51+
ann.showarrow = false;
52+
ann.font.size = heat_data.FontSize*1.15;
53+
ann.font.family = matlab2plotlyfont(heat_data.FontName);
54+
if cdata(j,i) < 0.925*maxcol
6555
col = [0,0,0];
6656
else
6757
col = [255,255,255];
6858
end
69-
ann{c}.font.color = sprintf("rgb(%d,%d,%d)", col);
70-
c = c+1;
59+
ann.font.color = getStringColor(col);
60+
annotations{i*(m-1)+j} = ann;
7161
end
7262
end
7363

74-
obj.layout.annotations = ann;
64+
obj.layout.annotations = annotations;
7565

7666
%-set background color if any NaN in cdata-%
7767
if any(isnan(cdata(:)))
78-
obj.layout.plot_bgcolor = 'rgb(40,40,40)';
79-
obj.data{heatIndex}.opacity = 1;
68+
obj.layout.plot_bgcolor = "rgb(40,40,40)";
69+
data.opacity = 1;
8070
end
8171
end

0 commit comments

Comments
 (0)