Skip to content

Commit b89336d

Browse files
Merge pull request #368 from plotly/adding_wordclud_to_fig2plotly
Adding wordclud to work with fig2plotly
2 parents 6fbdb71 + 1dc7c26 commit b89336d

File tree

2 files changed

+169
-0
lines changed

2 files changed

+169
-0
lines changed

plotly/plotlyfig_aux/core/updateData.m

+2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
switch lower(obj.State.Plot(dataIndex).Class)
3737

3838
%--CORE PLOT OBJECTS--%
39+
case 'wordcloud'
40+
updateWordcloud(obj, dataIndex);
3941
case 'heatmap'
4042
updateHeatmap(obj, dataIndex);
4143
case 'image'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
function updateWordcloud(obj,scatterIndex)
2+
3+
%-AXIS INDEX-%
4+
axIndex = obj.getAxisIndex(obj.State.Plot(scatterIndex).AssociatedAxis);
5+
6+
%-SCATTER DATA STRUCTURE- %
7+
scatter_data = get(obj.State.Plot(scatterIndex).Handle);
8+
9+
10+
%-CHECK FOR MULTIPLE AXES-%
11+
[xsource, ysource] = findSourceAxis(obj,axIndex);
12+
13+
%-------------------------------------------------------------------------%
14+
15+
%-scatter type-%
16+
obj.data{scatterIndex}.type = 'scatter';
17+
18+
%-------------------------------------------------------------------------%
19+
20+
%-format the mesh domain-%
21+
maxx = scatter_data.MaxDisplayWords;
22+
npoints = round(sqrt(maxx));
23+
24+
if mod(npoints, 2) == 0
25+
npoints = npoints+1;
26+
end
27+
28+
xdomain = linspace(1, maxx, npoints);
29+
ydomain = linspace(1, maxx*.7, npoints);
30+
31+
[xdata, ydata] = meshgrid(xdomain, ydomain);
32+
xrand = diff(xdomain(1:2)) * (rand(size(xdata)) - 0.5);
33+
yrand = diff(ydomain(1:2)) * (rand(size(ydata)) - 0.5);
34+
xdata = xdata + xrand;
35+
ydata = ydata + yrand;
36+
37+
%-------------------------------------------------------------------------%
38+
39+
%-make oval effect-%
40+
inds = (xdata-0.5*xdomain(end)).^2 + (ydata-0.5*ydomain(end)).^2 < (0.5*maxx)^2;
41+
xdata(~inds) = NaN;
42+
ydata(~inds) = NaN;
43+
44+
%-------------------------------------------------------------------------%
45+
46+
%-get frequency-%
47+
[B, inds] = sort(scatter_data.SizeData, 'descend');
48+
49+
%-------------------------------------------------------------------------%
50+
51+
%-take more freq words-%
52+
nwords = numel(xdata);
53+
inds = inds(1:nwords);
54+
55+
%-------------------------------------------------------------------------%
56+
57+
%-get indices for distribution-%
58+
middle = round(nwords*0.5);
59+
inds = inds(mod([1:nwords] + middle, nwords)+1);
60+
inds_aux = inds;
61+
inds1 = round(linspace(1,middle-1, round(middle/2)));
62+
inds2 = round(linspace(nwords,middle+1, round(middle/2)));
63+
inds(inds1) = inds_aux(inds2);
64+
inds(inds2) = inds_aux(inds1);
65+
66+
%-------------------------------------------------------------------------%
67+
68+
%-exchange columns-%
69+
inds = reshape(inds, size(xdata));
70+
inds_aux = inds;
71+
mc = round(0.5*size(inds_aux, 2));
72+
73+
inds(:,mc-2) = inds_aux(:,mc-1);
74+
inds(:,mc-1) = inds_aux(:,mc-2);
75+
inds(:,mc+1) = inds_aux(:,mc+2);
76+
inds(:,mc+2) = inds_aux(:,mc+1);
77+
inds = inds(:);
78+
79+
%-------------------------------------------------------------------------%
80+
81+
%-get data to wordcloud-%
82+
83+
% sizedata
84+
sizedata = scatter_data.SizeData(inds);
85+
86+
% worddata
87+
worddata = cell(nwords,1);
88+
for w = 1:nwords
89+
worddata{w} = char(scatter_data.WordData(inds(w)));
90+
end
91+
92+
%-------------------------------------------------------------------------%
93+
94+
%-sent data to plotly-%
95+
obj.data{scatterIndex}.mode = 'text';
96+
obj.data{scatterIndex}.x = xdata(:);
97+
obj.data{scatterIndex}.y = ydata(:);
98+
obj.data{scatterIndex}.text = worddata;
99+
obj.data{scatterIndex}.textfont.size = sizedata;
100+
101+
%-------------------------------------------------------------------------%
102+
103+
%-coloring-%
104+
is_colormap = size(scatter_data.Color, 1) > 1;
105+
col = cell(nwords, 1);
106+
107+
if ~is_colormap
108+
for w=1:nwords
109+
if B(4) > sizedata(w)
110+
col{w} = sprintf('rgb(%f,%f,%f)', scatter_data.Color*255);
111+
else
112+
col{w} = sprintf('rgb(%f,%f,%f)', scatter_data.HighlightColor*255);
113+
end
114+
end
115+
else
116+
for w=1:nwords
117+
col{w} = sprintf('rgb(%f,%f,%f)', scatter_data.Color(inds(w), :)*255);
118+
end
119+
end
120+
121+
obj.data{scatterIndex}.textfont.color = col;
122+
123+
%-------------------------------------------------------------------------%
124+
125+
%-det font family-%
126+
obj.data{scatterIndex}.textfont.family = matlab2plotlyfont(scatter_data.FontName);;
127+
128+
%-------------------------------------------------------------------------%
129+
130+
%-scatter visible-%
131+
obj.data{scatterIndex}.visible = strcmp(scatter_data.Visible,'on');
132+
133+
%-------------------------------------------------------------------------%
134+
135+
%-set layout-%
136+
xaxis.showgrid = false;
137+
xaxis.showticklabels = false;
138+
xaxis.zeroline = false;
139+
140+
yaxis.showgrid = false;
141+
yaxis.showticklabels = false;
142+
yaxis.zeroline = false;
143+
144+
xo = scatter_data.Position(1);
145+
yo = scatter_data.Position(2);
146+
w = scatter_data.Position(3);
147+
h = scatter_data.Position(4);
148+
149+
xaxis.domain = min([xo xo + w],1);
150+
yaxis.domain = min([yo yo + h],1);
151+
152+
obj.layout = setfield(obj.layout, sprintf('xaxis%d',xsource), xaxis);
153+
obj.layout = setfield(obj.layout, sprintf('yaxis%d',ysource), yaxis);
154+
155+
%-------------------------------------------------------------------------%
156+
157+
obj.layout.annotations{1}.xref = 'paper';
158+
obj.layout.annotations{1}.yref = 'paper';
159+
obj.layout.annotations{1}.showarrow = false;
160+
obj.layout.annotations{1}.text = sprintf('<b>%s</b>', scatter_data.Title);
161+
obj.layout.annotations{1}.x = mean(xaxis.domain);
162+
obj.layout.annotations{1}.y = (yaxis.domain(2) + obj.PlotlyDefaults.TitleHeight);
163+
obj.layout.annotations{1}.font.color = 'rgb(0,0,0)';
164+
obj.layout.annotations{1}.font.size = 15;
165+
166+
end
167+

0 commit comments

Comments
 (0)