From 217ab0088dc5e20a8e1514082d3d07f9db33f46f Mon Sep 17 00:00:00 2001
From: Houtan Bastani <houtan@dynare.org>
Date: Wed, 30 Jul 2014 15:19:39 +0200
Subject: [PATCH] reporting: tables: add row highlighting option

---
 doc/dynare.texi                                     |  9 ++++++++-
 matlab/reports/@report_series/writeSeriesForTable.m | 11 ++++++++---
 matlab/reports/@report_table/report_table.m         |  3 +++
 matlab/reports/@report_table/writeTableFile.m       |  2 +-
 tests/reporting/CountryTablePage.m                  |  3 ++-
 tests/reporting/runDynareReport.m                   |  3 ++-
 6 files changed, 24 insertions(+), 7 deletions(-)

diff --git a/doc/dynare.texi b/doc/dynare.texi
index c66fa4b5a7..407030117a 100644
--- a/doc/dynare.texi
+++ b/doc/dynare.texi
@@ -11503,7 +11503,7 @@ reports. Default: @code{`black'}
 @end table
 @end defmethod
 
-@defmethod Report addTable data, showHlines, precision, range, seriesToUse, tableDirName, tableName, title, titleFormat, vlineAfter, vlineAfterEndOfPeriod, showVlines, writeCSV
+@defmethod Report addTable data, highlightRows, showHlines, precision, range, seriesToUse, tableDirName, tableName, title, titleFormat, vlineAfter, vlineAfterEndOfPeriod, showVlines, writeCSV
 Adds a @code{Table} to a @code{Section}.
 @optionshead
 @table @code
@@ -11511,6 +11511,12 @@ Adds a @code{Table} to a @code{Section}.
 @item data, @code{dseries}
 @xref{data}.
 
+@item highlightRows, @code{CELL_ARRAY_STRINGS}
+A cell array containing the colors to use for row highlighting. See
+@ref{shadeColor} for how to use colors with reports. Highlighting for a
+specific row can be overridden by using the @ref{tableRowColor} option to
+@ref{addSeries}. Default: @code{empty}
+
 @item showHlines, @code{BOOLEAN}
 Whether or not to show horizontal lines separating the rows. Default: @code{false}
 
@@ -11627,6 +11633,7 @@ quarterly @code{tableDataRhs} could point to the yearly averages of
 the quarterly series. This would cause quarterly data to be displayed
 followed by annual data. Default: @code{empty}
 
+@anchor{tableRowColor}
 @item tableRowColor, @code{STRING}
 The color that you want the row to be. Predefined values include
 @code{LightCyan} and @code{Gray}. Default: @code{white}.
diff --git a/matlab/reports/@report_series/writeSeriesForTable.m b/matlab/reports/@report_series/writeSeriesForTable.m
index bd4f70b672..77bc82a3b1 100644
--- a/matlab/reports/@report_series/writeSeriesForTable.m
+++ b/matlab/reports/@report_series/writeSeriesForTable.m
@@ -1,5 +1,5 @@
-function o = writeSeriesForTable(o, fid, dates, precision, ncols)
-%function o = writeSeriesForTable(o, fid, dates, precision, ncols)
+function o = writeSeriesForTable(o, fid, dates, precision, ncols, rowcolor)
+%function o = writeSeriesForTable(o, fid, dates, precision, ncols, rowcolor)
 % Write Table Row
 %
 % INPUTS
@@ -8,6 +8,7 @@ function o = writeSeriesForTable(o, fid, dates, precision, ncols)
 %   dates        [dates]            dates for report_series slice
 %   precision    [float]            precision with which to print the data
 %   ncols        [int]              total number of columns in table
+%   rowcolor     [string]           string to color this row
 %
 %
 % OUTPUTS
@@ -65,7 +66,11 @@ assert(isfloat(o.tableMarkerLimit), '@report_series.writeSeriesForTable: tableMa
 
 %% Write Output
 fprintf(fid, '%% Table Row (report_series)\n');
-if ~isempty(o.tableRowColor)
+if ~isempty(o.tableRowColor) && ~strcmpi(o.tableRowColor, 'white')
+    fprintf(fid, '\\rowcolor{%s}', o.tableRowColor);
+elseif ~isempty(rowcolor)
+    fprintf(fid, '\\rowcolor{%s}', rowcolor);
+else
     fprintf(fid, '\\rowcolor{%s}', o.tableRowColor);
 end
 if ~isempty(o.tableSubSectionHeader)
diff --git a/matlab/reports/@report_table/report_table.m b/matlab/reports/@report_table/report_table.m
index ddc6c225ca..effd07e517 100644
--- a/matlab/reports/@report_table/report_table.m
+++ b/matlab/reports/@report_table/report_table.m
@@ -51,6 +51,8 @@ o.range = {};
 o.precision = 1;
 o.writeCSV = false;
 
+o.highlightRows = {''};
+
 if nargin == 1
     assert(isa(varargin{1}, 'report_table'),['With one arg to Report_Table constructor, ' ...
                         'you must pass a report_table object']);
@@ -117,6 +119,7 @@ assert(iscellstr(o.titleFormat), ...
 assert(ischar(o.tableName), '@report_table.report_table: tableName must be a string');
 assert(ischar(o.tableDirName), '@report_table.report_table: tableDirName must be a string');
 assert(islogical(o.writeCSV), '@report_table.report_table: writeCSV must be either true or false');
+assert(iscellstr(o.highlightRows), '@report_table.report_table: highlightRowsmust be a cell string');
 
 % using o.seriesToUse, create series objects and put them in o.series
 if ~isempty(o.data)
diff --git a/matlab/reports/@report_table/writeTableFile.m b/matlab/reports/@report_table/writeTableFile.m
index 7c0b7d1d90..01e04adaa5 100644
--- a/matlab/reports/@report_table/writeTableFile.m
+++ b/matlab/reports/@report_table/writeTableFile.m
@@ -175,7 +175,7 @@ if o.writeCSV
     csvseries = dseries();
 end
 for i=1:ne
-    o.series{i}.writeSeriesForTable(fid, o.range, o.precision, ncols);
+    o.series{i}.writeSeriesForTable(fid, o.range, o.precision, ncols, o.highlightRows{mod(i,length(o.highlightRows))+1});
     if o.writeCSV
         if isempty(o.series{i}.tableSubSectionHeader)
             csvseries = [csvseries ...
diff --git a/tests/reporting/CountryTablePage.m b/tests/reporting/CountryTablePage.m
index 4f91d9fe34..e73b865b28 100644
--- a/tests/reporting/CountryTablePage.m
+++ b/tests/reporting/CountryTablePage.m
@@ -44,7 +44,8 @@ notForOtherThree = {'BLT_', 'UNR_', 'UNR_BAR_', 'UNR_GAP_'};
 rep = rep.addTable('title', countryName, ...
                    'range', {trange, dates('2012a'):dates('2014a')}, ...
                    'vlineAfter', {vline_after dates('2014q4')}, ...
-                   'writeCSV', true);
+                   'writeCSV', true, ...
+                   'highlightRows', {'gray!22', 'cyan!33', 'blue!44', 'red!55'});
 
 
 
diff --git a/tests/reporting/runDynareReport.m b/tests/reporting/runDynareReport.m
index 305e2736d6..4ce50f4083 100644
--- a/tests/reporting/runDynareReport.m
+++ b/tests/reporting/runDynareReport.m
@@ -44,7 +44,8 @@ rep = rep.addVspace();
 % Table 1
 rep = rep.addTable('title', {'Real GDP Growth','subtitle 1', 'subtitle 2'}, ...
                    'range', larange, ...
-                   'vlineAfter', dates('2011y'));
+                   'vlineAfter', dates('2011y'), ...
+                   'highlightRows', {'gray!25','white','green!22'});
 rep = AnnualTable(rep, db_a, dc_a, 'PCH_GROWTH4_', larange);
 rep = rep.addVspace('number', 2);
 
-- 
GitLab