createGraph.m 3.77 KB
Newer Older
Houtan Bastani's avatar
Houtan Bastani committed
1
2
3
4
5
function o = createGraph(o)
%function o = createGraph(o)
% Create the graph
%
% INPUTS
Houtan Bastani's avatar
Houtan Bastani committed
6
%   o   [graph] graph object
Houtan Bastani's avatar
Houtan Bastani committed
7
8
%
% OUTPUTS
Houtan Bastani's avatar
Houtan Bastani committed
9
%   o   [graph] graph object
Houtan Bastani's avatar
Houtan Bastani committed
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
%
% SPECIAL REQUIREMENTS
%   none

% Copyright (C) 2013 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/>.

if ~isempty(o.figname)
32
33
    warning('@graph.createGraph: will overwrite %s with new graph\n', ...
            o.figname);
Houtan Bastani's avatar
Houtan Bastani committed
34
35
end

36
37
38
39
40
if ~o.seriesElements.numElements()
    warning('@graph.crepateGraph: no series to plot, returning');
    return;
end

Houtan Bastani's avatar
Houtan Bastani committed
41
42
43
h = figure('visible','off');
hold on;
box on;
44
if o.showGrid
Houtan Bastani's avatar
Houtan Bastani committed
45
46
47
    grid on;
    set(gca, 'GridLineStyle', '--');
end
Houtan Bastani's avatar
Houtan Bastani committed
48

49
50
51
52
53
54
if isempty(o.xrange)
    dd = o.seriesElements.getMaxRange();
else
    dd = o.xrange;
end

55
ne = o.seriesElements.numElements();
56
line_handles = zeros(ne, 1);
57
for i=1:ne
58
    line_handles(i) = o.seriesElements(i).getLine(dd);
Houtan Bastani's avatar
Houtan Bastani committed
59
60
end

61
x = 1:1:dd.ndat;
62
xlim([1 dd.ndat]);
63
xlabels = getDatesCellStringArray(dd);
Houtan Bastani's avatar
Houtan Bastani committed
64

65
if ~isempty(o.yrange)
66
    ylim(o.yrange);
Houtan Bastani's avatar
Houtan Bastani committed
67
68
end

69
if o.showZeroline
70
71
72
73
74
75
76
77
78
    a = ylim;
    if 0 > a(1) && 0 < a(2)
        lh = line(xlim, [0 0], 'color', 'k', 'LineWidth', 0.25);
        children =get(gca(), 'children');
        children = [children(2:end); lh];
        set(gca(), 'children', children);
    end
end

Houtan Bastani's avatar
Houtan Bastani committed
79
if ~isempty(o.shade)
80
81
    x1 = find(strcmpi(o.shade(1).format(), xlabels));
    x2 = find(strcmpi(o.shade(o.shade.ndat).format(), xlabels));
Houtan Bastani's avatar
Houtan Bastani committed
82
    assert(~isempty(x1) && ~isempty(x2), ['@graph.createGraph: either ' ...
83
                        o.shade(1).format() ' or ' o.shade(o.shade.ndat).format() ' is not in the date ' ...
Houtan Bastani's avatar
Houtan Bastani committed
84
                        'range of data selected.']);
Houtan Bastani's avatar
Houtan Bastani committed
85
86
87
88
    yrange = get(gca, 'YLim');

    % From ShadePlotForEmpahsis (Matlab Exchange)
    % use patch bc area doesn't work with matlab2tikz
89
    sh = patch([repmat(x1, 1, 2) repmat(x2, 1, 2)], ...
90
91
               [yrange fliplr(yrange)], o.shadeColor, ...
               'facealpha', o.shadeOpacity);
Houtan Bastani's avatar
Houtan Bastani committed
92
    children = get(gca, 'children');
93
    children = [children(2:end); sh];
Houtan Bastani's avatar
Houtan Bastani committed
94
    set(gca, 'children', children);
Houtan Bastani's avatar
Houtan Bastani committed
95
96
end

97
xticks = get(gca, 'XTick');
Houtan Bastani's avatar
Houtan Bastani committed
98
99
100
101
102
103
104
105
106
107
xTickLabels = cell(1, length(xticks));
for i=1:length(xticks)
    if xticks(i) >= x(1) && ...
            xticks(i) <= x(end)
        xTickLabels{i} = xlabels{xticks(i)};
    else
        xTickLabels{i} = '';
    end
end
set(gca, 'XTickLabel', xTickLabels);
Houtan Bastani's avatar
Houtan Bastani committed
108

109
if o.showLegend
110
    lh = legend(line_handles, o.seriesElements.getTexNames(), ...
111
112
113
                'orientation', o.legendOrientation, ...
                'location', o.legendLocation);
    set(lh, 'FontSize', o.legendFontSize);
114
    set(lh, 'interpreter', 'latex');
115
    if ~o.showLegendBox
116
117
        legend('boxoff');
    end
Houtan Bastani's avatar
Houtan Bastani committed
118
119
120
end

if ~isempty(o.xlabel)
Houtan Bastani's avatar
Houtan Bastani committed
121
    xlabel(['$\textbf{\footnotesize ' o.xlabel '}$'], 'Interpreter', 'LaTex');
Houtan Bastani's avatar
Houtan Bastani committed
122
123
124
end

if ~isempty(o.ylabel)
Houtan Bastani's avatar
Houtan Bastani committed
125
    ylabel(['$\textbf{\footnotesize ' o.ylabel '}$'], 'Interpreter', 'LaTex');
Houtan Bastani's avatar
Houtan Bastani committed
126
127
128
end
drawnow;

129
130
131
if isempty(o.figname)
    o.figname = [tempname '.tex'];
end
Houtan Bastani's avatar
Houtan Bastani committed
132
disp('  converting to tex....');
133
134
135
136
137
138
139
140
if exist('OCTAVE_VERSION')
    print(o.figname, '-dtikz');
else
    matlab2tikz('filename', o.figname, ...
                'showInfo', false, ...
                'showWarnings', false, ...
                'checkForUpdates', false);
end
Houtan Bastani's avatar
Houtan Bastani committed
141

Houtan Bastani's avatar
Houtan Bastani committed
142
grid off;
Houtan Bastani's avatar
Houtan Bastani committed
143
144
145
146
147
box off;
hold off;
close(h);
clear h;
end