Guido van Rossum | 36ddc9e | 1990-10-31 11:24:22 +0000 | [diff] [blame] | 1 | # Module 'StripChart' |
| 2 | |
| 3 | |
| 4 | import rect |
| 5 | from Buttons import * |
| 6 | from Resize import * |
| 7 | |
| 8 | |
| 9 | class StripChart() = LabelAppearance(), NoReactivity(), NoResize(): |
| 10 | # |
| 11 | def define(self, (win, bounds, scale)): |
| 12 | self.init_appearance(win, bounds) |
| 13 | self.init_reactivity() |
| 14 | self.init_resize() |
| 15 | self.ydata = [] |
| 16 | self.scale = scale |
| 17 | self.resetbounds() |
| 18 | return self |
| 19 | # |
| 20 | def setbounds(self, bounds): |
| 21 | LabelAppearance.setbounds(self, bounds) |
| 22 | self.resetbounds() |
| 23 | # |
| 24 | def resetbounds(self): |
| 25 | (left, top), (right, bottom) = self.bounds |
| 26 | self.width = right-left |
| 27 | self.height = bottom-top |
| 28 | excess = len(self.ydata) - self.width |
| 29 | if excess > 0: |
| 30 | del self.ydata[:excess] |
| 31 | elif excess < 0: |
| 32 | while len(self.ydata) < self.width: |
| 33 | self.ydata.insert(0, 0) |
| 34 | # |
| 35 | def append(self, y): |
| 36 | self.ydata.append(y) |
| 37 | excess = len(self.ydata) - self.width |
| 38 | if excess > 0: |
| 39 | del self.ydata[:excess] |
| 40 | if not self.limbo: |
| 41 | self.win.scroll(self.bounds, (-excess, 0)) |
| 42 | if not self.limbo: |
| 43 | (left, top), (right, bottom) = self.bounds |
| 44 | i = len(self.ydata) |
| 45 | area = (left+i-1, top), (left+i, bottom) |
| 46 | self.draw(self.win.begindrawing(), area) |
| 47 | # |
| 48 | def draw(self, (d, area)): |
| 49 | self.limbo = 0 |
| 50 | area = rect.intersect(area, self.bounds) |
| 51 | if area = rect.empty: |
| 52 | return |
| 53 | d.cliprect(area) |
| 54 | d.erase(self.bounds) |
| 55 | (a_left, a_top), (a_right, a_bottom) = area |
| 56 | (left, top), (right, bottom) = self.bounds |
| 57 | height = bottom - top |
| 58 | i1 = a_left - left |
| 59 | i2 = a_right - left |
| 60 | for i in range(max(0, i1), min(len(self.ydata), i2)): |
| 61 | split = bottom-self.ydata[i]*height/self.scale |
| 62 | d.paint((left+i, split), (left+i+1, bottom)) |
| 63 | if not self.enabled: |
| 64 | self.flipenable(d) |
| 65 | if self.hilited: |
| 66 | self.fliphilite(d) |
| 67 | d.noclip() |