153
|
1 import shelve, pickle, cx_Oracle, datetime, sys
|
|
2 shelvename = 'plot.shelve'
|
|
3
|
|
4 try:
|
|
5 import pylab
|
|
6 class Plot(object):
|
|
7 plottable_types = (cx_Oracle.NUMBER, datetime.datetime)
|
|
8 def __init__(self):
|
|
9 self.legends = []
|
|
10 self.yserieslists = []
|
|
11 self.xticks = []
|
|
12 def build(self, sqlSession):
|
|
13 self.title = sqlSession.tblname
|
|
14 self.xlabel = sqlSession.curs.description[0][0]
|
|
15 self.datatypes = [d[1] for d in sqlSession.curs.description]
|
|
16 for (colNum, datatype) in enumerate(self.datatypes):
|
|
17 if colNum > 0 and datatype in self.plottable_types:
|
|
18 yseries = [row[colNum] for row in sqlSession.rows]
|
|
19 if max(yseries) is not None:
|
|
20 self.yserieslists.append(yseries)
|
|
21 self.legends.append(sqlSession.curs.description[colNum][0])
|
|
22 if self.datatypes[0] in self.plottable_types:
|
|
23 self.xvalues = [r[0] for r in sqlSession.rows]
|
|
24 else:
|
|
25 self.xvalues = range(sqlSession.curs.rowcount)
|
|
26 self.xticks = [r[0] for r in sqlSession.rows]
|
|
27 def shelve(self):
|
|
28 s = shelve.open(shelvename,'c')
|
|
29 for k in ('xvalues xticks yserieslists title legends xlabel'.split()):
|
|
30 s[k] = getattr(self, k)
|
|
31 s.close()
|
|
32 # reading pickles fails with EOF error, don't understand
|
|
33 def unshelve(self):
|
|
34 s = shelve.open(shelvename)
|
|
35 self.__dict__.update(s)
|
|
36 s.close()
|
|
37 self.draw()
|
|
38 def draw(self):
|
|
39 if not self.yserieslists:
|
|
40 print 'At least one quantitative column needed to plot.'
|
|
41 return None
|
|
42 for yseries in self.yserieslists:
|
|
43 pylab.plot(self.xvalues, yseries, '-o')
|
|
44 if self.xticks:
|
|
45 pylab.xticks(self.xvalues, self.xticks)
|
|
46 pylab.xlabel(self.xlabel)
|
|
47 pylab.title(self.title)
|
|
48 pylab.legend(self.legends)
|
|
49 pylab.show()
|
|
50
|
|
51 except ImportError:
|
|
52 class Plot(object):
|
|
53 def build(self, sqlSession):
|
|
54 pass
|
|
55 def save(self):
|
|
56 pass
|
|
57 def draw(self):
|
|
58 return 'Must install python-matplotlib to plot query results.' |