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

111 lines
4.5 KiB
Matlab
Raw Normal View History

function o = writeSeriesForGraph(o, fid, xrange)
%function o = writeSeriesForGraph(o, fid, xrange)
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
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
2014-01-22 00:24:24 +01:00
% Copyright (C) 2014 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 <http://www.gnu.org/licenses/>.
%% Validate options provided by user
assert(~isempty(o.data) && isa(o.data, 'dseries'), ['@report_series.writeSeriesForGraph: must ' ...
2013-10-18 17:38:43 +02:00
'provide data as a dseries']);
2013-03-27 16:20:59 +01:00
assert(ischar(o.graphMiscTikzAddPlotOptions), ['@report_series.writeSeriesForGraph: ' ...
'graphMiscTikzAddPlotOptions file must be a string']);
2013-03-27 16:20:59 +01:00
% Line
2014-01-22 00:24:24 +01:00
valid_graphLineColor = {'red', 'green', 'blue', 'cyan ', 'magenta', 'yellow', ...
'black', 'gray', 'darkgray', 'lightgray', 'brown', ...
'lime', 'olive', 'orange', 'pink', 'purple', 'teal', 'violet', 'white'};
assert(any(strcmp(o.graphLineColor, valid_graphLineColor)), ...
['@report_series.writeSeriesForGraph: graphLineColor must be one of ' strjoin(valid_graphLineColor)]);
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, ...
'@report_series.writeSeriesForGraph: graphLineWidth must be a positive number');
2013-03-27 16:20:59 +01:00
% 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)), ...
['@report_series.writeSeriesForGraph: graphMarker must be one of ' strjoin(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, ...
'@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
% Validate xrange
2014-01-22 00:24:24 +01:00
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
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
% if graphing data that is within zeroTol, set to zero, create report_series and
2013-07-24 16:40:01 +02:00
% get line:
thedata = ds.data;
stz = bsxfun(@and, ...
bsxfun(@lt, thedata, o.zeroTol), ...
bsxfun(@gt, thedata, -o.zeroTol));
2013-07-24 16:40:01 +02:00
if any(stz)
thedata(stz) = 0;
end
2014-02-11 18:26:28 +01:00
fprintf(fid, '%%series %s\n\\addplot[color=%s,%s,line width=%fpt,line join=round',...
2014-01-22 00:24:24 +01:00
o.data.name{:}, 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,']\ntable[row sep=crcr]{\nx y\\\\\n');
2014-01-22 00:24:24 +01:00
for i=1:ds.dates.ndat
fprintf(fid, '%d %f\\\\\n', i, thedata(i));
end
fprintf(fid,'};\n');
2013-03-27 16:20:59 +01:00
end