Skip to content

Commit 7a060e9

Browse files
authored
Merge pull request #369 from plotly/trueOffline
Merged Fig <-> Nofig, now truely offline
2 parents d6ad933 + d4683dc commit 7a060e9

File tree

4 files changed

+88
-1033
lines changed

4 files changed

+88
-1033
lines changed

Diff for: plotly/plotly_aux/plotly.m

+22-24
Original file line numberDiff line numberDiff line change
@@ -11,61 +11,59 @@
1111
% For full documentation and examples, see https://plot.ly/api
1212
origin = 'plot';
1313
offline_given = true;
14-
struct_provided = false;
14+
writeFile=true;
1515

1616
if isstruct(varargin{end})
1717
structargs = varargin{end};
18-
f = lower(fieldnames(structargs));
19-
20-
if any(strcmp('offline', f))
21-
offline = getfield(structargs, 'offline');
18+
f = fieldnames(structargs);
19+
20+
idx = cellfun(@(x) strcmpi(x,'offline'), f);
21+
if sum(idx)==1
22+
offline = structargs.(f{idx});
2223
offline_given = offline;
2324
else
2425
offline = false;
2526
offline_given = offline;
2627
end
2728

28-
29-
if ~any(strcmp('filename',f))
29+
if ~any(strcmp('filename',lower(f)))
3030
if offline
3131
structargs.filename = 'untitled';
3232
else
3333
structargs.filename = NaN;
3434
end
3535
end
36-
if ~any(strcmp('fileopt',f))
36+
if ~any(strcmp('fileopt',lower(f)))
3737
structargs.fileopt = NaN;
3838
end
3939

40-
41-
struct_provided = true;
42-
40+
idx = cellfun(@(x) strcmpi(x,'writefile'),f);
41+
if sum(idx)==1
42+
writeFile=structargs.(f{idx});
43+
end
44+
4345
args = varargin(1:(end-1));
4446

4547
else
46-
4748
if offline_given
4849
structargs = struct('filename', 'untitled', 'fileopt', NaN);
4950
else
50-
structargs = struct('filename', NaN,'fileopt',NaN);
51+
structargs = struct('filename', NaN, 'fileopt', NaN);
5152
end
5253
args = varargin(1:end);
53-
struct_provided = false;
54+
end
55+
56+
if ~writeFile
57+
offline_given = true;
5458
end
5559

5660
if offline_given
57-
if (struct_provided)
58-
nofig_obj = plotlynofig(varargin{1:end-1}, structargs);
59-
else
60-
nofig_obj = plotlynofig(varargin{1:end}, structargs);
61-
end
62-
nofig_obj.layout.width = 840;
63-
nofig_obj.layout.height = 630;
64-
response = nofig_obj.plotly;
65-
61+
obj = plotlyfig(args, structargs);
62+
obj.layout.width = 840;
63+
obj.layout.height = 630;
64+
response = obj.plotly;
6665
else
6766
response = makecall(args, origin, structargs);
6867
end
6968

70-
7169
end

Diff for: plotly/plotly_offline_aux/plotlyoffline.m

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,6 @@
8585
plotly_offline_file = strrep(plotly_offline_file, ' ', '%20');
8686

8787
% return the local file url to be rendered in the browser
88-
response = ['file://' plotly_offline_file];
88+
response = ['file:///' plotly_offline_file];
8989

9090
end

Diff for: plotly/plotlyfig.m

+65-43
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
%----CONSTRUCTOR---%
3333
function obj = plotlyfig(varargin)
34-
34+
3535
%-Core-%
3636
obj.data = {};
3737
obj.layout = struct();
@@ -47,6 +47,7 @@
4747
obj.PlotOptions.ShowURL = true;
4848
obj.PlotOptions.OpenURL = true;
4949
obj.PlotOptions.Strip = false;
50+
obj.PlotOptions.WriteFile = true;
5051
obj.PlotOptions.Visible = 'on';
5152
obj.PlotOptions.TriangulatePatch = false;
5253
obj.PlotOptions.StripMargins = false;
@@ -73,7 +74,8 @@
7374
obj.UserData.ApiKey,...
7475
obj.UserData.PlotlyDomain] = signin;
7576
catch
76-
if obj.PlotOptions.Offline
77+
idx=find(cellfun(@(x) strcmpi(x,'offline'), varargin))+1;
78+
if (nargin>1 && ~isempty(idx) && varargin{idx}) || (obj.PlotOptions.Offline)
7779
obj.UserData.Username = 'offlineUser';
7880
obj.UserData.ApiKey = '';
7981
obj.UserData.PlotlyDomain = 'https://plot.ly';
@@ -127,6 +129,8 @@
127129
% initialize autoupdate key
128130
updatekey = false;
129131

132+
noFig = false;
133+
130134
% parse inputs
131135
switch nargin
132136

@@ -152,6 +156,16 @@
152156
updatekey = true;
153157
parseinit = 2;
154158
end
159+
elseif iscell(varargin{1}) && isstruct(varargin{2})
160+
obj.data = varargin{1}{:};
161+
structargs = varargin{2};
162+
ff=fieldnames(structargs);
163+
for i=1:length(ff)
164+
varargin{2*i-1}=ff{i};
165+
varargin{2*i}=structargs.(ff{i});
166+
end
167+
noFig=true;
168+
parseinit = 1;
155169
else
156170
parseinit = 1;
157171
end
@@ -163,7 +177,7 @@
163177
end
164178

165179
% parse property/values
166-
for a = parseinit:2:nargin
180+
for a = parseinit:2:length(varargin)
167181
if(strcmpi(varargin{a},'filename'))
168182
obj.PlotOptions.FileName = varargin{a+1};
169183
% overwrite if filename provided
@@ -187,6 +201,9 @@
187201
if(strcmpi(varargin{a},'strip'))
188202
obj.PlotOptions.Strip = varargin{a+1};
189203
end
204+
if(strcmpi(varargin{a},'writeFile'))
205+
obj.PlotOptions.WriteFile = varargin{a+1};
206+
end
190207
if(strcmpi(varargin{a},'visible'))
191208
obj.PlotOptions.Visible = varargin{a+1};
192209
end
@@ -235,32 +252,36 @@
235252
end
236253
end
237254

238-
% create figure/axes if empty
239-
if isempty(fig_han)
240-
fig_han = figure;
241-
axes;
255+
if ~noFig
256+
% create figure/axes if empty
257+
if isempty(fig_han)
258+
fig_han = figure;
259+
axes;
260+
end
261+
262+
% plotly figure default style
263+
set(fig_han,'Name',obj.PlotOptions.FileName,'Color',[1 1 1],'NumberTitle','off', 'Visible', obj.PlotOptions.Visible);
264+
265+
% figure state
266+
obj.State.Figure.Handle = fig_han;
242267
end
243268

244-
% plotly figure default style
245-
set(fig_han,'Name',obj.PlotOptions.FileName,'Color',[1 1 1],'NumberTitle','off', 'Visible', obj.PlotOptions.Visible);
246-
247-
% figure state
248-
obj.State.Figure.Handle = fig_han;
249-
250269
% update
251270
if updatekey
252271
obj.update;
253272
end
254273

255-
% add figure listeners
256-
addlistener(obj.State.Figure.Handle,'Visible','PostSet',@(src,event)updateFigureVisible(obj,src,event));
257-
addlistener(obj.State.Figure.Handle,'Name','PostSet',@(src,event)updateFigureName(obj,src,event));
258-
259-
% add plot options listeners
260-
addlistener(obj,'PlotOptions','PostSet',@(src,event)updatePlotOptions(obj,src,event));
261-
262-
% add user data listeners
263-
addlistener(obj,'UserData','PostSet',@(src,event)updateUserData(obj,src,event));
274+
if ~noFig
275+
% add figure listeners
276+
addlistener(obj.State.Figure.Handle,'Visible','PostSet',@(src,event)updateFigureVisible(obj,src,event));
277+
addlistener(obj.State.Figure.Handle,'Name','PostSet',@(src,event)updateFigureName(obj,src,event));
278+
279+
% add plot options listeners
280+
addlistener(obj,'PlotOptions','PostSet',@(src,event)updatePlotOptions(obj,src,event));
281+
282+
% add user data listeners
283+
addlistener(obj,'UserData','PostSet',@(src,event)updateUserData(obj,src,event));
284+
end
264285
end
265286

266287
%-------------------------USER METHODS----------------------------%
@@ -495,27 +516,28 @@ function validate(obj)
495516
%layout
496517
args.layout = obj.layout;
497518

498-
%send to plotly
499-
if ~obj.PlotOptions.Offline
500-
response = plotly(obj.data, args);
501-
502-
%update response
503-
obj.url = response.url;
504-
obj.error = response.error;
505-
obj.warning = response.warning;
506-
obj.message = response.message;
507-
508-
%open url in browser
509-
if obj.PlotOptions.OpenURL
510-
web(response.url, '-browser');
511-
end
512-
else
513-
obj.url = plotlyoffline(obj);
514-
if obj.PlotOptions.OpenURL
515-
web(obj.url, '-browser');
516-
end
517-
end
518-
519+
if obj.PlotOptions.WriteFile
520+
%send to plotly
521+
if ~obj.PlotOptions.Offline
522+
response = plotly(obj.data, args);
523+
524+
%update response
525+
obj.url = response.url;
526+
obj.error = response.error;
527+
obj.warning = response.warning;
528+
obj.message = response.message;
529+
530+
%open url in browser
531+
if obj.PlotOptions.OpenURL
532+
web(response.url, '-browser');
533+
end
534+
else
535+
obj.url = plotlyoffline(obj);
536+
if obj.PlotOptions.OpenURL
537+
web(obj.url, '-browser');
538+
end
539+
end
540+
end
519541
end
520542

521543
%-----------------------FIGURE CONVERSION-------------------------%

0 commit comments

Comments
 (0)