diff --git a/plotly/plotly_offline_aux/plotlyoffline.m b/plotly/plotly_offline_aux/plotlyoffline.m index 5acf0752..2b82e1e7 100644 --- a/plotly/plotly_offline_aux/plotlyoffline.m +++ b/plotly/plotly_offline_aux/plotlyoffline.m @@ -12,18 +12,21 @@ bundle_name = 'plotly-matlab-offline-bundle.js'; bundle_file = fullfile(plotly_js_folder, bundle_name); - % check that the bundle exists - try - bundle = fileread(bundle_file); - % template dependencies - dep_script = sprintf('\n', ... - bundle); - catch - error(['Error reading: %s.\nPlease download the required ', ... - 'dependencies using: >>getplotlyoffline \n', ... - 'or contact support@plot.ly for assistance.'], ... - bundle_file); + % Check that the bundle exists, attempt to download if not + if ~exist(bundle_file, 'file') + %TODO move bundle_url to plotly config + bundle_url = 'http://cdn.plot.ly/plotly-latest.min.js'; + warning('No plotly offline file: %s.\nAttempting to download required dependency from %s',... + bundle_file,... + bundle_url) + bundle_dir = fileparts(bundle_file); + mkdir(bundle_dir); + websave(bundle_file, bundle_url); end + + % template dependencies + dep_script = sprintf('\n', bundle_file); + else dep_script = ''; end @@ -44,16 +47,16 @@ jlayout = m2json(plotlyfig.layout); clean_jdata = escapechars(jdata); clean_jlayout = escapechars(jlayout); - - % template environment vars + + % template environment vars plotly_domain = plotlyfig.UserData.PlotlyDomain; env_script = sprintf([''], plotly_domain, link_text); - - % template Plotly.plot + + % template Plotly.plot script = sprintf(['\n Plotly.plot("%s", %s, %s).then(function(){'... '\n $(".%s.loading").remove();' ... '\n $(".link--embedview").text("%s");'... @@ -61,15 +64,18 @@ id, link_text); plotly_script = sprintf(['\n
Drawing...
' ... + 'color: rgb(50,50,50);">' ... << TODO insert 'Drawing...' as text in this div if jquery available '\n
' ... '
\n'], id, id, height, width, ... script); - % template entire script - offline_script = [dep_script env_script plotly_script]; + % The plot function requires jquery (slim, min, only) as a dependency + % to use the $() operator. If it's not there, then the 'Drawing' text + % and embeddable link don't get updated on plot... temporarily removed + offline_script = sprintf('\n%s\n%s\n\n\n%s\n%s\n', dep_script, env_script, plotly_script); + filename = plotlyfig.PlotOptions.FileName; % remove the whitespace from the filename diff --git a/plotly/plotlyfig.m b/plotly/plotlyfig.m index 1bc54841..33608d09 100644 --- a/plotly/plotlyfig.m +++ b/plotly/plotlyfig.m @@ -14,6 +14,7 @@ properties (SetObservable) UserData;% credentials/configuration/verbose PlotOptions; % filename,fileopt,world_readable + RenderFig = true; % uses matlab figure rendering to allow use of the matlab API instead of direct creation using plotly native end properties (Hidden = true) @@ -110,6 +111,9 @@ % initialize autoupdate key updatekey = false; + % initialize render_fig flag + obj.RenderFig = true; + % parse inputs switch nargin @@ -121,6 +125,10 @@ fig_han = varargin{1}; updatekey = true; end + elseif ischar(varargin{1}) && strcmp(varargin{1},'-norender') + obj.RenderFig = false; + updatekey = false; + parseinit = 2; else errkey = 'plotlyfigConstructor:invalidInputs'; error(errkey , plotlymsg(errkey)); @@ -135,6 +143,10 @@ updatekey = true; parseinit = 2; end + elseif ischar(varargin{1}) && strcmp(varargin{1},'-norender') + obj.RenderFig = false; + updatekey = false; + parseinit = 2; else parseinit = 1; end @@ -191,32 +203,35 @@ end end - % create figure/axes if empty - if isempty(fig_han) - fig_han = figure; - axes; - end - - % plotly figure default style - set(fig_han,'Name',obj.PlotOptions.FileName,'Color',[1 1 1],'NumberTitle','off', 'Visible', obj.PlotOptions.Visible); - - % figure state - obj.State.Figure.Handle = fig_han; - - % update - if updatekey - obj.update; + % Only init the figure renderer if the -norender flag is not passed + if obj.RenderFig + % create figure/axes if empty + if isempty(fig_han) + fig_han = figure; + axes; + end + + % plotly figure default style + set(fig_han,'Name',obj.PlotOptions.FileName,'Color',[1 1 1],'NumberTitle','off', 'Visible', obj.PlotOptions.Visible); + + % figure state + obj.State.Figure.Handle = fig_han; + + % update + if updatekey + obj.update; + end + + % add figure listeners + addlistener(obj.State.Figure.Handle,'Visible','PostSet',@(src,event)updateFigureVisible(obj,src,event)); + addlistener(obj.State.Figure.Handle,'Name','PostSet',@(src,event)updateFigureName(obj,src,event)); + + % add plot options listeners + addlistener(obj,'PlotOptions','PostSet',@(src,event)updatePlotOptions(obj,src,event)); + + % add user data listeners + addlistener(obj,'UserData','PostSet',@(src,event)updateUserData(obj,src,event)); end - - % add figure listeners - addlistener(obj.State.Figure.Handle,'Visible','PostSet',@(src,event)updateFigureVisible(obj,src,event)); - addlistener(obj.State.Figure.Handle,'Name','PostSet',@(src,event)updateFigureName(obj,src,event)); - - % add plot options listeners - addlistener(obj,'PlotOptions','PostSet',@(src,event)updatePlotOptions(obj,src,event)); - - % add user data listeners - addlistener(obj,'UserData','PostSet',@(src,event)updateUserData(obj,src,event)); end %-------------------------USER METHODS----------------------------%