From d470ef16ca5735c68037785166b72c32a99b4f6b Mon Sep 17 00:00:00 2001
From: Houtan Bastani <houtan@dynare.org>
Date: Tue, 20 May 2014 11:33:57 +0200
Subject: [PATCH] reporting: support vertical lines

---
 doc/dynare.texi                               |  5 ++-
 matlab/reports/@graph/writeGraphFile.m        |  5 ++-
 matlab/reports/@report_series/getTexName.m    | 12 ++++--
 matlab/reports/@report_series/report_series.m |  2 +
 .../@report_series/writeSeriesForGraph.m      | 38 +++++++++++++++++--
 tests/reporting/runDynareReport.m             |  3 ++
 6 files changed, 56 insertions(+), 9 deletions(-)

diff --git a/doc/dynare.texi b/doc/dynare.texi
index 60e3fca4be..e299c00d29 100644
--- a/doc/dynare.texi
+++ b/doc/dynare.texi
@@ -11356,7 +11356,7 @@ Whether or not to show vertical lines separating the columns. Default: @code{fal
 @end defmethod
 
 @anchor{addSeries}
-@defmethod Report addSeries data, graphLegendName, graphLineColor, graphLineStyle, graphLineWidth, graphMarker, graphMarkerEdgeColor, graphMarkerFaceColor, graphMarkerSize, graphMiscTikzAddPlotOptions, tableDataRhs, tableRowColor, tableRowIndent, tableShowMarkers, tableAlignRight, tableNegColor, tablePosColor, tableSubSectionHeader, zeroTol
+@defmethod Report addSeries data, graphLegendName, graphLineColor, graphLineStyle, graphLineWidth, graphMarker, graphMarkerEdgeColor, graphMarkerFaceColor, graphMarkerSize, graphMiscTikzAddPlotOptions, graphVline, tableDataRhs, tableRowColor, tableRowIndent, tableShowMarkers, tableAlignRight, tableNegColor, tablePosColor, tableSubSectionHeader, zeroTol
 Adds a @code{Series} to a @code{Graph} or a @code{Table}.
 @optionshead
 @table @code
@@ -11400,6 +11400,9 @@ above, you can pass a string such as the following to this option:
 used for desired @code{PGFPLOTS/Ti}@i{k}@code{Z} options that have not been
 incorporated into Dynare Reproting. Default: @code{empty}
 
+@item graphVline, @code{dates}
+Use this option to draw a vertical line at a given date. Default: @code{empty}
+
 @item tableDataRhs, @code{dseries}
 A series to be added to the right of the current series. Usefull for
 displaying aggregate data for a series. @i{e.g} if the series is
diff --git a/matlab/reports/@graph/writeGraphFile.m b/matlab/reports/@graph/writeGraphFile.m
index f02283b5a7..8b649b5897 100644
--- a/matlab/reports/@graph/writeGraphFile.m
+++ b/matlab/reports/@graph/writeGraphFile.m
@@ -182,7 +182,10 @@ end
 for i=1:ne
     o.series{i}.writeSeriesForGraph(fid, dd);
     if o.showLegend
-        fprintf(fid, '\\addlegendentry{%s}\n', o.series{i}.getTexName());
+        le = o.series{i}.getTexName();
+        if ~isempty(le)
+            fprintf(fid, '\\addlegendentry{%s}\n', le);
+        end
     end
 end
 
diff --git a/matlab/reports/@report_series/getTexName.m b/matlab/reports/@report_series/getTexName.m
index 90e6ba2335..0672508f9b 100644
--- a/matlab/reports/@report_series/getTexName.m
+++ b/matlab/reports/@report_series/getTexName.m
@@ -1,7 +1,7 @@
 function s = getTexName(o)
 %function s = getTexName(o)
 
-% Copyright (C) 2013 Dynare Team
+% Copyright (C) 2013-2014 Dynare Team
 %
 % This file is part of Dynare.
 %
@@ -18,6 +18,12 @@ function s = getTexName(o)
 % You should have received a copy of the GNU General Public License
 % along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
 
-assert(~isempty(o.data) && size(o.data, 2) == 1);
-s = o.data.tex{:};
+if isempty(o.data)
+    % for the case when there is no data in the series
+    % e.g. graphVline was passed
+    s = '';
+else
+    assert(size(o.data,2) == 1);
+    s = o.data.tex{:};
+end
 end
\ No newline at end of file
diff --git a/matlab/reports/@report_series/report_series.m b/matlab/reports/@report_series/report_series.m
index 611277686e..5f06d85309 100644
--- a/matlab/reports/@report_series/report_series.m
+++ b/matlab/reports/@report_series/report_series.m
@@ -48,6 +48,8 @@ o.graphMarkerSize = 1;
 
 o.graphMiscTikzAddPlotOptions = '';
 
+o.graphVline = dates();
+
 o.tableShowMarkers = false;
 o.tableNegColor = 'red';
 o.tablePosColor = 'blue';
diff --git a/matlab/reports/@report_series/writeSeriesForGraph.m b/matlab/reports/@report_series/writeSeriesForGraph.m
index f45f4a9289..c834d7d40b 100644
--- a/matlab/reports/@report_series/writeSeriesForGraph.m
+++ b/matlab/reports/@report_series/writeSeriesForGraph.m
@@ -30,8 +30,10 @@ function o = writeSeriesForGraph(o, fid, xrange)
 % along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
 
 %% Validate options provided by user
-assert(~isempty(o.data) && isa(o.data, 'dseries'), ['@report_series.writeSeriesForGraph: must ' ...
-                    'provide data as a dseries']);
+if isempty(o.graphVline)
+    assert(~isempty(o.data) && isa(o.data, 'dseries'), ['@report_series.writeSeriesForGraph: must ' ...
+                        'provide data as a dseries']);
+end
 
 assert(ischar(o.graphMiscTikzAddPlotOptions), ['@report_series.writeSeriesForGraph: ' ...
                     'graphMiscTikzAddPlotOptions file must be a string']);
@@ -64,12 +66,40 @@ assert(isfloat(o.graphMarkerSize) && o.graphMarkerSize > 0, ...
 assert(~(strcmp(o.graphLineStyle, 'none') && isempty(o.graphMarker)), ['@report_series.writeSeriesForGraph: ' ...
                     'you must provide at least one of graphLineStyle and graphMarker']);
 
-% Validate xrange
-
+% Validate graphVline
+assert(isdates(o.graphVline), '@report_series.writeSeriesForGraph: graphVline must be a dates');
 
 % Zero tolerance
 assert(isfloat(o.zeroTol), '@report_series.write: zeroTol must be a float');
 
+%% graphVline
+
+if ~isempty(o.graphVline)
+    for i=1:o.graphVline.ndat
+        fprintf(fid, '%%vline %d\n\\begin{pgfonlayer}{background1}\n\\draw[color=%s,%s,line width=%fpt,line join=round',...
+                i, o.graphLineColor, o.graphLineStyle, o.graphLineWidth);
+        if ~isempty(o.graphMarker)
+            if isempty(o.graphMarkerEdgeColor)
+                o.graphMarkerEdgeColor = o.graphLineColor;
+            end
+        if isempty(o.graphMarkerFaceColor)
+            o.graphMarkerFaceColor = o.graphLineColor;
+        end
+        fprintf(fid, ',mark=%s,mark size=%f,every mark/.append style={draw=%s,fill=%s}',...
+                o.graphMarker,o.graphMarkerSize,o.graphMarkerEdgeColor,o.graphMarkerFaceColor);
+        end
+        if ~isempty(o.graphMiscTikzAddPlotOptions)
+            fprintf(fid, ',%s', o.graphMiscTikzAddPlotOptions);
+        end
+        stringsdd = strings(xrange);
+        x = find(strcmpi(date2string(o.graphVline(i)), stringsdd));
+        fprintf(fid, ['] (axis cs:%d,\\pgfkeysvalueof{/pgfplots/ymin}) -- (axis ' ...
+                      'cs:%d,\\pgfkeysvalueof{/pgfplots/ymax});\n\\end{pgfonlayer}\n'], ...
+                x, x);
+    end
+    return
+end
+
 %%
 if isempty(xrange) || all(xrange == o.data.dates)
     ds = o.data;
diff --git a/tests/reporting/runDynareReport.m b/tests/reporting/runDynareReport.m
index f6e51afa3f..812f948a00 100644
--- a/tests/reporting/runDynareReport.m
+++ b/tests/reporting/runDynareReport.m
@@ -184,6 +184,9 @@ rep = rep.addSeries('data', db_q{'LRPFOOD_BAR_WORLD'}, ...
                     'graphLineColor', 'green', ...
                     'graphLineStyle', 'solid', ...
                     'graphLineWidth', 1.5);
+rep = rep.addSeries('graphVline', dates('2009q2'):dates('2010q1'), ...
+                    'graphLineColor', 'red', ...
+                    'graphLineWidth', 1.5);
 
 % Pae 2
 rep = rep.addPage('title', {'Jan1 vs Jan2', 'World Oil and Food Prices'}, ...
-- 
GitLab