Skip to content

Object oriented approach to plot.ly #8

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

Closed
wants to merge 15 commits into from
Closed
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Added overloaded plot method
Signed-off-by: oleg komarov <o.komarov11@imperial.ac.uk>
  • Loading branch information
oleg komarov committed May 30, 2014
commit 3cbde4fedc76789f578cb40e524b8bd822195da8
105 changes: 93 additions & 12 deletions oo/plotly.m
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
classdef plotly
% PLOTLY
% PLOTLY
% Summary of this class goes here
% Detailed explanation goes here

properties
User = '';
properties
User = '';
Response = '';
end

properties(Access = private)
Key = '';
end

methods
methods

function obj = plotly(user,key)
% PLOTLY Sign into plot.ly
Expand All @@ -28,11 +29,89 @@
end
end

function obj = signin(obj, user, key)
% SIGNIN Sign into plot.ly with username and key
function obj = plot(obj,varargin)
hf = figure('visible','off');
ha = axes('parent',hf);
plot(ha,varargin{:})
obj.fig2plotly(hf)
end

function obj = fig2plotly(obj, varargin)
% fig2plotly - plots a matlab figure object with PLOTLY
% [response] = fig2plotly()
% [response] = fig2plotly(gcf)
% [response] = fig2plotly(f)
% [response] = fig2plotly(gcf, 'property', value, ...)
% [response] = fig2plotly(f, 'property', value, ...)
% gcf - root figure object in the form of a double.
% f - root figure object in the form of a struct. Use f = get(gcf); to
% get the current figure struct.
% List of valid properties:
% 'name' - ('untitled')string, name of the plot
% 'strip' - (false)boolean, ignores all styling, uses plotly defaults
% 'open' - (true)boolean, opens a browser window with plot result
% response - a struct containing the result info of the plot
%
% For full documentation and examples, see https://plot.ly/api

%default input
f = get(gcf);
plot_name = 'untitled';
strip_style = false;
open_browser = true;

switch numel(varargin)
case 0
case 1
if isa(varargin{1}, 'double')
f = get(varargin{1});
end
if isa(varargin{1}, 'struct')
f = varargin{1};
end
otherwise
if isa(varargin{1}, 'double')
f = get(varargin{1});
end
if isa(varargin{1}, 'struct')
f = varargin{1};
end
if mod(numel(varargin),2)~=1
error('Invalid number of arguments')
else
for i=2:2:numel(varargin)
if strcmp('strip', varargin{i})
strip_style = varargin{i+1};
end
if strcmp('name', varargin{i})
plot_name = varargin{i+1};
end
if strcmp('open', varargin{i})
open_browser = varargin{i+1};
end
end
end
end

%convert figure into data and layout data structures
[data, layout, title] = convertFigure(f, strip_style);

if numel(title)>0 && strcmp('untitled', plot_name)
plot_name = title;
end

% send graph request
origin = 'plot';
structargs = struct('layout', layout, 'filename',plot_name, 'fileopt', 'overwrite');
obj.Response = makecall(data, obj.User, obj.Key, origin, structargs);

if open_browser
status = dos(['open ' obj.Response.url ' > nul 2> nul']);
if status==1
status = dos(['start ' obj.Response.url ' > nul 2> nul']);
end
end

obj.User = user;
obj.Key = key;
end

function obj = savecredentials(obj)
Expand All @@ -42,10 +121,13 @@
savecredentials(obj.User,obj.Key)
end
end

methods(Access = private)

% SIGNUP
function obj = signup(obj)
% SIGNUP

% Call external
response = signup;
if isempty(response), return, end
obj.User = response.un;
Expand All @@ -54,6 +136,7 @@

function obj = loadcredentials(obj)
% LOADCREDENTIALS

% Credentials file name
userhome = getuserdir();
if ispc
Expand Down Expand Up @@ -91,7 +174,5 @@

end
end


end

end