annotate plothandler.py @ 153:ebdd20cfba69

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