Skip to content

Commit 6662c7a

Browse files
Merge pull request #398 from plotly/add_geobubble_functionality_in_fig2plotly
Add geobubble functionality in fig2plotly
2 parents 5e62bb7 + 296fc7c commit 6662c7a

File tree

2 files changed

+251
-0
lines changed

2 files changed

+251
-0
lines changed

plotly/plotlyfig_aux/core/updateData.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@
5656
UpdateGeoAxes(obj, dataIndex);
5757

5858
%--CORE PLOT OBJECTS--%
59+
case 'geobubble'
60+
updateGeobubble(obj, dataIndex);
5961
case 'scatterhistogram'
6062
updateScatterhistogram(obj, dataIndex);
6163
case 'wordcloud'
Lines changed: 249 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,249 @@
1+
function updateGeobubble(obj,geoIndex)
2+
3+
%-AXIS INDEX-%
4+
axIndex = obj.getAxisIndex(obj.State.Plot(geoIndex).AssociatedAxis);
5+
6+
%-GET STRUCTURES-%
7+
geoData = get(obj.State.Plot(geoIndex).Handle);
8+
9+
%-CHECK FOR MULTIPLE AXES-%
10+
[xsource, ysource] = findSourceAxis(obj,axIndex);
11+
12+
%-----------------------------------------------------------------------------%
13+
14+
%-PASE DATA-%
15+
bubbleRange = geoData.BubbleWidthRange;
16+
allLats = geoData.LatitudeData;
17+
allLons = geoData.LongitudeData;
18+
allSizes = rescale(geoData.SizeData, bubbleRange(1), bubbleRange(2));
19+
20+
if ~isempty(geoData.ColorData)
21+
allNames = geoData.ColorData;
22+
23+
[groupNames, ~, allNamesIdx] = unique(allNames);
24+
nGroups = length(groupNames);
25+
byGroups = true;
26+
27+
for g=1:nGroups
28+
29+
idx = g == allNamesIdx;
30+
31+
group{g} = char(groupNames(g));
32+
lat{g} = allLats(idx);
33+
lon{g} = allLons(idx);
34+
sData{g} = allSizes(idx);
35+
36+
if length(lat{g}) < 2
37+
lat{g} = [allLats(idx); NaN; NaN];
38+
lon{g} = [allLons(idx); NaN; NaN];
39+
sData{g} = [allSizes(idx); NaN; NaN];
40+
end
41+
42+
end
43+
44+
else
45+
lat{1} = allLats;
46+
lon{1} = allLons;
47+
sData{1} = allSizes;
48+
nGroups = 1;
49+
byGroups = false;
50+
end
51+
52+
%-----------------------------------------------------------------------------%
53+
54+
%=============================================================================%
55+
%
56+
%-SETTING GEOBBUBLE PLOT-%
57+
%
58+
%=============================================================================%
59+
60+
for g = 1:nGroups
61+
62+
%-------------------------------------------------------------------------%
63+
64+
%-get current trace index-%
65+
p = geoIndex;
66+
67+
if g > 1
68+
obj.PlotOptions.nPlots = obj.PlotOptions.nPlots + 1;
69+
p = obj.PlotOptions.nPlots;
70+
end
71+
72+
%-------------------------------------------------------------------------%
73+
74+
%-set scattergeo type-%
75+
obj.data{p}.type = 'scattergeo';
76+
77+
%-------------------------------------------------------------------------%
78+
79+
%-set scattergeo mode-%
80+
obj.data{p}.mode = 'markers';
81+
82+
%-------------------------------------------------------------------------%
83+
84+
%-set plot data-%
85+
obj.data{p}.lat = lat{g};
86+
obj.data{p}.lon = lon{g};
87+
88+
%-------------------------------------------------------------------------%
89+
90+
%-get marker setting-%
91+
marker = struct();
92+
marker.size = sData{g}*1.25;
93+
marker.color = sprintf('rgb(%f,%f,%f)', 255*geoData.BubbleColorList(g ,:));
94+
marker.line.color = 'rgb(255, 255, 255)';
95+
96+
%-------------------------------------------------------------------------%
97+
98+
%-set marker field-%
99+
obj.data{p}.marker = marker;
100+
101+
%-------------------------------------------------------------------------%
102+
103+
%-ASSOCIATE GEO-AXES LAYOUT-%
104+
obj.data{p}.geo = sprintf('geo%d', xsource+1);
105+
106+
%-------------------------------------------------------------------------%
107+
108+
%-legend-%
109+
if byGroups
110+
obj.data{p}.name = group{g};
111+
obj.data{p}.legendgroup = obj.data{p}.name;
112+
obj.data{p}.showlegend = true;
113+
end
114+
115+
%-------------------------------------------------------------------------%
116+
117+
end
118+
119+
%=============================================================================%
120+
%
121+
%-SETTING LAYOUT-%
122+
%
123+
%=============================================================================%
124+
125+
%-set domain geo plot-%
126+
xo = geoData.Position(1);
127+
yo = geoData.Position(2);
128+
w = geoData.Position(3);
129+
h = geoData.Position(4);
130+
131+
geo.domain.x = min([xo xo + w],1);
132+
geo.domain.y = min([yo yo + h],1);
133+
134+
%-----------------------------------------------------------------------------%
135+
136+
%-setting projection-%
137+
geo.projection.type = 'mercator';
138+
139+
%-----------------------------------------------------------------------------%
140+
141+
%-setting basemap-%
142+
geo.framecolor = 'rgb(120,120,120)';
143+
144+
if strcmpi(geoData.Basemap, 'streets-light')
145+
geo.oceancolor = 'rgba(20,220,220,1)';
146+
geo.landcolor = 'rgba(20,220,220,0.2)';
147+
elseif strcmpi(geoData.Basemap, 'colorterrain')
148+
geo.oceancolor = 'rgba(118,165,225,0.6)';
149+
geo.landcolor = 'rgba(190,180,170,1)';
150+
geo.showcountries = true;
151+
geo.showlakes = true;
152+
end
153+
154+
geo.showocean = true;
155+
geo.showcoastlines = false;
156+
geo.showland = true;
157+
158+
%-----------------------------------------------------------------------------%
159+
160+
%-setting latitude axis-%
161+
geo.lataxis.range = geoData.LatitudeLimits;
162+
163+
if strcmpi(geoData.GridVisible, 'on')
164+
geo.lataxis.showgrid = true;
165+
geo.lataxis.gridwidth = 0.5;
166+
geo.lataxis.gridcolor = 'rgba(38.250000,38.250000,38.250000,0.150000)';
167+
end
168+
169+
%-----------------------------------------------------------------------------%
170+
171+
%-setting longitude axis-%
172+
geo.lonaxis.range = geoData.LongitudeLimits;
173+
174+
if strcmpi(geoData.GridVisible, 'on')
175+
geo.lonaxis.showgrid = true;
176+
geo.lonaxis.gridwidth = 0.5;
177+
geo.lonaxis.gridcolor = 'rgba(38.250000,38.250000,38.250000,0.150000)';
178+
end
179+
180+
%-----------------------------------------------------------------------------%
181+
182+
%-set map center-%
183+
geo.center.lat = geoData.MapCenter(1);
184+
geo.center.lon = geoData.MapCenter(2);
185+
186+
%-----------------------------------------------------------------------------%
187+
188+
%-set better resolution-%
189+
geo.resolution = '50';
190+
191+
%-----------------------------------------------------------------------------%
192+
193+
%-set geo axes to layout-%
194+
obj.layout = setfield(obj.layout, sprintf('geo%d', xsource+1), geo);
195+
196+
%-----------------------------------------------------------------------------%
197+
198+
%-remove any annotation text-%
199+
istitle = length(geoData.Title) > 0;
200+
obj.layout.annotations{1}.text = ' ';
201+
obj.layout.annotations{1}.showarrow = false;
202+
203+
%-----------------------------------------------------------------------------%
204+
205+
%-layout title-%
206+
if istitle
207+
obj.layout.annotations{1}.text = sprintf('<b>%s</b>', geoData.Title);
208+
obj.layout.annotations{1}.xref = 'paper';
209+
obj.layout.annotations{1}.yref = 'paper';
210+
obj.layout.annotations{1}.yanchor = 'top';
211+
obj.layout.annotations{1}.xanchor = 'middle';
212+
obj.layout.annotations{1}.x = mean(geo.domain.x);
213+
obj.layout.annotations{1}.y = 0.96;
214+
obj.layout.annotations{1}.font.color = 'black';
215+
obj.layout.annotations{1}.font.family = matlab2plotlyfont(geoData.FontName);
216+
obj.layout.annotations{1}.font.size = 1.5*geoData.FontSize;
217+
end
218+
219+
%-----------------------------------------------------------------------------%
220+
221+
%-setting legend-%
222+
if byGroups
223+
obj.layout.showlegend = true;
224+
obj.layout.legend.borderwidth = 1;
225+
obj.layout.legend.bordercolor = 'rgba(0,0,0,0.2)';
226+
obj.layout.legend.font.family = matlab2plotlyfont(geoData.FontName);
227+
obj.layout.legend.font.size = 1.0*geoData.FontSize;
228+
229+
if length(geoData.ColorLegendTitle) > 0
230+
obj.layout.legend.title.text = sprintf('<b>%s</b>', geoData.ColorLegendTitle);
231+
obj.layout.legend.title.side = 'top';
232+
obj.layout.legend.title.font.family = matlab2plotlyfont(geoData.FontName);
233+
obj.layout.legend.title.font.size = 1.2*geoData.SizeLegendTitle;
234+
obj.layout.legend.title.font.color = 'black';
235+
end
236+
237+
obj.layout.legend.x = geo.domain.x(end) * 0.980;
238+
obj.layout.legend.y = geo.domain.y(end) * 1.001;
239+
obj.layout.legend.xanchor = 'left';
240+
obj.layout.legend.yanchor = 'top';
241+
obj.layout.legend.itemsizing = 'constant';
242+
obj.layout.legend.itemwidth = 30;
243+
obj.layout.legend.tracegroupgap = 0.01;
244+
obj.layout.legend.traceorder = 'normal';
245+
obj.layout.legend.valign = 'middle';
246+
end
247+
248+
%-----------------------------------------------------------------------------%
249+
end

0 commit comments

Comments
 (0)