Package PyFoam :: Package Basics :: Module QwtPlotTimelines
[hide private]
[frames] | no frames]

Source Code for Module PyFoam.Basics.QwtPlotTimelines

  1  #  ICE Revision: $Id: /local/openfoam/Python/PyFoam/PyFoam/Basics/QwtPlotTimelines.py 6093 2010-01-28T23:00:14.709549Z bgschaid  $  
  2  """Plots a collection of timelines""" 
  3   
  4  from PyFoam.Error import warning,error 
  5   
  6  from PyFoam.Basics.CustomPlotInfo import readCustomPlotInfo,CustomPlotInfo 
  7   
  8  from GeneralPlotTimelines import GeneralPlotTimelines 
  9   
 10  from os import uname 
 11   
 12  firstTimeImport=True 
 13  app=None 
 14   
 15   
16 -class QwtPlotTimelines(GeneralPlotTimelines):
17 """This class opens a Qt-window and plots a timelines-collection in aQwt.Plot-widget""" 18 19 figureNr=1 20
21 - def __init__(self, 22 timelines, 23 custom, 24 showWindow=True, 25 registry=None):
26 """@param timelines: The timelines object 27 @type timelines: TimeLineCollection 28 @param custom: A CustomplotInfo-object. Values in this object usually override the 29 other options 30 """ 31 32 try: 33 global Qt,Qwt,app 34 35 from PyQt4 import Qt 36 import PyQt4.Qwt5 as Qwt 37 38 if showWindow and app==None: 39 app = Qt.QApplication([]) 40 # app.thread() 41 except ImportError: 42 error("Could not import Qt4 or Qwt") 43 44 GeneralPlotTimelines.__init__(self,timelines,custom,showWindow=showWindow,registry=registry) 45 46 self.figNr=QwtPlotTimelines.figureNr 47 QwtPlotTimelines.figureNr+=1 48 49 self.figure=None 50 self.title="no title" 51 52 self.ylabel="no label" 53 self.ylabel2="no label" 54 try: 55 if self.spec.ylabel: 56 self.setYLabel(self.spec.ylabel) 57 except AttributeError: 58 pass 59 try: 60 if self.spec.y2label: 61 self.setYLabel2(self.spec.y2label) 62 except AttributeError: 63 pass 64 65 self.axis1=None 66 self.axis2=None 67 68 self.setTitle(self.spec.theTitle) 69 70 self.with_=self.spec.with_ 71 if not self.with_ in ['lines']: 72 warning("'with'-style",self.with_,"not implemented, using 'lines'") 73 self.with_='lines' 74 75 self.curves={} 76 77 self.redo()
78
79 - def buildData(self,times,name,title,lastValid):
80 """Build the implementation specific data 81 @param times: The vector of times for which data exists 82 @param name: the name under which the data is stored in the timeline 83 @param title: the title under which this will be displayed""" 84 85 if self.figure==None: 86 return 87 88 axis=self.axis1 89 if name in self.alternate: 90 a=self.axis2 91 data=self.data.getValues(name) 92 tm=times 93 if len(tm)>0 and not lastValid: 94 tm=tm[:-1] 95 data=data[:-1] 96 plotIt=True 97 try: 98 if self.spec.logscale and min(data)<=0: 99 plotIt=False 100 except AttributeError: 101 pass 102 103 if not plotIt: 104 return 105 106 if name not in self.curves: 107 a=Qwt.QwtPlotCurve(title) 108 print "Plot",dir(a) 109 a.attach(self.figure) 110 a.setPen(Qt.QPen(Qt.Qt.red)) 111 self.curves[name]=a 112 self.figure.update() 113 114 a=self.curves[name] 115 a.setData(tm,data) 116 # print "Figure",dir(self.figure) 117 self.figure.replot()
118 119 ## drawstyle='default' 120 ## marker='' 121 ## linestyle='-' 122 123 ## if self.with_=='lines': 124 ## pass 125 ## elif self.with_=='steps': 126 ## drawstyle='steps' 127 ## elif self.with_=='points': 128 ## linestyle='' 129 ## marker='*' 130 ## elif self.with_=='dots': 131 ## linestyle='' 132 ## marker='.' 133 ## elif self.with_=='linespoints': 134 ## marker='*' 135 ## else: 136 ## warning("'with'-style",self.with_,"not implemented, using 'lines'") 137 138 ## if plotIt: 139 ## a.plot(tm, 140 ## data, 141 ## label=title, 142 ## drawstyle=drawstyle, 143 ## marker=marker, 144 ## linestyle=linestyle) 145
146 - def preparePlot(self):
147 """Prepare the plotting window""" 148 if self.figure: 149 return 150 self.figure=Qwt.QwtPlot() 151 self.figure.setCanvasBackground(Qt.Qt.white) 152 self.figure.canvas().setFrameStyle(Qt.QFrame.Box | Qt.QFrame.Plain) 153 self.figure.canvas().setLineWidth(1) 154 for i in range(Qwt.QwtPlot.axisCnt): 155 scaleWidget = self.figure.axisWidget(i) 156 if scaleWidget: 157 scaleWidget.setMargin(0) 158 scaleDraw = self.figure.axisScaleDraw(i) 159 if scaleDraw: 160 scaleDraw.enableComponent( 161 Qwt.QwtAbstractScaleDraw.Backbone, False) 162 self.figure.setTitle("Figure: %d - %s" % (self.figNr,self.title)) 163 self.figure.insertLegend(Qwt.QwtLegend(), Qwt.QwtPlot.BottomLegend) 164 165 self.figure.setAxisTitle(Qwt.QwtPlot.xBottom, "Time") 166 self.figure.setAxisTitle(Qwt.QwtPlot.yLeft, self.ylabel) 167 self.axis1=Qwt.QwtPlot.yLeft 168 if len(self.alternate)>0: 169 self.figure.enableAxis(Qwt.QwtPlot.yRight) 170 self.figure.setAxisTitle(Qwt.QwtPlot.yRight, self.ylabel2) 171 self.axis2=Qwt.QwtPlot.yRight 172 173 if self.spec.logscale: 174 self.figure.setAxisScaleEngine(Qwt.QwtPlot.yLeft, 175 Qwt.QwtLog10ScaleEngine()) 176 if len(self.alternate)>0: 177 self.figure.setAxisScaleEngine(Qwt.QwtPlot.yRight, 178 Qwt.QwtLog10ScaleEngine()) 179 180 mY = Qwt.QwtPlotMarker() 181 mY.setLabelAlignment(Qt.Qt.AlignRight | Qt.Qt.AlignTop) 182 mY.setLineStyle(Qwt.QwtPlotMarker.HLine) 183 mY.setYValue(0.0) 184 mY.attach(self.figure) 185 186 self.figure.resize(500,300) 187 self.figure.show()
188 189 ## self.figure=plt.figure(self.figNr) 190 ## self.figure.clear() 191 ## # this is black magic that makes the legend work with two axes 192 ## if self.hasSubplotHost: 193 ## self.axis1=SubplotHost(self.figure,111) 194 ## self.figure.add_subplot(self.axis1) 195 ## else: 196 ## self.axis1=self.figure.add_subplot(111) 197 ## self.axis1.set_xlabel("Time") 198 ## self.axis1.set_ylabel(self.ylabel) 199 ## if self.spec.start or self.spec.end: 200 ## self.axis1.set_xbound(lower=self.spec.start,upper=self.spec.end) 201 202 ## if len(self.alternate)>0: 203 ## self.axis2=self.axis1.twinx() 204 ## self.axis2.set_ylabel(self.ylabel2) 205 206 ## try: 207 ## if self.spec.logscale: 208 ## self.axis1.set_yscale("log") 209 ## if self.axis2: 210 ## self.axis2.set_yscale("log") 211 ## except AttributeError: 212 ## pass 213
214 - def doReplot(self):
215 """Replot the whole data""" 216 217 self.figure.replot()
218 219 ## if self.hasSubplotHost: 220 ## l=self.axis1.legend(fancybox=True) 221 ## else: 222 ## l=plt.legend(fancybox=True) 223 ## # l.get_frame().set_fill(False) 224 ## if l: 225 ## l.get_frame().set_alpha(0.7) 226 ## l.get_texts()[0].set_size(10) 227 ## plt.suptitle(self.title) 228 ## plt.draw() 229
230 - def actualSetTitle(self,title):
231 """Sets the title""" 232 233 self.title=title
234
235 - def setYLabel(self,title):
236 """Sets the label on the first Y-Axis""" 237 238 self.ylabel=title
239
240 - def setYLabel2(self,title):
241 """Sets the label on the second Y-Axis""" 242 243 self.ylabel2=title
244
245 - def doHardcopy(self,filename,form):
246 """Write the contents of the plot to disk 247 @param filename: Name of the file without type extension 248 @param form: String describing the format""" 249 250 Qt.QPixmap.grabWidget(self.figure).save(filename+"."+form.lower(),form)
251