graph.m 5.54 KB
Newer Older
Houtan Bastani's avatar
Houtan Bastani committed
1
2
3
4
5
function o = graph(varargin)
%function o = graph(varargin)
% Graph Class Constructor
%
% INPUTS
Houtan Bastani's avatar
Houtan Bastani committed
6
7
8
9
%   varargin        0 args  : empty graph object
%                   1 arg   : must be graph object (return a copy of arg)
%                   > 1 args: option/value pairs (see structure below for
%                   options)
Houtan Bastani's avatar
Houtan Bastani committed
10
11
%
% OUTPUTS
Houtan Bastani's avatar
Houtan Bastani committed
12
%   o   [graph] graph object
Houtan Bastani's avatar
Houtan Bastani committed
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
%
% 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/>.

o = struct;
Houtan Bastani's avatar
Houtan Bastani committed
35
36
37

o.config = '';

38
39
o.seriesElements = seriesElements();

Houtan Bastani's avatar
Houtan Bastani committed
40
41
42
o.title = '';
o.ylabel = '';
o.xlabel = '';
Houtan Bastani's avatar
Houtan Bastani committed
43
o.footnote = '';
Houtan Bastani's avatar
Houtan Bastani committed
44
45

o.figname = '';
Houtan Bastani's avatar
Houtan Bastani committed
46
o.data = '';
47
o.seriesToUse = '';
48
49
o.xrange = '';
o.yrange = '';
Houtan Bastani's avatar
Houtan Bastani committed
50

51
o.shade = '';
52
o.shadeColor = 'green';
53
o.shadeOpacity = .2;
54

55
o.showGrid = true;
Houtan Bastani's avatar
Houtan Bastani committed
56

57
o.showLegend = false;
58
o.showLegendBox = false;
59
60
61
o.legendLocation = 'SouthEast';
o.legendOrientation = 'horizontal';
o.legendFontSize = 8;
Houtan Bastani's avatar
Houtan Bastani committed
62

63
o.showZeroline = false;
64

Houtan Bastani's avatar
Houtan Bastani committed
65
if nargin == 1
66
67
    assert(isa(varargin{1}, 'graph'),['@graph.graph: with one arg you ' ...
                        'must pass a graph object']);
Houtan Bastani's avatar
Houtan Bastani committed
68
69
70
71
    o = varargin{1};
    return;
elseif nargin > 1
    if round(nargin/2) ~= nargin/2
72
        error(['@graph.graph: options must be supplied in name/value ' ...
Houtan Bastani's avatar
Houtan Bastani committed
73
74
75
               'pairs.']);
    end

76
    optNames = fieldnames(o);
Houtan Bastani's avatar
Houtan Bastani committed
77
78
79

    % overwrite default values
    for pair = reshape(varargin, 2, [])
80
        ind = strmatch(lower(pair{1}), lower(optNames), 'exact');
81
82
83
        assert(isempty(ind) || length(ind) == 1);
        if ~isempty(ind)
            o.(optNames{ind}) = pair{2};
Houtan Bastani's avatar
Houtan Bastani committed
84
        else
85
            error('@graph.graph: %s is not a recognized option.', pair{1});
Houtan Bastani's avatar
Houtan Bastani committed
86
87
88
89
        end
    end
end

90
91
92
93
94
95
96
% Check options provided by user
assert(ischar(o.title), '@graph.graph: title must be a string');
assert(ischar(o.footnote), '@graph.graph: footnote must be a string');
assert(ischar(o.config), '@graph.graph: config file must be a string');
assert(ischar(o.xlabel), '@graph.graph: xlabel file must be a string');
assert(ischar(o.ylabel), '@graph.graph: ylabel file must be a string');
assert(ischar(o.figname), '@graph.graph: figname must be a string');
97
assert(islogical(o.showGrid), '@graph.graph: showGrid must be either true or false');
98
assert(islogical(o.showLegend), '@graph.graph: showLegend must be either true or false');
99
assert(islogical(o.showLegendBox), '@graph.graph: showLegendBox must be either true or false');
100
assert(isint(o.legendFontSize), '@graph.graph: legendFontSize must be an integer');
101
assert(islogical(o.showZeroline), '@graph.graph: showZeroline must be either true or false');
102
assert(ischar(o.shadeColor), '@graph.graph: shadeColor must be a string');
103
104
105
assert(isfloat(o.shadeOpacity) && length(o.shadeOpacity)==1 && ...
       o.shadeOpacity >= 0 && o.shadeOpacity <= 1, ...
       '@graph.graph: o.shadeOpacity must be a real in [0 1]');
106
107
108
109
110
111
112
valid_legend_locations = ...
    {'North', 'South', 'East', 'West', ...
     'NorthEast', 'SouthEast', 'NorthWest', 'SouthWest', ...
     'NorthOutside', 'SouthOutside', 'EastOutside', 'WestOutside', ...
     'NorthEastOutside', 'SouthEastOutside', 'NorthWestOutside', 'SouthWestOutside', ...
     'Best', 'BestOutside', ...
    };
113
114
assert(any(strcmp(o.legendLocation, valid_legend_locations)), ...
       ['@graph.graph: legendLocation must be one of ' strjoin(valid_legend_locations, ' ')]);
115
116

valid_legend_orientations = {'vertical', 'horizontal'};
117
118
assert(any(strcmp(o.legendOrientation, valid_legend_orientations)), ...
       ['@graph.graph: legendOrientation must be one of ' strjoin(valid_legend_orientations, ' ')]);
119

120
121
122
123
124
125
126
127
128
129
assert(isempty(o.shade) || (isa(o.shade, 'dynDates') && o.shade.ndat >= 2), ...
       ['@graph.graph: shade is specified as a dynDates range, e.g. ' ...
        '''dynDates(''1999q1''):dynDates(''1999q3'')''.']);
assert(isempty(o.xrange) || (isa(o.xrange, 'dynDates') && o.xrange.ndat >= 2), ...
       ['@graph.graph: xrange is specified as a dynDates range, e.g. ' ...
        '''dynDates(''1999q1''):dynDates(''1999q3'')''.']);
assert(isempty(o.yrange) || (isfloat(o.yrange) && length(o.yrange) == 2 && ...
                             o.yrange(1) < o.yrange(2)), ...
       ['@graph.graph: yrange is specified an array with two float entries, ' ...
        'the lower bound and upper bound.']);
130
131
assert(isempty(o.data) || isa(o.data, 'dynSeries'), ['@graph.graph: data must ' ...
                    'be a dynSeries']);
132
assert(isempty(o.seriesToUse) || iscellstr(o.seriesToUse), ['@graph.graph: ' ...
133
                    'series to use must be a cell array of string(s)']);
134

135
% using o.seriesToUse, create series objects and put them in o.seriesElements
136
if ~isempty(o.data)
137
    if isempty(o.seriesToUse)
138
        for i=1:o.data.vobs
Houtan Bastani's avatar
Houtan Bastani committed
139
            o.seriesElements = o.seriesElements.addSeries('data', o.data{o.data.name{i}});
140
141
        end
    else
142
143
        for i=1:length(o.seriesToUse)
            o.seriesElements = o.seriesElements.addSeries('data', o.data{o.seriesToUse{i}});
144
145
146
        end
    end
end
147
o = rmfield(o, 'seriesToUse');
148
149
o = rmfield(o, 'data');

Houtan Bastani's avatar
Houtan Bastani committed
150
151
152
% Create graph object
o = class(o, 'graph');
end