From eeedda5e2d0828dabe1391ffce829da01830a592 Mon Sep 17 00:00:00 2001
From: Houtan Bastani <houtan@dynare.org>
Date: Thu, 13 Feb 2014 17:01:34 +0100
Subject: [PATCH] reporting: add options xAxisTight and yAxisTight

---
 doc/dynare.texi                        | 10 +++++++++-
 matlab/reports/@graph/graph.m          |  4 ++++
 matlab/reports/@graph/writeGraphFile.m | 24 ++++++++++++++++--------
 tests/reporting/runDynareReport.m      |  6 ++++--
 4 files changed, 33 insertions(+), 11 deletions(-)

diff --git a/doc/dynare.texi b/doc/dynare.texi
index faa427b10b..30c5093a76 100644
--- a/doc/dynare.texi
+++ b/doc/dynare.texi
@@ -10704,7 +10704,7 @@ command. Default: @code{`!'}
 @end table
 @end defmethod
 
-@defmethod Report addGraph data, figname, figDirName, graphSize, height, showGrid, showLegend, showLegendBox, legendLocation, legendOrientation, legendFontSize, seriesToUse, shade, shadeColor, shadeOpacity, title, width, xlabel, ylabel, xrange, xTicks, xTickLabels, xTickLabelAnchor, xTickLabelRotation, yrange, showZeroline
+@defmethod Report addGraph data, figname, figDirName, graphSize, height, showGrid, showLegend, showLegendBox, legendLocation, legendOrientation, legendFontSize, seriesToUse, shade, shadeColor, shadeOpacity, title, width, xlabel, ylabel, xAxisTight, xrange, xTicks, xTickLabels, xTickLabelAnchor, xTickLabelRotation, yAxisTight, yrange, showZeroline
 Adds a @code{Graph} to a @code{Section}.
 @optionshead
 @table @code
@@ -10779,6 +10779,10 @@ The x-axis label. Default: @code{none}
 @item ylabel, @code{STRING}
 The y-axis label. Default: @code{none}
 
+@item xAxisTight, @code{BOOLEAN}
+Use a tight x axis. If false, uses pgfplots @code{enlarge x limits} to
+choose appropriate axis size. Default: @code{true}
+
 @item xrange, @code{dates}
 The boundary on the x-axis to display in the graph. Default: all
 
@@ -10799,6 +10803,10 @@ Where to anchor the x tick label. Default: @code{`south'}
 @item xTickLabelRotation, @code{DOUBLE}
 The amount to rotate the x tick labels by. Default: @code{45}
 
+@item yAxisTight, @code{BOOLEAN}
+Use a tight y axis. If false, uses pgfplots @code{enlarge y limits} to
+choose appropriate axis size. Default: @code{false}
+
 @item yrange, @code{NUMERICAL_VECTOR}
 The boundary on the y-axis to display in the graph, represented as a
 @code{NUMERICAL_VECTOR} of size @math{2}, with the first entry less
diff --git a/matlab/reports/@graph/graph.m b/matlab/reports/@graph/graph.m
index 1745ac57c3..83b1a5aa9d 100644
--- a/matlab/reports/@graph/graph.m
+++ b/matlab/reports/@graph/graph.m
@@ -44,7 +44,9 @@ o.figname = '';
 o.data = '';
 o.seriesToUse = '';
 o.xrange = '';
+o.xAxisTight = true;
 o.yrange = '';
+o.yAxisTight = false;
 
 o.shade = '';
 o.shadeColor = 'green';
@@ -101,6 +103,8 @@ assert(ischar(o.ylabel), '@graph.graph: ylabel file must be a string');
 assert(ischar(o.figname), '@graph.graph: figname must be a string');
 assert(ischar(o.figDirName), '@graph.graph: figDirName must be a string');
 assert(islogical(o.showGrid), '@graph.graph: showGrid must be either true or false');
+assert(islogical(o.xAxisTight), '@graph.graph: xAxisTight must be either true or false');
+assert(islogical(o.yAxisTight), '@graph.graph: yAxisTight must be either true or false');
 assert(islogical(o.showLegend), '@graph.graph: showLegend must be either true or false');
 assert(islogical(o.showLegendBox), '@graph.graph: showLegendBox must be either true or false');
 assert(islogical(o.showZeroline), '@graph.graph: showZeroline must be either true or false');
diff --git a/matlab/reports/@graph/writeGraphFile.m b/matlab/reports/@graph/writeGraphFile.m
index 7ce376aca5..e08d96fc2b 100644
--- a/matlab/reports/@graph/writeGraphFile.m
+++ b/matlab/reports/@graph/writeGraphFile.m
@@ -85,12 +85,20 @@ fprintf(fid, ['},\n',...
               'width=%fin,\n'...
               'height=%fin,\n'...
               'scale only axis,\n'...
-              'xmin=1,\n'...
-              'xmax=%d,\n'...
-              'axis lines=box,\n'], o.width, o.height, dd.ndat);
+              'axis lines=box,\n'], o.width, o.height);
+
+if o.xAxisTight
+    fprintf(fid, 'enlarge x limits=false,\n');
+else
+    fprintf(fid, 'enlarge x limits=true,\n');
+end
 
 if isempty(o.yrange)
-    fprintf(fid, 'enlarge y limits=true,\n');
+    if o.yAxisTight
+        fprintf(fid, 'enlarge y limits=false,\n');
+    else
+        fprintf(fid, 'enlarge y limits=true,\n');
+    end
 else
     fprintf(fid, 'ymin=%f,\nymax=%f,\n',o.yrange(1),o.yrange(2));
 end
@@ -139,10 +147,10 @@ if ~isempty(o.shade)
                         date2string(o.shade(1)) ' or ' date2string(o.shade(end)) ' is not in the date ' ...
                         'range of data selected.']);
     fprintf(fid,['\\begin{pgfonlayer}{background}\n\\fill[%s!%f]\n(axis ' ...
-                 'cs:%d,\\pgfkeysvalueof{/pgfplots/ymin})\nrectangle (axis ' ...
-                 'cs:%d,\\pgfkeysvalueof{/pgfplots/ymax});\n' ...
-                 '\\end{pgfonlayer}\n'], ...
-            o.shadeColor, o.shadeOpacity, x1, x2);
+                 'cs:%f,\\pgfkeysvalueof{/pgfplots/ymin})\nrectangle (axis ' ...
+                 'cs:\\pgfkeysvalueof{/pgfplots/xmax},\\pgfkeysvalueof{/' ...
+                 'pgfplots/ymax});\n\\end{pgfonlayer}\n'], ...
+            o.shadeColor, o.shadeOpacity,x1);
 end
 
 fprintf(fid, '\\end{axis}\n\\end{tikzpicture}\n');
diff --git a/tests/reporting/runDynareReport.m b/tests/reporting/runDynareReport.m
index b7617fed5e..ef03704fc4 100644
--- a/tests/reporting/runDynareReport.m
+++ b/tests/reporting/runDynareReport.m
@@ -117,7 +117,7 @@ rep = AnnualTable(rep, db_a, dc_a, 'Y_', larange);
 for i=1:length(shortNames)
     rep = rep.addPage('title', {'Jan1 vs Jan2', longNames{i}}, ...
                       'titleFormat', {'\large\bfseries', '\large'});
-    rep = rep.addSection('cols', 2);
+    rep = rep.addSection('cols', 5);
     rep = CountryGraphPage(rep, shortNames{i}, db_q, dc_q, prange, srange);
 
     rep = rep.addPage('title', 'Jan1 vs Jan2', ...
@@ -223,7 +223,9 @@ rep = rep.addGraph('title', 'World Real Food Price', ...
                    'shade', srange, ...
                    'xTicks', [1,5,10,15,find(srange(1)==prange),length(prange)], ...
                    'xTickLabels',{startpoint{:},'2008Q1','2009Q2','2010Q3',shaded{:}, endpoint{:}},...
-                   'xTickLabelRotation', 0);
+                   'xTickLabelRotation', 0,...
+                   'xAxisTight',false,...
+                   'yAxisTight',true);
 rep = rep.addSeries('data', db_q{'LRPFOOD_WORLD'}, ...
                     'graphLineColor', 'blue', ...
                     'graphLineWidth', 1.5);
-- 
GitLab