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

Source Code for Module PyFoam.Basics.QwtPlotTimelines

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