From 0247faa9edc68f43baf206ddb8ce619994a001e5 Mon Sep 17 00:00:00 2001
From: Houtan Bastani <houtan@dynare.org>
Date: Tue, 20 May 2014 14:18:18 +0200
Subject: [PATCH] reporting: support horizontal lines

---
 doc/dynare.texi                               |  6 ++++-
 matlab/reports/@report_series/report_series.m |  1 +
 .../@report_series/writeSeriesForGraph.m      | 27 +++++++++++++++++--
 tests/reporting/runDynareReport.m             |  4 +++
 4 files changed, 35 insertions(+), 3 deletions(-)

diff --git a/doc/dynare.texi b/doc/dynare.texi
index e299c00d29..bdb467fd4f 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, graphVline, tableDataRhs, tableRowColor, tableRowIndent, tableShowMarkers, tableAlignRight, tableNegColor, tablePosColor, tableSubSectionHeader, zeroTol
+@defmethod Report addSeries data, graphHline, 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
@@ -11364,6 +11364,10 @@ Adds a @code{Series} to a @code{Graph} or a @code{Table}.
 @item data, @code{dseries}
 @xref{data}.
 
+@item graphHline, @code{DOUBLE}
+Use this option to draw a horizontal line at the given value. Default:
+@code{empty}
+
 @anchor{graphLegendName}
 @item graphLegendName, @code{STRING}
 The name to display in the legend for this series. Will be displayed only if
diff --git a/matlab/reports/@report_series/report_series.m b/matlab/reports/@report_series/report_series.m
index 5f06d85309..7c708df186 100644
--- a/matlab/reports/@report_series/report_series.m
+++ b/matlab/reports/@report_series/report_series.m
@@ -48,6 +48,7 @@ o.graphMarkerSize = 1;
 
 o.graphMiscTikzAddPlotOptions = '';
 
+o.graphHline = {};
 o.graphVline = dates();
 
 o.tableShowMarkers = false;
diff --git a/matlab/reports/@report_series/writeSeriesForGraph.m b/matlab/reports/@report_series/writeSeriesForGraph.m
index c834d7d40b..ab50a16734 100644
--- a/matlab/reports/@report_series/writeSeriesForGraph.m
+++ b/matlab/reports/@report_series/writeSeriesForGraph.m
@@ -30,7 +30,7 @@ function o = writeSeriesForGraph(o, fid, xrange)
 % along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
 
 %% Validate options provided by user
-if isempty(o.graphVline)
+if isempty(o.graphVline) && isempty(o.graphHline)
     assert(~isempty(o.data) && isa(o.data, 'dseries'), ['@report_series.writeSeriesForGraph: must ' ...
                         'provide data as a dseries']);
 end
@@ -68,13 +68,15 @@ assert(~(strcmp(o.graphLineStyle, 'none') && isempty(o.graphMarker)), ['@report_
 
 % Validate graphVline
 assert(isdates(o.graphVline), '@report_series.writeSeriesForGraph: graphVline must be a dates');
+assert(isempty(o.graphHline) || isnumeric(o.graphHline), ...
+       '@report_series.writeSeriesForGraph: graphHline must a single numeric value');
 
 % Zero tolerance
 assert(isfloat(o.zeroTol), '@report_series.write: zeroTol must be a float');
 
 %% graphVline
 
-if ~isempty(o.graphVline)
+if ~isempty(o.graphVline) || ~isempty(o.graphHline)
     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);
@@ -97,6 +99,27 @@ if ~isempty(o.graphVline)
                       'cs:%d,\\pgfkeysvalueof{/pgfplots/ymax});\n\\end{pgfonlayer}\n'], ...
                 x, x);
     end
+
+    if ~isempty(o.graphHline)
+        fprintf(fid, '%%hline %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
+        fprintf(fid, ['] (axis cs:\\pgfkeysvalueof{/pgfplots/xmin},%f) -- (axis ' ...
+                      'cs:\\pgfkeysvalueof{/pgfplots/xmax},%f);\n\\end{pgfonlayer}\n'], ...
+                o.graphHline, o.graphHline);
+    end
     return
 end
 
diff --git a/tests/reporting/runDynareReport.m b/tests/reporting/runDynareReport.m
index 812f948a00..4f541bf3ca 100644
--- a/tests/reporting/runDynareReport.m
+++ b/tests/reporting/runDynareReport.m
@@ -234,6 +234,10 @@ rep = rep.addSeries('data', dc_q{'LRPFOOD_WORLD'}, ...
                     'graphLineColor', 'blue', ...
                     'graphLineStyle', 'dashed', ...
                     'graphLineWidth', 1.5);
+rep = rep.addSeries('graphHline', 460, ...
+                    'graphLineColor', 'red', ...
+                    'graphLineWidth', 1.5);
+
 
 rep = rep.addGraph('title', 'Equilibrium World Real Food Price', ...
                    'xrange', prange, ...
-- 
GitLab