Skip to content

Add geoplot functionality in fig2plotly #397

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
Sep 25, 2021
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
10 changes: 7 additions & 3 deletions plotly/plotlyfig.m
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@
obj.PlotlyDefaults.CaptionMarginIncreaseFactor = 1.2;
obj.PlotlyDefaults.MinCaptionMargin = 80;
obj.PlotlyDefaults.IsLight = false;
obj.PlotlyDefaults.isGeoaxis = false;

%-State-%
obj.State.Figure = [];
Expand Down Expand Up @@ -741,11 +742,13 @@ function validate(obj)
% update annotations
for n = 1:obj.State.Figure.NumTexts
try
if ~obj.PlotOptions.is_headmap_axis
updateAnnotation(obj,n);
else
if obj.PlotOptions.is_headmap_axis
updateHeatmapAnnotation(obj,n);
obj.PlotOptions.CleanFeedTitle = false;
elseif obj.PlotlyDefaults.isGeoaxis
% TODO
else
updateAnnotation(obj,n);
end
catch
% TODO to the future
Expand Down Expand Up @@ -1060,6 +1063,7 @@ function delete(obj)
|| strcmpi(fieldname,'yaxis') || strcmpi(fieldname,'cone')...
|| strcmpi(fieldname,'legend') || strcmpi(fieldname,'histogram')...
|| strcmpi(fieldname,'scatter') || strcmpi(fieldname,'line')...
|| strcmpi(fieldname,'scattergeo') ...
)
fprintf(['\nWhoops! ' exception.message(1:end-1) ' in ' fieldname '\n\n']);
end
Expand Down
20 changes: 14 additions & 6 deletions plotly/plotlyfig_aux/core/updateAxis.m
Original file line number Diff line number Diff line change
Expand Up @@ -61,20 +61,28 @@
is_headmap_axis = isfield(axis_data, 'XDisplayData');
obj.PlotOptions.is_headmap_axis = is_headmap_axis;

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

%-check if geo-axis-%
isGeoaxis = isfield(axis_data, 'Type') && strcmpi(axis_data.Type, 'geoaxes');
obj.PlotlyDefaults.isGeoaxis = isGeoaxis;

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

%-xaxis-%
if ~is_headmap_axis
xaxis = extractAxisData(obj,axis_data,'X');
if is_headmap_axis
xaxis = extractHeatmapAxisData(obj,axis_data, 'X');
else
xaxis = extractHeatmapAxisData(obj,axis_data,'X');
xaxis = extractAxisData(obj,axis_data, 'X');
end

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

%-yaxis-%
if ~is_headmap_axis
yaxis = extractAxisData(obj,axis_data,'Y');
if is_headmap_axis
yaxis = extractHeatmapAxisData(obj,axis_data, 'Y');
else
yaxis = extractHeatmapAxisData(obj,axis_data,'Y');
yaxis = extractAxisData(obj,axis_data, 'Y');
end

%-------------------------------------------------------------------------%
Expand Down
14 changes: 12 additions & 2 deletions plotly/plotlyfig_aux/core/updateData.m
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@

switch lower(obj.State.Plot(dataIndex).Class)

%--GEOAXES SPECIAL CASE--%
case 'geoaxes'
UpdateGeoAxes(obj, dataIndex);

%--CORE PLOT OBJECTS--%
case 'scatterhistogram'
updateScatterhistogram(obj, dataIndex);
Expand All @@ -65,7 +69,11 @@
updateImage3D(obj, dataIndex);
end
case 'line'
updateLineseries(obj, dataIndex);
if obj.PlotlyDefaults.isGeoaxis
updateGeoPlot(obj, dataIndex);
else
updateLineseries(obj, dataIndex);
end
case 'categoricalhistogram'
updateCategoricalHistogram(obj, dataIndex);
case 'histogram'
Expand Down Expand Up @@ -119,7 +127,9 @@
updateQuivergroup(obj, dataIndex);
case 'scatter'
if strcmpi(obj.State.Axis(dataIndex).Handle.Type, 'polaraxes')
updateScatterPolar(obj, dataIndex);
updateScatterPolar(obj, dataIndex);
elseif obj.PlotlyDefaults.isGeoaxis
updateGeoScatter(obj, dataIndex);
else
updateScatter(obj, dataIndex);
end
Expand Down
135 changes: 135 additions & 0 deletions plotly/plotlyfig_aux/handlegraphics/UpdateGeoAxes.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
function UpdateGeoAxes(obj, geoIndex)

%-AXIS INDEX-%
axIndex = obj.getAxisIndex(obj.State.Plot(geoIndex).AssociatedAxis);

%-GET DATA STRUCTURE- %
geoData = get(obj.State.Plot(geoIndex).Handle);

%-CHECK FOR MULTIPLE AXES-%
[xsource, ysource] = findSourceAxis(obj,axIndex);

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

%-set domain geo plot-%
xo = geoData.Position(1);
yo = geoData.Position(2);
w = geoData.Position(3);
h = geoData.Position(4);

geo.domain.x = min([xo xo + w],1);
geo.domain.y = min([yo yo + h],1);

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

%-setting projection-%
geo.projection.type = 'mercator';

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

%-setting basemap-%
geo.framecolor = 'rgb(120,120,120)';

if strcmpi(geoData.Basemap, 'streets-light')
geo.oceancolor = 'rgba(20,220,220,1)';
geo.landcolor = 'rgba(20,220,220,0.2)';
elseif strcmpi(geoData.Basemap, 'colorterrain')
geo.oceancolor = 'rgba(118,165,225,0.6)';
geo.landcolor = 'rgba(190,180,170,1)';
geo.showcountries = true;
geo.showlakes = true;
end

geo.showocean = true;
geo.showcoastlines = false;
geo.showland = true;

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

%-setting latitude axis-%
latTick = geoData.LatitudeAxis.TickValues;

geo.lataxis.range = geoData.LatitudeLimits;
geo.lataxis.tick0 = latTick(1);
geo.lataxis.dtick = mean(diff(latTick));

if strcmpi(geoData.Grid, 'on')
geo.lataxis.showgrid = true;
geo.lataxis.gridwidth = geoData.LineWidth;
geo.lataxis.gridcolor = sprintf('rgba(%f,%f,%f,%f)', 255*geoData.GridColor, geoData.GridAlpha);
end

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

%-setting longitude axis-%
lonTick = geoData.LongitudeAxis.TickValues;

geo.lonaxis.range = geoData.LongitudeLimits;
geo.lonaxis.tick0 = lonTick(1);
geo.lonaxis.dtick = mean(diff(lonTick));

if strcmpi(geoData.Grid, 'on')
geo.lonaxis.showgrid = true;
geo.lonaxis.gridwidth = geoData.LineWidth;
geo.lonaxis.gridcolor = sprintf('rgba(%f,%f,%f,%f)', 255*geoData.GridColor, geoData.GridAlpha);
end

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

%-set map center-%
geo.center.lat = geoData.MapCenter(1);
geo.center.lon = geoData.MapCenter(2);

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

%-set better resolution-%
geo.resolution = '50';

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

%-set geo axes to layout-%
obj.layout = setfield(obj.layout, sprintf('geo%d', xsource+1), geo);

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

%-TEXT STTINGS-%
isText = false;
child = geoData.Children;
t = 1;

for n=1:length(child)
if strcmpi(child(n).Type, 'text')
isText = true;
texts{t} = child(t).String;
lats(t) = child(t).Position(1);
lons(t) = child(t).Position(2);
sizes(t) = child(t).FontSize;
families{t} = matlab2plotlyfont(child(t).FontName);
colors{t} = sprintf('rgb(%f,%f,%f)', child(t).Color);

if strcmpi(child(t).HorizontalAlignment, 'left')
pos{t} = 'right';
elseif strcmpi(child(t).HorizontalAlignment, 'right')
pos{t} = 'left';
else
pos{t} = child(t).HorizontalAlignment;
end

t = t + 1;
end
end

if isText
obj.data{geoIndex}.type = 'scattergeo';
obj.data{geoIndex}.mode = 'text';
obj.data{geoIndex}.text = texts;
obj.data{geoIndex}.lat = lats;
obj.data{geoIndex}.lon = lons;
obj.data{geoIndex}.geo = obj.data{geoIndex-1}.geo;

obj.data{geoIndex}.textfont.size = sizes;
obj.data{geoIndex}.textfont.color = colors;
obj.data{geoIndex}.textfont.family = families;
obj.data{geoIndex}.textposition = pos;
end
end
53 changes: 53 additions & 0 deletions plotly/plotlyfig_aux/handlegraphics/updateGeoPlot.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
function updateGeoPlot(obj,geoIndex)

%-AXIS INDEX-%
axIndex = obj.getAxisIndex(obj.State.Plot(geoIndex).AssociatedAxis);

%-GET STRUCTURES-%
geoData = get(obj.State.Plot(geoIndex).Handle);
axisData = geoData.Parent;
figureData = get(obj.State.Figure.Handle);

%-CHECK FOR MULTIPLE AXES-%
[xsource, ysource] = findSourceAxis(obj,axIndex);

%-ASSOCIATE GEO-AXES LAYOUT-%
obj.data{geoIndex}.geo = sprintf('geo%d', xsource+1);

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

%-set scattergeo type-%
obj.data{geoIndex}.type = 'scattergeo';

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

%-set scattergeo mode-%
obj.data{geoIndex}.mode = 'lines+markers';

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

%-set plot data-%
obj.data{geoIndex}.lat = geoData.LatitudeData;
obj.data{geoIndex}.lon = geoData.LongitudeData;

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

%-get marker setting-%
[marker, linee] = extractGeoLinePlusMarker(geoData, axisData);

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

%-set marker field-%
obj.data{geoIndex}.marker = marker;

if strcmpi(geoData.Marker, 'none')
obj.data{geoIndex}.mode = 'lines';
end

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

%-set line field-%
obj.data{geoIndex}.line = linee;

%-------------------------------------------------------------------------%
end
44 changes: 44 additions & 0 deletions plotly/plotlyfig_aux/handlegraphics/updateGeoScatter.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
function updateGeoScatter(obj,geoIndex)

%-AXIS INDEX-%
axIndex = obj.getAxisIndex(obj.State.Plot(geoIndex).AssociatedAxis);

%-GET STRUCTURES-%
geoData = get(obj.State.Plot(geoIndex).Handle);
axisData = geoData.Parent;
figureData = get(obj.State.Figure.Handle);

%-CHECK FOR MULTIPLE AXES-%
[xsource, ysource] = findSourceAxis(obj,axIndex);

%-ASSOCIATE GEO-AXES LAYOUT-%
obj.data{geoIndex}.geo = sprintf('geo%d', xsource+1);

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

%-set scattergeo type-%
obj.data{geoIndex}.type = 'scattergeo';

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

%-set scattergeo mode-%
obj.data{geoIndex}.mode = 'markers';

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

%-set plot data-%
obj.data{geoIndex}.lat = geoData.LatitudeData;
obj.data{geoIndex}.lon = geoData.LongitudeData;

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

%-get marker setting-%
marker = extractGeoMarker(geoData, axisData);

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

%-set marker field-%
obj.data{geoIndex}.marker = marker;

%-------------------------------------------------------------------------%
end
2 changes: 1 addition & 1 deletion plotly/plotlyfig_aux/handlegraphics/updateWordcloud.m
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ function updateWordcloud(obj,scatterIndex)
%-------------------------------------------------------------------------%

%-det font family-%
obj.data{scatterIndex}.textfont.family = matlab2plotlyfont(scatter_data.FontName);;
obj.data{scatterIndex}.textfont.family = matlab2plotlyfont(scatter_data.FontName);

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

Expand Down
Loading