diff --git a/plotly/plotlyfig.m b/plotly/plotlyfig.m index 9b740525..19a6fc74 100644 --- a/plotly/plotlyfig.m +++ b/plotly/plotlyfig.m @@ -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')) diff --git a/plotly/plotlyfig_aux/core/updateAxis.m b/plotly/plotlyfig_aux/core/updateAxis.m index b6e04d24..cee95c0c 100644 --- a/plotly/plotlyfig_aux/core/updateAxis.m +++ b/plotly/plotlyfig_aux/core/updateAxis.m @@ -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) + [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) @@ -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 %-------------------------------------------------------------------------% @@ -387,11 +401,6 @@ %-------------------------------------------------------------------------% -%-yaxis range-% -yaxis.range = axis_data.YLim; - -%-------------------------------------------------------------------------% - %-yaxis show tick labels-% yaxis.showticklabels = true; @@ -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) + 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-% diff --git a/plotly/plotlyfig_aux/core/updateData.m b/plotly/plotlyfig_aux/core/updateData.m index ce812fb2..d40878db 100644 --- a/plotly/plotlyfig_aux/core/updateData.m +++ b/plotly/plotlyfig_aux/core/updateData.m @@ -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') @@ -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') diff --git a/plotly/plotlyfig_aux/helpers/convertDate.m b/plotly/plotlyfig_aux/helpers/convertDate.m index 15b66980..889230b2 100644 --- a/plotly/plotlyfig_aux/helpers/convertDate.m +++ b/plotly/plotlyfig_aux/helpers/convertDate.m @@ -1,3 +1,4 @@ -function converted = convertDate(date) - converted = (date - datenum(1969,12,31,19,00,00))*1000*60*60*24; -end \ No newline at end of file +function convertedDate = convertDate(date) + date.Format = 'yyyy-MM-dd HH:mm:ss'; + convertedDate = char(date); +end \ No newline at end of file diff --git a/plotly/plotlyfig_aux/helpers/convertDuration.m b/plotly/plotlyfig_aux/helpers/convertDuration.m new file mode 100644 index 00000000..bbffae35 --- /dev/null +++ b/plotly/plotlyfig_aux/helpers/convertDuration.m @@ -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 \ No newline at end of file