reporting: set up legends for graphs

time-shift
Houtan Bastani 2014-02-11 18:26:28 +01:00
parent be1819b282
commit 031dca436b
7 changed files with 32 additions and 63 deletions

View File

@ -10741,15 +10741,14 @@ Whether or not to display the legend. Default: @code{false}
Whether or not to display a box around the legend. Default:
@code{false}
@item legendLocation, `North' | `South' | `East' | `West' | `NorthEast' | `SouthEast' | `NorthWest' | `SouthWest' | `NorthOutside' | `SouthOutside' | `EastOutside' | `WestOutside' | `NorthEastOutside' | `SouthEastOutside' | `NorthWestOutside' | `SouthWestOutside' | `Best' | `BestOutside'
Where to place the legend in the graph. NB: some of these are not
available under Octave. Default: @code{`SouthEast'}
@item legendLocation, @code{`south west'} | @code{`south east'} | @code{`north west'} | @code{`north east'} | @code{`outer north east'}
Where to place the legend in the graph. Default: @code{`south east'}
@item legendOrientation, `vertical' | `horizontal'
Orientation of the legend. Default: @code{`horizontal'}
@item legendFontSize, @code{DOUBLE}
The font size for legend entries. Default: @code{8}
@item legendFontSize, @code{`tiny'} | @code{`scriptsize'} | @code{`footnotesize'} | @code{`small'} | @code{`normalsize'} | @code{`large'} | @code{`Large'} | @code{`LARGE'} | @code{`huge'} | @code{`Huge'}
The font size for legend entries. Default: @code{tiny}
@anchor{seriesToUse}
@item seriesToUse, @code{CELL_ARRAY_STRINGS}

View File

@ -54,9 +54,9 @@ o.showGrid = true;
o.showLegend = false;
o.showLegendBox = false;
o.legendLocation = 'SouthEast';
o.legendLocation = 'south east';
o.legendOrientation = 'horizontal';
o.legendFontSize = 8;
o.legendFontSize = 'tiny';
o.showZeroline = false;
@ -103,7 +103,6 @@ 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.showLegend), '@graph.graph: showLegend must be either true or false');
assert(islogical(o.showLegendBox), '@graph.graph: showLegendBox must be either true or false');
assert(isint(o.legendFontSize), '@graph.graph: legendFontSize must be an integer');
assert(islogical(o.showZeroline), '@graph.graph: showZeroline must be either true or false');
assert(ischar(o.shadeColor), '@graph.graph: shadeColor must be a string');
assert(isfloat(o.shadeOpacity) && length(o.shadeOpacity)==1 && ...
@ -115,15 +114,15 @@ assert(isfloat(o.xTickLabelRotation), '@graph.graph: o.xTickLabelRotation must b
assert(ischar(o.xTickLabelAnchor), '@graph.graph: xTickLabelAnchor must be a string');
valid_legend_locations = ...
{'North', 'South', 'East', 'West', ...
'NorthEast', 'SouthEast', 'NorthWest', 'SouthWest', ...
'NorthOutside', 'SouthOutside', 'EastOutside', 'WestOutside', ...
'NorthEastOutside', 'SouthEastOutside', 'NorthWestOutside', 'SouthWestOutside', ...
'Best', 'BestOutside', ...
};
{'south west','south east','north west','north east','outer north east'};
assert(any(strcmp(o.legendLocation, valid_legend_locations)), ...
['@graph.graph: legendLocation must be one of ' strjoin(valid_legend_locations, ' ')]);
valid_legend_font_sizes = {'tiny', 'scriptsize', 'footnotesize', 'small', ...
'normalsize', 'large', 'Large', 'LARGE', 'huge', 'Huge'};
assert(any(strcmp(o.legendFontSize, valid_legend_font_sizes)), ...
['@graph.graph: legendFontSize must be one of ' strjoin(valid_legend_font_sizes)]);
valid_legend_orientations = {'vertical', 'horizontal'};
assert(any(strcmp(o.legendOrientation, valid_legend_orientations)), ...
['@graph.graph: legendOrientation must be one of ' strjoin(valid_legend_orientations, ' ')]);

View File

@ -105,6 +105,18 @@ fprintf(fid, ['},\n',...
'ymax=%d,\n'...
'axis lines=box,\n'], o.width, o.height, dd.ndat, ymin, ymax);
if o.showLegend
fprintf(fid, 'legend style={');
if ~o.showLegendBox
fprintf(fid, 'draw=none,');
end
fprintf(fid, 'font=\\%s,', o.legendFontSize);
if strcmp(o.legendOrientation, 'horizontal')
fprintf(fid,'legend columns=-1,');
end
fprintf(fid, '},\nlegend pos=%s,\n', o.legendLocation);
end
if o.showGrid
fprintf(fid, 'xmajorgrids=true,\nymajorgrids=true,\n');
end
@ -136,22 +148,13 @@ end
for i=1:ne
o.series{i}.writeSeriesForGraph(fid, dd);
if o.showLegend
fprintf(fid, '\\addlegendentry{%s}\n', o.series{i}.getTexName());
end
end
fprintf(fid, '\\end{axis}\\end{tikzpicture}\n');
fprintf(fid, '\\end{axis}\n\\end{tikzpicture}\n');
if fclose(fid) == -1
error('@graph.writeGraphFile: closing %s\n', o.filename);
end
return
if o.showLegend
lh = legend(line_handles, getTexNames(o.series), ...
'orientation', o.legendOrientation, ...
'location', o.legendLocation);
set(lh, 'FontSize', o.legendFontSize);
set(lh, 'interpreter', 'latex');
if ~o.showLegendBox
legend('boxoff');
end
end
end
end

View File

@ -19,5 +19,5 @@ function s = getTexName(o)
% along with Dynare. If not, see <http://www.gnu.org/licenses/>.
assert(~isempty(o.data) && size(o.data, 2) == 1);
s = o.data.tex;
s = o.data.tex{:};
end

View File

@ -84,7 +84,7 @@ if any(stz)
thedata(stz) = 0;
end
fprintf(fid, '%%series %s\n\\addplot[color=%s,%s,line width=%fpt,forget plot,line join=round',...
fprintf(fid, '%%series %s\n\\addplot[color=%s,%s,line width=%fpt,line join=round',...
o.data.name{:}, o.graphLineColor, o.graphLineStyle, o.graphLineWidth);
if ~isempty(o.graphMarker)
if isempty(o.graphMarkerEdgeColor)

View File

@ -1,26 +0,0 @@
function names = getTexNames(cellser)
%function names = getTexNames(cellser)
% Copyright (C) 2013-2014 Dynare Team
%
% This file is part of Dynare.
%
% Dynare is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% Dynare is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with Dynare. If not, see <http://www.gnu.org/licenses/>.
ne = length(cellser);
names = cell(ne, 1);
for i=1:ne
names(i) = cellser{i}.getTexName();
end
end

View File

@ -151,7 +151,6 @@ rep = rep.addSection('height', '60mm');
rep = rep.addGraph('title', 'World Real Oil Price Index', ...
'xrange', prange, ...
'shade', srange, ...
'showLegend', true, ...
'xTicks', [1,5,10,15,find(srange(1)==prange),length(prange)], ...
'xTickLabels',{startpoint{:},'2008Q1','2009Q2','2010Q3',shaded{:}, endpoint{:}},...
'xTickLabelRotation', 0);
@ -172,7 +171,6 @@ rep = rep.addSeries('data', db_q{'LRPOIL_BAR_WORLD'}, ...
rep = rep.addGraph('title', 'World Real Food Price Index', ...
'xrange', prange, ...
'shade', srange, ...
'showLegend', true, ...
'xTicks', [1,5,10,15,find(srange(1)==prange),length(prange)], ...
'xTickLabels',{startpoint{:},'2008Q1','2009Q2','2010Q3',shaded{:}, endpoint{:}},...
'xTickLabelRotation', 0);
@ -195,7 +193,6 @@ rep = rep.addSection('cols', 2);
rep = rep.addGraph('title', 'World Real Oil Price', ...
'xrange', prange, ...
'shade', srange, ...
'showLegend', true, ...
'xTicks', [1,5,10,15,find(srange(1)==prange),length(prange)], ...
'xTickLabels',{startpoint{:},'2008Q1','2009Q2','2010Q3',shaded{:}, endpoint{:}},...
'xTickLabelRotation', 0);
@ -210,7 +207,6 @@ rep = rep.addSeries('data', dc_q{'LRPOIL_WORLD'}, ...
rep = rep.addGraph('title', 'Equilibrium World Real Oil Price', ...
'xrange', prange, ...
'shade', srange, ...
'showLegend', true, ...
'xTicks', [1,5,10,15,find(srange(1)==prange),length(prange)], ...
'xTickLabels',{startpoint{:},'2008Q1','2009Q2','2010Q3',shaded{:}, endpoint{:}},...
'xTickLabelRotation', 0);
@ -225,11 +221,9 @@ rep = rep.addSeries('data', dc_q{'LRPOIL_BAR_WORLD'}, ...
rep = rep.addGraph('title', 'World Real Food Price', ...
'xrange', prange, ...
'shade', srange, ...
'showLegend', true, ...
'xTicks', [1,5,10,15,find(srange(1)==prange),length(prange)], ...
'xTickLabels',{startpoint{:},'2008Q1','2009Q2','2010Q3',shaded{:}, endpoint{:}},...
'xTickLabelRotation', 0,...
'yrange',[400 500]);
'xTickLabelRotation', 0);
rep = rep.addSeries('data', db_q{'LRPFOOD_WORLD'}, ...
'graphLineColor', 'blue', ...
'graphLineWidth', 1.5);