Commit d1c012d2 authored by Houtan Bastani's avatar Houtan Bastani
Browse files

reporting: annualAverages option for addTable

parent 79d62d85
......@@ -8427,11 +8427,16 @@ Display a solid black line at @math{y = 0}. Default: @code{false}
@end table
@end defmethod
@defmethod Report addTable data, showHlines, precision, range, seriesToUse, title, titleSize, vlineAfter, vlineAfterEndOfPeriod, showVlines
@defmethod Report addTable annualAverages, data, showHlines, precision, range, seriesToUse, title, titleSize, vlineAfter, vlineAfterEndOfPeriod, showVlines
Adds a @code{Table} to a @code{Section}.
@optionshead
@table @code
@item annualAverages, @code{bool}
Compute the average over every year in the table and display it in a
column to the right of the data (one column for every year). Only
works for quarterly data. Default: @code{false}
@item data, @code{dynSeries}
@xref{data}.
......
function o = write(o, fid, dates, precision)
%function o = write(o, fid, dates, precision)
function o = write(o, fid, dates, precision, yrsForAvgs)
%function o = write(o, fid, dates, precision, yrsForAvgs)
% Write Table Row
%
% INPUTS
......@@ -7,6 +7,8 @@ function o = write(o, fid, dates, precision)
% fid [int] file id
% dates [dynDates] dates for series slice
% precision [float] precision with which to print the data
% yrsForAvgs [bool] the years for which to compute averages
%
%
% OUTPUTS
% o [series] series object
......@@ -92,5 +94,26 @@ for i=1:size(data,1)
fprintf(fid, ']');
end
end
% Calculate and display annual averages
for i=1:length(yrsForAvgs)
slice = o.data(dynDate([num2str(yrsForAvgs(i)) 'q1']):dynDate([num2str(yrsForAvgs(i)) 'q4']));
avg = sum(slice.data)/size(slice);
fprintf(fid, '&');
if o.tableShowMarkers
if avg < -o.tableMarkerLimit
fprintf(fid, '\\color{%s}', o.tableNegColor);
elseif avg > o.tableMarkerLimit
fprintf(fid, '\\color{%s}', o.tablePosColor);
end
fprintf(fid, '[');
end
fprintf(fid, dataString, round(avg*precision)/precision);
if o.tableShowMarkers
fprintf(fid, ']');
end
end
fprintf(fid, '\\\\%%\n');
end
......@@ -41,6 +41,8 @@ o.showVlines = false;
o.vlineAfter = '';
o.vlineAfterEndOfPeriod = false;
o.annualAverages = false;
o.data = '';
o.seriesToUse = '';
o.range = {};
......@@ -73,6 +75,7 @@ end
% Check options provided by user
assert(ischar(o.title), '@table.table: title must be a string');
assert(islogical(o.annualAverages), '@table.table: annualAverages must be true or false');
assert(islogical(o.showHlines), '@table.table: showHlines must be true or false');
assert(islogical(o.showVlines), '@table.table: showVlines must be true or false');
assert(isint(o.precision), '@table.table: precision must be an int');
......
......@@ -72,6 +72,19 @@ for i=1:ndates
end
end
end
datedata = dates.time;
years = unique(datedata(:, 1));
if o.annualAverages
yrsForAvgs = years;
else
yrsForAvgs = [];
end
for i=1:length(yrsForAvgs)
fprintf(fid, 'r');
if o.showVlines
fprintf(fid, '|');
end
end
fprintf(fid, '@{}}%%\n');
if ~isempty(o.title)
fprintf(fid, '\\multicolumn{%d}{c}{\\%s %s}\\\\\n', ...
......@@ -80,8 +93,6 @@ end
fprintf(fid, '\\toprule%%\n');
% Column Headers
datedata = dates.time;
years = unique(datedata(:, 1));
thdr = num2cell(years, size(years, 1));
switch dates.freq
case 1
......@@ -101,7 +112,6 @@ switch dates.freq
end
end
end
for i=1:size(thdr, 1)
fprintf(fid, ' & \\multicolumn{%d}{c}{%d}', size(thdr{i,2}, 2), thdr{i,1});
end
......@@ -121,6 +131,9 @@ switch dates.freq
otherwise
error('@table.write: invalid dynSeries frequency');
end
for i=1:length(yrsForAvgs)
fprintf(fid, ' & %d', years(i));
end
fprintf(fid, '\\\\[-2pt]%%\n');
fprintf(fid, '\\hline%%\n');
fprintf(fid, '%%\n');
......@@ -128,7 +141,7 @@ fprintf(fid, '%%\n');
% Write Table Data
ne = o.seriesElements.numSeriesElements();
for i=1:ne
o.seriesElements(i).write(fid, dates, o.precision);
o.seriesElements(i).write(fid, dates, o.precision, yrsForAvgs);
if o.showHlines
fprintf(fid, '\\hline\n');
end
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment