Guido van Rossum | e1f069e | 1990-10-25 18:51:42 +0000 | [diff] [blame] | 1 | # Module 'Histogram' |
| 2 | |
| 3 | from Buttons import * |
| 4 | |
| 5 | |
| 6 | # A Histogram displays a histogram of numeric data. |
| 7 | # It reacts to resize events by resizing itself, |
| 8 | # leaving the same amount of space around the borders. |
| 9 | # |
| 10 | class HistogramAppearance() = LabelAppearance(): |
| 11 | # |
| 12 | def define(self, (win, bounds, ydata, scale)): |
| 13 | self.init_appearance(win, bounds) |
| 14 | self.ydata = ydata |
| 15 | self.scale = scale # (min, max) |
| 16 | self.left_top, (right, bottom) = bounds |
| 17 | width, height = win.getwinsize() |
| 18 | self.right_margin = width - right |
| 19 | self.bottom_margin = height - bottom |
| 20 | return self |
| 21 | # |
| 22 | def setdata(self, (ydata, scale)): |
| 23 | self.ydata = ydata |
| 24 | self.scale = scale # (min, max) |
| 25 | self.win.change(self.bounds) |
| 26 | # |
| 27 | def drawit(self, d): |
| 28 | ydata = self.ydata |
| 29 | (left, top), (right, bottom) = self.bounds |
| 30 | min, max = self.scale |
| 31 | size = max-min |
| 32 | width, height = right-left, bottom-top |
| 33 | for i in range(len(ydata)): |
| 34 | h0 = left + i * width/len(ydata) |
| 35 | h1 = left + (i+1) * width/len(ydata) |
| 36 | v0 = top + height - (self.ydata[i]-min)*height/size |
| 37 | v1 = top + height |
| 38 | d.paint((h0, v0), (h1, v1)) |
| 39 | # |
| 40 | def resize(self): |
| 41 | width, height = self.win.getwinsize() |
| 42 | right = width - self.right_margin |
| 43 | bottom = height - self.bottom_margin |
| 44 | self.setbounds(self.left_top, (right, bottom)) |
| 45 | # |
| 46 | |
| 47 | class HistogramReactivity() = NoReactivity(): pass |
| 48 | |
| 49 | class Histogram() = HistogramAppearance(), HistogramReactivity(): pass |