dynare/matlab/reporting/@report_series/writeSeriesForGraph.m

168 lines
7.2 KiB
Matlab
Raw Normal View History

function writeSeriesForGraph(o, fid, xrange, series_num)
%function writeSeriesForGraph(o, fid, xrange, series_num)
2014-01-22 00:24:24 +01:00
% Print a TikZ line
2013-03-27 16:20:59 +01:00
%
% INPUTS
% o [report_series] series object
% xrange [dates] range of x values for line
% series_num [int] the number of this series in the total number of series passed to this graph
2013-03-27 16:20:59 +01:00
%
% OUTPUTS
2014-01-22 00:24:24 +01:00
% NONE
2013-03-27 16:20:59 +01:00
%
% SPECIAL REQUIREMENTS
% none
% Copyright © 2014-2019 Dynare Team
2013-03-27 16:20:59 +01:00
%
% 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 <https://www.gnu.org/licenses/>.
2013-03-27 16:20:59 +01:00
%% Validate options provided by user
2014-05-20 14:18:18 +02:00
if isempty(o.graphVline) && isempty(o.graphHline)
assert(~isempty(o.data) && isdseries(o.data), ['@report_series.writeSeriesForGraph: must ' ...
2014-05-20 11:33:57 +02:00
'provide data as a dseries']);
end
2013-03-27 16:20:59 +01:00
assert(ischar(o.graphMiscTikzAddPlotOptions), ['@report_series.writeSeriesForGraph: ' ...
'graphMiscTikzAddPlotOptions file must be a string']);
2014-12-23 15:32:54 +01:00
assert(islogical(o.graphShowInLegend), ['@report_series.writeSeriesForGraph: ' ...
'graphShowInLegend must be either true or false']);
2013-03-27 16:20:59 +01:00
% Line
assert(ischar(o.graphLineColor), '@report_series.writeSeriesForGraph: graphLineColor must be a string');
assert(ischar(o.graphLineStyle), '@report_series.writeSeriesForGraph: graphLineStyle must be a string');
2014-01-22 00:24:24 +01:00
assert(isfloat(o.graphLineWidth) && o.graphLineWidth > 0, ...
2017-05-19 00:04:08 +02:00
'@report_series.writeSeriesForGraph: graphLineWidth must be a positive number');
2013-03-27 16:20:59 +01:00
% Bar
assert(islogical(o.graphBar), '@report_series.writeSeriesForGraph: graphBar must be either true or false');
assert(ischar(o.graphBarColor), '@report_series.writeSeriesForGraph: graphBarColor must be a string');
assert(ischar(o.graphBarFillColor), '@report_series.writeSeriesForGraph: graphBarFillColor must be a string');
assert(isfloat(o.graphBarWidth) && o.graphBarWidth > 0, ...
2017-05-19 00:04:08 +02:00
'@report_series.writeSeriesForGraph: graphbarWidth must be a positive number');
% GraphMarker
valid_graphMarker = {'x', '+', '-', '|', 'o', 'asterisk', 'star', '10-pointed star', 'oplus', ...
'oplus*', 'otimes', 'otimes*', 'square', 'square*', 'triangle', 'triangle*', 'diamond', ...
'diamond*', 'halfdiamond*', 'halfsquare*', 'halfsquare right*', ...
'halfsquare left*','Mercedes star','Mercedes star flipped','halfcircle',...
'halfcircle*','pentagon','pentagon star'};
assert(isempty(o.graphMarker) || any(strcmp(o.graphMarker, valid_graphMarker)), ...
2016-02-24 14:38:20 +01:00
['@report_series.writeSeriesForGraph: graphMarker must be one of ' addCommasToCellStr(valid_graphMarker)]);
2013-03-27 16:20:59 +01:00
assert(ischar(o.graphMarkerEdgeColor), '@report_series.writeSeriesForGraph: graphMarkerEdgeColor must be a string');
assert(ischar(o.graphMarkerFaceColor), '@report_series.writeSeriesForGraph: graphMarkerFaceColor must be a string');
assert(isfloat(o.graphMarkerSize) && o.graphMarkerSize > 0, ...
2017-05-19 00:04:08 +02:00
'@report_series.writeSeriesForGraph: graphMarkerSize must be a positive number');
2013-03-27 16:20:59 +01:00
% Marker & Line
assert(~(strcmp(o.graphLineStyle, 'none') && isempty(o.graphMarker)), ['@report_series.writeSeriesForGraph: ' ...
'you must provide at least one of graphLineStyle and graphMarker']);
2013-03-27 16:20:59 +01:00
2014-05-20 11:33:57 +02:00
% Validate graphVline
assert(isempty(o.graphVline) || (isdates(o.graphVline) && o.graphVline.ndat == 1), ...
2017-05-19 00:04:08 +02:00
'@report_series.writeSeriesForGraph: graphVline must be a dates of size one');
2014-05-20 14:18:18 +02:00
assert(isempty(o.graphHline) || isnumeric(o.graphHline), ...
2017-05-19 00:04:08 +02:00
'@report_series.writeSeriesForGraph: graphHline must a single numeric value');
2013-03-27 16:20:59 +01:00
2013-07-24 16:40:01 +02:00
% Zero tolerance
assert(isfloat(o.zeroTol), '@report_series.write: zeroTol must be a float');
2013-07-24 16:40:01 +02:00
2015-04-27 18:16:05 +02:00
% Fan Chart
assert(ischar(o.graphFanShadeColor), '@report_series.writeSeriesForGraph: graphFanShadeColor must be a string');
assert(isint(o.graphFanShadeOpacity), '@report_series.writeSeriesForGraph: graphFanShadeOpacity must be an int');
%% graphVline && graphHline
if ~isempty(o.graphVline)
2015-04-24 16:13:16 +02:00
fprintf(fid, '%%Vertical Line\n\\begin{pgfonlayer}{axis lines}\n\\draw');
2015-04-27 18:16:05 +02:00
writeLineOptions(o, fid, series_num);
stringsdd = strings(xrange);
x = find(strcmpi(date2string(o.graphVline), stringsdd));
fprintf(fid, ['(axis cs:%d,\\pgfkeysvalueof{/pgfplots/ymin}) -- (axis ' ...
2017-05-19 00:04:08 +02:00
'cs:%d,\\pgfkeysvalueof{/pgfplots/ymax});\n\\end{pgfonlayer}\n'], ...
x, x);
end
if ~isempty(o.graphHline)
2015-04-24 16:13:16 +02:00
fprintf(fid, '%%Horizontal Line\n\\begin{pgfonlayer}{axis lines}\n\\addplot');
2015-04-27 18:16:05 +02:00
writeLineOptions(o, fid, series_num);
2014-05-22 17:03:52 +02:00
fprintf(fid, ['coordinates {(\\pgfkeysvalueof{/pgfplots/xmin},%f)' ...
'(\\pgfkeysvalueof{/pgfplots/xmax},%f)};\n\\end{pgfonlayer}\n'], ...
o.graphHline, o.graphHline);
end
2014-05-20 14:18:18 +02:00
if ~isempty(o.graphVline) || ~isempty(o.graphHline)
% return since the code below assumes that o.data exists
2014-05-20 11:33:57 +02:00
return
end
2013-03-27 16:20:59 +01:00
%%
if isempty(xrange) || all(xrange == o.data.dates)
ds = o.data;
else
2013-03-27 16:20:59 +01:00
ds = o.data(xrange);
end
thedata = setDataToZeroFromZeroTol(o, ds);
fprintf(fid, '%%series %s\n\\addplot', o.data.name{:});
2015-04-27 18:16:05 +02:00
writeLineOptions(o, fid, series_num);
fprintf(fid,'\ntable[row sep=crcr]{\nx y\\\\\n');
for i=1:ds.dates.ndat
2015-10-15 12:32:24 +02:00
if ~isnan(thedata(i))
fprintf(fid, '%d %f\\\\\n', i, thedata(i));
2015-10-15 11:16:13 +02:00
end
end
fprintf(fid,'};\n');
2015-04-27 18:16:05 +02:00
% For Fan charts
if ispc || ismac
2017-05-19 00:04:08 +02:00
if ~isempty(o.graphFanShadeColor)
assert(isint(series_num) && series_num > 1, ['@report_series.writeSeriesForGraph: can only add '...
'graphFanShadeColor and graphFanShadeOpacity starting from the ' ...
'second series in the graph']);
fprintf(fid, '\\addplot[%s!%d, forget plot] fill between[of=%d and %d];\n', ...
o.graphFanShadeColor, o.graphFanShadeOpacity, series_num, series_num - 1);
end
2015-04-27 18:16:05 +02:00
end
end
2015-04-27 18:16:05 +02:00
function writeLineOptions(o, fid, series_num)
if o.graphBar
fprintf(fid, '[ybar,ybar legend,color=%s,fill=%s,line width=%fpt',...
o.graphBarColor, o.graphBarFillColor, o.graphBarWidth);
else
fprintf(fid, '[color=%s,%s,line width=%fpt,line join=round',...
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
end
if ~isempty(o.graphMiscTikzAddPlotOptions)
2014-05-19 16:31:37 +02:00
fprintf(fid, ',%s', o.graphMiscTikzAddPlotOptions);
end
if isunix && ~ismac
2017-05-19 00:04:08 +02:00
fprintf(fid,']');
else
2017-05-19 00:04:08 +02:00
fprintf(fid,',name path=%d]', series_num);
end
2013-03-27 16:20:59 +01:00
end