Skip to content
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

fixing issue 162 + issue 233 #270

Closed
wants to merge 6 commits into from
Closed
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
2 changes: 1 addition & 1 deletion plotly/plotlyfig.m
Original file line number Diff line number Diff line change
Expand Up @@ -838,7 +838,7 @@ function delete(obj)
fnmod = fn;

try
for d = 1:length(fn);
for d = 1:length(fn)

% clean up axis keys
if any(strfind(fn{d},'xaxis')) || any(strfind(fn{d},'yaxis'))
Expand Down
117 changes: 74 additions & 43 deletions plotly/plotlyfig_aux/core/updateAxis.m
Original file line number Diff line number Diff line change
Expand Up @@ -253,12 +253,42 @@

%-xaxis range-%
xaxis.range = log10(axis_data.XLim);
%-xaxis autotick-%
xaxis.autotick = true;
%-xaxis nticks-%
xaxis.nticks = length(axis_data.XTick) + 1;

elseif strcmp(xaxis.type,'linear')

if strcmp(axis_data.XTickLabelMode,'auto')
%-xaxis range-%
xaxis.range = axis_data.XLim;
if isnumeric(axis_data.XLim)
xaxis.range = axis_data.XLim;

elseif isduration(axis_data.XLim)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice

[temp,type] = convertDuration(axis_data.XLim);

if (~isduration(temp))
xaxis.range = temp;
xaxis.type = 'duration';
xaxis.title = type;
else
xaxis.autorange = true;
xaxis.type = 'duration - specified format';
end

elseif isdatetime(axis_data.XLim)
xaxis.range = convertDate(axis_data.XLim);
xaxis.type = 'date';
else
% data is a category type
end

%-xaxis autotick-%
xaxis.autotick = true;
%-xaxis numticks-%
xaxis.nticks = length(axis_data.XTick)+1;

else
%-xaxis show tick labels-%
if isempty(axis_data.XTickLabel)
Expand All @@ -267,45 +297,29 @@
%-xaxis autorange-%
xaxis.autorange = true;
else
try datevec(axis_data.XTickLabel(1,:),axis_data.UserData.plotly.xdateformat);
%-xaxis type date-%
xaxis.type = 'date';
try
%find numbers in labels
labelnums = find(~isnan(xlabels));
%-yaxis type linear-%
xaxis.type = 'linear';
%-range (overwrite)-%
xaxis.range = [convertDate(datenum(axis_data.XTickLabel(1,:),axis_data.UserData.plotly.xdateformat)), ...
convertDate(datenum(axis_data.XTickLabel(end,:),axis_data.UserData.plotly.xdateformat))];
delta = (xlabels(labelnums(2)) - xlabels(labelnums(1)))/(labelnums(2)-labelnums(1));
xaxis.range = [xlabels(labelnums(1))-delta*(labelnums(1)-1) xlabels(labelnums(1)) + (length(xlabels)-labelnums(1))*delta];
%-yaxis autotick-%
xaxis.autotick = true;
%-yaxis numticks-%
xaxis.nticks = length(axis_data.XTick) + 1;
catch
%x-axis labels
xlabels = str2double(axis_data.XTickLabel);
try
%find numbers in labels
labelnums = find(~isnan(xlabels));
%-yaxis type linear-%
xaxis.type = 'linear';
%-range (overwrite)-%
delta = (xlabels(labelnums(2)) - xlabels(labelnums(1)))/(labelnums(2)-labelnums(1));
xaxis.range = [xlabels(labelnums(1))-delta*(labelnums(1)-1) xlabels(labelnums(1)) + (length(xlabels)-labelnums(1))*delta];
%-yaxis autotick-%
xaxis.autotick = true;
%-yaxis numticks-%
xaxis.nticks = length(axis_data.XTick) + 1;
catch
%-yaxis type category-%
xaxis.type = 'category';
%-range (overwrite)-%
xaxis.autorange = true;
%-yaxis autotick-%
xaxis.autotick = true;
end
%-yaxis type category-%
xaxis.type = 'category';
%-range (overwrite)-%
xaxis.autorange = true;
%-yaxis autotick-%
xaxis.autotick = true;
end
end
end
end

%-xaxis autotick-%
xaxis.autotick = true;
%-xaxis numticks-%
xaxis.nticks = length(axis_data.XTick) + 1;

end

%-------------------------------------------------------------------------%
Expand Down Expand Up @@ -387,11 +401,6 @@

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

%-yaxis range-%
yaxis.range = axis_data.YLim;

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

%-yaxis show tick labels-%
yaxis.showticklabels = true;

Expand Down Expand Up @@ -450,12 +459,34 @@
%-xaxis nticks-%
yaxis.nticks = length(axis_data.YTick) + 1;

elseif strcmp(yaxis.type,'linear')

%-xaxis range-%
yaxis.range = axis_data.YLim;
elseif strcmp(yaxis.type,'linear')

if strcmp(axis_data.YTickLabelMode,'auto')

%-xaxis range-%
if isnumeric(axis_data.YLim)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a way that you can make this more DRY? It seems like you have essentially the same code for the X and Y access. Can you put this into a function that works for both axes for example? https://en.wikipedia.org/wiki/Don%27t_repeat_yourself

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually a lot of field are changed and we're not changing the same field each time so it won't be optimal to write everything in a function don't you think

yaxis.range = axis_data.YLim;

elseif isduration(axis_data.YLim)

[temp,type] = convertDuration(axis_data.YLim);

if (~isduration(temp))
yaxis.range = temp;
yaxis.type = 'duration';
yaxis.title = type;
else
yaxis.autorange = true;
yaxis.type = 'duration - specified format';
end

elseif isdatetime(axis_data.YLim)
yaxis.range = convertDate(axis_data.YLim);
yaxis.type = 'date';
else
% data is a category type
end

%-yaxis autotick-%
yaxis.autotick = true;
%-yaxis numticks-%
Expand Down
20 changes: 20 additions & 0 deletions plotly/plotlyfig_aux/core/updateData.m
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,16 @@
obj.data{dataIndex}.x = convertDate(obj.data{dataIndex}.x);
end

% check for xaxis duration
if strcmpi(xaxis.type, 'duration')
obj.data{dataIndex}.x = convertDuration(obj.data{dataIndex}.x);
end

% check for xaxis duration with input format
if strcmpi(xaxis.type, 'duration - specified format')
obj.data{dataIndex}.x = get(obj.State.Plot(dataIndex).AssociatedAxis,'XTickLabel');
end

% check for xaxis categories
if strcmpi(xaxis.type, 'category') && ...
~strcmp(obj.data{dataIndex}.type,'box')
Expand All @@ -110,6 +120,16 @@
obj.data{dataIndex}.y = convertDate(obj.data{dataIndex}.y);
end

% check for yaxis duration
if strcmpi(yaxis.type, 'duration')
obj.data{dataIndex}.y = convertDuration(obj.data{dataIndex}.y);
end

% check for yaxis duration with input format
if strcmpi(yaxis.type, 'duration - specified format')
obj.data{dataIndex}.y = get(obj.State.Plot(dataIndex).AssociatedAxis,'YTickLabel');
end

% check for yaxis categories
if strcmpi(yaxis.type, 'category') && ...
~strcmp(obj.data{dataIndex}.type,'box')
Expand Down
7 changes: 4 additions & 3 deletions plotly/plotlyfig_aux/helpers/convertDate.m
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
function converted = convertDate(date)
converted = (date - datenum(1969,12,31,19,00,00))*1000*60*60*24;
end
function convertedDate = convertDate(date)
date.Format = 'yyyy-MM-dd HH:mm:ss';
convertedDate = char(date);
end
24 changes: 24 additions & 0 deletions plotly/plotlyfig_aux/helpers/convertDuration.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
function [converted,type] = convertDuration(duration)
switch (duration.Format)
case 's'
converted = seconds(duration);
type = 'sec';
case 'm'
converted = minutes(duration);
type = 'min';
case 'h'
converted = hours(duration);
type = 'hr';
case 'd'
converted = days(duration);
type = 'days';
case 'y'
converted = years(duration);
type = 'yrs';
otherwise
%no convertion is applied
converted = duration;
type = '';

end
end