home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / pytho152.zip / emx / lib / python1.5 / lib-stdwin / StripChart.py < prev    next >
Text File  |  2000-08-10  |  2KB  |  71 lines

  1. # Module 'StripChart'
  2.  
  3. import rect
  4. from Buttons import LabelAppearance, NoReactivity
  5.  
  6. # A StripChart doesn't really look like a label but it needs a base class.
  7. # LabelAppearance allows it to be disabled and hilited.
  8.  
  9. class StripChart(LabelAppearance, NoReactivity):
  10.     #
  11.     def define(self, parent, scale):
  12.         self.parent = parent
  13.         parent.addchild(self)
  14.         self.init_appearance()
  15.         self.init_reactivity()
  16.         self.ydata = []
  17.         self.scale = scale
  18.         self.resetbounds()
  19.         return self
  20.     #
  21.     def destroy(self):
  22.         self.parent = 0
  23.     #
  24.     def setbounds(self, bounds):
  25.         LabelAppearance.setbounds(self, bounds)
  26.         self.resetbounds()
  27.     #
  28.     def resetbounds(self):
  29.         (left, top), (right, bottom) = self.bounds
  30.         self.width = right-left
  31.         self.height = bottom-top
  32.         excess = len(self.ydata) - self.width
  33.         if excess > 0:
  34.             del self.ydata[:excess]
  35.         elif excess < 0:
  36.             while len(self.ydata) < self.width:
  37.                 self.ydata.insert(0, 0)
  38.     #
  39.     def append(self, y):
  40.         self.ydata.append(y)
  41.         excess = len(self.ydata) - self.width
  42.         if excess > 0:
  43.             del self.ydata[:excess]
  44.             if self.bounds <> rect.empty:
  45.                 self.parent.scroll(self.bounds, (-excess, 0))
  46.         if self.bounds <> rect.empty:
  47.             (left, top), (right, bottom) = self.bounds
  48.             i = len(self.ydata)
  49.             area = (left+i-1, top), (left+i, bottom)
  50.             self.draw(self.parent.begindrawing(), area)
  51.     #
  52.     def draw(self, d, area):
  53.         area = rect.intersect([area, self.bounds])
  54.         if area == rect.empty:
  55.             return
  56.         d.cliprect(area)
  57.         d.erase(self.bounds)
  58.         (a_left, a_top), (a_right, a_bottom) = area
  59.         (left, top), (right, bottom) = self.bounds
  60.         height = bottom - top
  61.         i1 = a_left - left
  62.         i2 = a_right - left
  63.         for i in range(max(0, i1), min(len(self.ydata), i2)):
  64.             split = bottom-self.ydata[i]*height/self.scale
  65.             d.paint((left+i, split), (left+i+1, bottom))
  66.         if not self.enabled:
  67.             self.flipenable(d)
  68.         if self.hilited:
  69.             self.fliphilite(d)
  70.         d.noclip()
  71.