2 # -*- coding: utf-8 -*-
4 # Server monitoring system
6 # Copyright © 2011 Rodrigo Eduardo Lazo Paz
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
23 """Data Plotting routines.
25 Requires matplotlib with cairo support built-in."""
28 __author__
= "rlazo.paz@gmail.com (Rodrigo Lazo)"
33 matplotlib
.use('cairo.png')
35 from datetime
import datetime
36 import matplotlib
.pyplot
as plot
37 from pylab
import epoch2num
44 # TODO (09/20): Make this functions more generic and clear, right now
45 # they depend on `DataEntry` objects.
46 def new_multigraph(data_dict
, title
, target_dir
=""):
47 """Creates a single image with multiple plots.
49 `data_dict` must be in the form:
50 {'key': `DataEntry`, 'key': `DataEntry`}
52 'key' will be the label of the plot.
55 - `data_dict`: Dict, data source.
56 - `title`: String, title of the plot.
57 - `target_dir`: String, path where to write the image file.
60 String, filename of the generated image.
62 for name
, values
in data_dict
.iteritems():
63 data
= values
.get_all()
64 xs
= [x
.timestamp
for x
in data
] # pylint: disable=C0103
65 ys
= [x
.value
for x
in data
] # pylint: disable=C0103
66 plot
.plot_date(epoch2num(xs
),
69 plot
.xlabel("timestamp")
71 plot
.legend(loc
="upper right")
73 filename
= "%s-%s.png" % (title
.replace(' ', '_'),
74 datetime
.now().strftime("%Y%m%d_%H%M%S"))
75 target_file
= str(os
.path
.join(target_dir
, filename
))
76 plot
.savefig(target_file
, dpi
=IMAGE_DPI
)
78 return filename
, target_file
81 def new_graph(data_lst
, title
, target_dir
):
82 """Creates a single image with multiple plots.
84 `data_lst` must be a list of pairs in the form:
85 [(x, y), (x,y), (x,y)]
88 - `data_lst`: Iterable, data source.
89 - `title`: String, title of the plot.
90 - `target_dir`: String, path where to write the image file.
93 String, filename of the generated image.
95 xs
= [x
[0] for x
in data_lst
] # pylint: disable=C0103
96 ys
= [x
[1] for x
in data_lst
] # pylint: disable=C0103
97 plot
.plot_date(epoch2num(xs
), ys
, '-')
99 plot
.xlabel("timestamp")
101 plot
.legend(loc
="upper right")
103 now_str
= datetime
.now().strftime("%Y%m%d_%H%M%S")
104 filename
= "%s-%s.png" % (title
.replace(' ', '_'), now_str
)
105 target_file
= str(os
.path
.join(target_dir
, filename
))
106 print now_str
, filename
, target_file
107 plot
.savefig(target_file
, dpi
=IMAGE_DPI
)
109 return filename
, target_file