writeGraphFile.m 8.17 KB
Newer Older
1
2
function o = writeGraphFile(o, pg, sec, row, col)
%function o = writeGraphFile(o, pg, sec, row, col)
Houtan Bastani's avatar
Houtan Bastani committed
3
4
5
% Write the tikz file that contains the graph
%
% INPUTS
6
7
8
9
10
%   o   [graph]   graph object
%   pg  [integer] this page number
%   sec [integer] this section number
%   row [integer] this row number
%   col [integer] this col number
Houtan Bastani's avatar
Houtan Bastani committed
11
12
13
14
15
16
17
%
% OUTPUTS
%   o   [graph] graph object
%
% SPECIAL REQUIREMENTS
%   none

Houtan Bastani's avatar
Houtan Bastani committed
18
% Copyright (C) 2013-2015 Dynare Team
Houtan Bastani's avatar
Houtan Bastani committed
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
%
% 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(o.series);
if ne < 1
    warning('@graph.writeGraphFile: no series to plot, returning');
    return;
end

41
42
if isempty(o.graphName)
    o.graphName = sprintf('%s/graph_pg%d_sec%d_row%d_col%d.tex', o.graphDirName, pg, sec, row, col);
43
else
44
    o.graphName = [o.graphDirName '/' o.graphName];
Houtan Bastani's avatar
Houtan Bastani committed
45
46
end

47
[fid, msg] = fopen(o.graphName, 'w');
Houtan Bastani's avatar
Houtan Bastani committed
48
49
50
51
if fid == -1
    error(['@graph.writeGraphFile: ' msg]);
end

52
53
54
55
56
fprintf(fid, '\\begin{tikzpicture}[baseline');
if ~isempty(o.miscTikzPictureOptions)
    fprintf(fid, ',%s', o.miscTikzPictureOptions);
end
fprintf(fid, ']');
Houtan Bastani's avatar
Houtan Bastani committed
57
58
59
60
61
62
63

if isempty(o.xrange)
    dd = getMaxRange(o.series);
else
    dd = o.xrange;
end

Houtan Bastani's avatar
Houtan Bastani committed
64
fprintf(fid, '\\begin{axis}[%%\nset layers,');
Houtan Bastani's avatar
Houtan Bastani committed
65
66
% set tick labels
if isempty(o.xTickLabels)
67
    stringsdd = strings(dd);
68
69
    if ~isempty(o.shade)
        x1 = find(strcmpi(date2string(o.shade(1)), stringsdd));
70
71
72
73
74
75
76
77
78
79
80
        x2 = find(strcmpi(date2string(o.shade(end)), stringsdd));
        if x1 == 1
            x = [1 x2 dd.ndat];
            xTickLabels = [stringsdd(1) stringsdd(x2) stringsdd(end)];
        elseif x2 == dd.ndat
            x = [1 x1 dd.ndat];
            xTickLabels = [stringsdd(1) stringsdd(x1) stringsdd(end)];
        else
            x = [1 x1 x2 dd.ndat];
            xTickLabels = [stringsdd(1) stringsdd(x1) stringsdd(x2) stringsdd(end)];
        end
81
82
83
84
    else
        x = [1 dd.ndat];
        xTickLabels = [stringsdd(1) stringsdd(end)];
    end
Houtan Bastani's avatar
Houtan Bastani committed
85
    fprintf(fid, 'xminorticks=true,\nyminorticks=true,\n');
86
elseif iscell(o.xTickLabels)
Houtan Bastani's avatar
Houtan Bastani committed
87
88
89
    fprintf(fid,'minor xtick,\n');
    x = o.xTicks;
    xTickLabels = o.xTickLabels;
90
91
92
else
    x = [1:dd.ndat];
    xTickLabels = strings(dd);
Houtan Bastani's avatar
Houtan Bastani committed
93
94
end
fprintf(fid, 'xticklabels={');
95
96
xlen = length(x);
for i = 1:xlen
Houtan Bastani's avatar
Houtan Bastani committed
97
98
99
    fprintf(fid,'%s,',lower(xTickLabels{i}));
end
fprintf(fid, '},\nxtick={');
100
for i = 1:xlen
Houtan Bastani's avatar
Houtan Bastani committed
101
102
103
104
105
    fprintf(fid, '%d',x(i));
    if i ~= length(x)
        fprintf(fid,',');
    end
end
106
107
108
109
110
111
112
113
114
fprintf(fid, '},\ny tick label style={\n/pgf/number format/.cd,\n');
if o.yTickLabelFixed
    fprintf(fid, 'fixed,\n');
end
if o.yTickLabelZeroFill
    fprintf(fid, 'zerofill,\n');
end
fprintf(fid, 'precision=%d,\n/tikz/.cd\n},\n', o.yTickLabelPrecision);
fprintf(fid, 'x tick label style={rotate=%f', o.xTickLabelRotation);
115
116
117
118
119
if o.xTickLabelRotation ~= 0
    fprintf(fid, ',anchor=%s', o.xTickLabelAnchor);
end
fprintf(fid, ['},\n',...
              'width=%fin,\n'...
120
              'height=%fin,\n'...
Houtan Bastani's avatar
Houtan Bastani committed
121
              'scale only axis,\n'...
122
              'unbounded coords=jump,\n'], o.width, o.height);
123

124
125
126
127
128
129
if strcmpi(o.axisShape, 'box')
    fprintf(fid, 'axis lines=box,\n');
elseif strcmpi(o.axisShape, 'L')
    fprintf(fid, 'axis x line=bottom,\naxis y line=left,\n');
end

130
131
132
133
134
135
136
137
138
139
140
141
142
143
if ~isempty(o.title{1})
    fprintf(fid, 'title style={align=center');
    if ~isempty(o.titleFormat)
        fprintf(fid, ',font=%s', o.titleFormat);
    end
    fprintf(fid, '},\ntitle=');
    nt = length(o.title);
    for i=1:nt
        fprintf(fid, '%s', o.title{i});
        if i ~= nt
            fprintf(fid, '\\\\');
        end
    end
    fprintf(fid, ',\n');
144
145
end

146
147
148
149
150
if o.xAxisTight
    fprintf(fid, 'enlarge x limits=false,\n');
else
    fprintf(fid, 'enlarge x limits=true,\n');
end
151
152

if isempty(o.yrange)
153
154
155
156
157
    if o.yAxisTight
        fprintf(fid, 'enlarge y limits=false,\n');
    else
        fprintf(fid, 'enlarge y limits=true,\n');
    end
158
159
160
else
    fprintf(fid, 'ymin=%f,\nymax=%f,\n',o.yrange(1),o.yrange(2));
end
161
fprintf(fid, 'xmin = 1,\nxmax = %d,\n', length(dd));
Houtan Bastani's avatar
Houtan Bastani committed
162

163
164
165
166
167
168
169
170
171
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
172
173
174
175
176
    if isempty(o.legendAt)
        fprintf(fid, '},\nlegend pos=%s,\n', o.legendLocation);
    else
        fprintf(fid, 'at={(%f,%f)}},\n',o.legendAt(1),o.legendAt(2));
    end
177
178
end

179
180
fprintf(fid, 'tick label style={font=\\%s},\n', o.tickFontSize);

Houtan Bastani's avatar
Houtan Bastani committed
181
182
183
184
185
186
187
188
189
190
191
if o.showGrid
    fprintf(fid, 'xmajorgrids=true,\nymajorgrids=true,\n');
end

if ~isempty(o.xlabel)
    fprintf(fid, 'xlabel=%s,\n', o.xlabel);
end

if ~isempty(o.ylabel)
    fprintf(fid, 'ylabel=%s,\n', o.ylabel);
end
192

193
194
195
196
if ~o.yTickLabelScaled
    fprintf(fid, 'scaled y ticks = false,\n');
end

197
198
199
if ~isempty(o.miscTikzAxisOptions)
    fprintf(fid, '%s', o.miscTikzAxisOptions);
end
Houtan Bastani's avatar
Houtan Bastani committed
200
201
fprintf(fid, ']\n');

202
if ~isempty(o.title{1})
203
    fprintf(fid, '\\pgfplotsset{every axis title/.append style={}}=[font=\\%s]\n', o.titleFontSize);
204
205
end

Houtan Bastani's avatar
Houtan Bastani committed
206
if ~isempty(o.shade)
207
    fprintf(fid, '%%shading\n');
208
    stringsdd = strings(dd);
209
210
    x1 = find(strcmpi(date2string(o.shade(1)), stringsdd));
    x2 = find(strcmpi(date2string(o.shade(end)), stringsdd));
Houtan Bastani's avatar
Houtan Bastani committed
211
212
213
    assert(~isempty(x1) && ~isempty(x2), ['@graph.writeGraphFile: either ' ...
                        date2string(o.shade(1)) ' or ' date2string(o.shade(end)) ' is not in the date ' ...
                        'range of data selected.']);
Houtan Bastani's avatar
Houtan Bastani committed
214
    if x1 == 1
Houtan Bastani's avatar
Houtan Bastani committed
215
        fprintf(fid,['\\begin{pgfonlayer}{axis background}\n\\fill[%s!%f]\n(axis ' ...
Houtan Bastani's avatar
Houtan Bastani committed
216
217
218
219
                     'cs:\\pgfkeysvalueof{/pgfplots/xmin},\\pgfkeysvalueof{/pgfplots/ymin})\nrectangle (axis ' ...
                     'cs:%f,\\pgfkeysvalueof{/pgfplots/ymax});\n\\end{pgfonlayer}\n'], ...
                o.shadeColor, o.shadeOpacity, x2);
    elseif x2 == dd.ndat
Houtan Bastani's avatar
Houtan Bastani committed
220
        fprintf(fid,['\\begin{pgfonlayer}{axis background}\n\\fill[%s!%f]\n(axis ' ...
Houtan Bastani's avatar
Houtan Bastani committed
221
222
223
224
225
                     'cs:%f,\\pgfkeysvalueof{/pgfplots/ymin})\nrectangle (axis ' ...
                     'cs:\\pgfkeysvalueof{/pgfplots/xmax},\\pgfkeysvalueof{/' ...
                     'pgfplots/ymax});\n\\end{pgfonlayer}\n'], ...
                o.shadeColor, o.shadeOpacity, x1);
    else
Houtan Bastani's avatar
Houtan Bastani committed
226
        fprintf(fid,['\\begin{pgfonlayer}{axis background}\n\\fill[%s!%f]\n(axis ' ...
Houtan Bastani's avatar
Houtan Bastani committed
227
228
229
230
                     'cs:%f,\\pgfkeysvalueof{/pgfplots/ymin})\nrectangle (axis ' ...
                     'cs:%f,\\pgfkeysvalueof{/pgfplots/ymax});\n\\end{pgfonlayer}\n'], ...
                o.shadeColor, o.shadeOpacity, x1, x2);
    end
Houtan Bastani's avatar
Houtan Bastani committed
231
232
end

233
if o.showZeroline
234
235
    fprintf(fid, '%%zeroline\n\\addplot[%s,line width=.5,forget plot] coordinates {(1,0)(%d,0)};\n', ...
        o.zeroLineColor, dd.ndat);
236
237
end

238
239
240
if o.writeCSV
    csvseries = dseries();
end
241
242
for i=1:ne
    o.series{i}.writeSeriesForGraph(fid, dd);
243
244
245
246
247
248
249
250
251
252
253
254
255
256
    if o.writeCSV
        csvseries = [csvseries ...
            o.series{i}.data(dd).set_names([...
            o.series{i}.data.name{:} '_' ...
            o.series{i}.graphLegendName '_' ...
            o.series{i}.graphLineColor '_' ...
            o.series{i}.graphLineStyle '_' ...
            num2str(o.series{i}.graphLineWidth) '_' ...
            o.series{i}.graphMarker '_' ...
            o.series{i}.graphMarkerEdgeColor '_' ...
            o.series{i}.graphMarkerFaceColor '_' ...
            num2str(o.series{i}.graphMarkerSize)]) ...
            ];
    end
257
258
259
260
261
262
263
    if o.showLegend
        le = o.series{i}.getNameForLegend();
        if ~isempty(le)
            fprintf(fid, '\\addlegendentry{%s}\n', le);
        end
    end
end
264
265
266
if o.writeCSV
    csvseries.save(strrep(o.graphName, '.tex', ''), 'csv');
end
267
fprintf(fid, '\\end{axis}\n\\end{tikzpicture}%%');
Houtan Bastani's avatar
Houtan Bastani committed
268
269
270
if fclose(fid) == -1
    error('@graph.writeGraphFile: closing %s\n', o.filename);
end
Houtan Bastani's avatar
Houtan Bastani committed
271
end