Changeset 73
- Timestamp:
- 12/20/07 06:00:51 (4 years ago)
- Location:
- trunk
- Files:
-
- 4 modified
-
examples/hbarchart.png (modified) (previous)
-
examples/vbarchart.png (modified) (previous)
-
src/bar.py (modified) (4 diffs)
-
tests/bar.py (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/bar.py
r65 r73 24 24 super(BarChart, self).__init__(surface, options) 25 25 self.bars = [] 26 self.minxdelta = 0 26 self.minxdelta = 0.0 27 self.barWidthForSet = 0.0 28 self.barMargin = 0.0 29 30 def _updateChart(self): 31 """Evaluates measures for vertical bars""" 32 stores = self._getDatasetsValues() 33 uniqx = uniqueIndices(stores) 34 xdelta = min([abs(uniqx[j] - uniqx[j-1]) for j in range(1, len(uniqx))]) 35 36 barWidth = 0 37 if len(uniqx) == 1: 38 xdelta = 1.0 39 self.xscale = 1.0 40 self.minxval = uniqx[0] 41 barWidth = 1.0 * self.options.barWidthFillFraction 42 self.barWidthForSet = barWidth / len(stores) 43 self.barMargin = (1.0 - self.options.barWidthFillFraction) / 2 44 else: 45 self.xscale = 1.0 / (self.xrange + 1) 46 barWidth = xdelta * self.xscale * self.options.barWidthFillFraction 47 self.barWidthForSet = barWidth / len(stores) 48 self.barMargin = (xdelta * self.xscale 49 * (1.0 - self.options.barWidthFillFraction) / 2) 50 51 self.minxdelta = xdelta 52 self.bars = [] 27 53 28 54 def _renderChart(self, cx): … … 65 91 def _updateChart(self): 66 92 """Evaluates measures for vertical bars""" 67 stores = self._getDatasetsValues() 68 uniqx = uniqueIndices(stores) 69 xdelta = min([abs(uniqx[j] - uniqx[j-1]) for j in range(1, len(uniqx))]) 70 71 barWidth = 0 72 barWidthForSet = 0 73 barMargin = 0 74 if len(uniqx) == 1: 75 xdelta = 1.0 76 self.xscale = 1.0 77 self.minxval = uniqx[0] 78 barWidth = 1.0 * self.options.barWidthFillFraction 79 barWidthForSet = barWidth / len(stores) 80 barMargin = (1.0 - self.options.barWidthFillFraction) / 2 81 else: 82 if self.xrange == 1: 83 self.xscale = 0.5 84 elif self.xrange == 2: 85 self.xscale = 1 / 3.0 86 else: 87 self.xscale = (1.0 - 1 / self.xrange) / self.xrange 88 89 barWidth = xdelta * self.xscale * self.options.barWidthFillFraction 90 barWidthForSet = barWidth / len(stores) 91 barMargin = (xdelta * self.xscale 92 * (1.0 - self.options.barWidthFillFraction)/2) 93 94 self.minxdelta = xdelta 95 self.bars = [] 93 super(VerticalBarChart, self)._updateChart() 96 94 97 95 for i, (name, store) in enumerate(self.datasets): … … 99 97 xval, yval = item 100 98 x = (((xval - self.minxval) * self.xscale) 101 + (i * barWidthForSet) + barMargin) 102 y = 1.0 - (yval - self.minyval) * self.yscale 103 w = barWidthForSet 99 + (i * self.barWidthForSet) + self.barMargin) 100 w = self.barWidthForSet 104 101 h = (yval - self.minyval) * self.yscale 102 y = 1.0 - h 105 103 rect = Rect(x, y, w, h, xval, yval, name) 106 104 … … 122 120 def _updateChart(self): 123 121 """Evaluates measures for horizontal bars""" 124 stores = self._getDatasetsValues() 125 uniqx = uniqueIndices(stores) 126 xdelta = min([abs(uniqx[j] - uniqx[j-1]) for j in range(1, len(uniqx))]) 127 barWidth = 0 128 barWidthForSet = 0 129 barMargin = 0 130 if len(uniqx) == 1: 131 xdelta = 1.0 132 self.xscale = 1.0 133 self.minxval = uniqx[0] 134 barWidth = 1.0 * self.options.barWidthFillFraction 135 barWidthForSet = barWidth / len(stores) 136 barMargin = (1.0 - self.options.barWidthFillFraction) / 2 137 else: 138 self.xscale = (1.0 - xdelta / self.xrange) / self.xrange 139 barWidth = xdelta * self.xscale * self.options.barWidthFillFraction 140 barWidthForSet = barWidth / len(stores) 141 barMargin = xdelta * self.xscale * (1.0 - self.options.barWidthFillFraction) / 2 142 143 self.minxdelta = xdelta 144 self.bars = [] 122 super(HorizontalBarChart, self)._updateChart() 145 123 146 124 for i, (name, store) in enumerate(self.datasets): 147 125 for item in store: 148 126 xval, yval = item 149 y = ((xval - self.minxval) * self.xscale) + (i * barWidthForSet) + barMargin 127 y = (((xval - self.minxval) * self.xscale) 128 + (i * self.barWidthForSet) + self.barMargin) 150 129 x = 0.0 151 h = barWidthForSet130 h = self.barWidthForSet 152 131 w = (yval - self.minyval) * self.yscale 153 y = clamp(0.0, 1.0, y)154 132 rect = Rect(x, y, w, h, xval, yval, name) 155 133 156 if (0.0 <= rect.x <= 1.0) :134 if (0.0 <= rect.x <= 1.0) and (0.0 <= rect.y <= 1.0): 157 135 self.bars.append(rect) 158 136 -
trunk/tests/bar.py
r65 r73 46 46 surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 500, 500) 47 47 dataset = ( 48 ('dataset1', ([0, 1], [1, 1], [2, 3])),49 ('dataset2', ([0, 2], [1, 0], [3, 4])),48 ('dataset1', ([0, 3], [1, 4], [2, 2], [3,5], [4,3.5])), 49 ('dataset2', ([0, 2], [1, 3], [2, 1], [3,5], [4,2.5])), 50 50 ) 51 51 ch = pycha.bar.VerticalBarChart(surface) … … 53 53 ch._updateXY() 54 54 ch._updateChart() 55 self.assertEqual(ch.xrange, 3) 56 self.assertAlmostEqual(ch.xscale, 2 / 9.0, 4) 55 self.assertEqual(ch.xrange, 4) 56 self.assertAlmostEqual(ch.xscale, 0.2, 4) 57 self.assertAlmostEqual(ch.yscale, 0.2, 4) 57 58 self.assertEqual(ch.minxdelta, 1) 59 self.assertAlmostEqual(ch.barWidthForSet, 0.075, 4) 60 self.assertAlmostEqual(ch.barMargin, 0.025, 4) 58 61 59 62 bars = ( 60 pycha.bar.Rect(0.25/9, 3.0/4, 0.0833, 1.0/4, 0, 1, 'dataset1'), 61 pycha.bar.Rect(0.25, 3.0/4, 0.0833, 1.0/4, 1, 1, 'dataset1'), 62 pycha.bar.Rect(4.25/9, 1.0/4, 0.0833, 3.0/4, 2, 3, 'dataset1'), 63 pycha.bar.Rect(0.025, 0.4, 0.075, 0.6, 0, 3, 'dataset1'), 64 pycha.bar.Rect(0.225, 0.2, 0.075, 0.8, 1, 4, 'dataset1'), 65 pycha.bar.Rect(0.425, 0.6, 0.075, 0.4, 2, 2, 'dataset1'), 66 pycha.bar.Rect(0.625, 0.0, 0.075, 1.0, 3, 5, 'dataset1'), 67 pycha.bar.Rect(0.825, 0.3, 0.075, 0.7, 4, 3.5, 'dataset1'), 63 68 64 pycha.bar.Rect(1.0/9, 2.0/4, 0.0833, 2.0/4, 0, 2, 'dataset2'), 65 pycha.bar.Rect(3.0/9, 1, 0.0833, 0, 1, 0, 'dataset2'), 66 pycha.bar.Rect(7.0/9, 0, 0.0833, 1, 3, 4, 'dataset2'), 69 pycha.bar.Rect(0.100, 0.6, 0.075, 0.4, 0, 2, 'dataset2'), 70 pycha.bar.Rect(0.300, 0.4, 0.075, 0.6, 1, 3, 'dataset2'), 71 pycha.bar.Rect(0.500, 0.8, 0.075, 0.2, 2, 1, 'dataset2'), 72 pycha.bar.Rect(0.700, 0.0, 0.075, 1.0, 3, 5, 'dataset2'), 73 pycha.bar.Rect(0.900, 0.5, 0.075, 0.5, 4, 2.5, 'dataset2'), 67 74 ) 68 75 … … 88 95 ch._updateChart() 89 96 ch._updateTicks() 90 xticks = [( 1/9.0, 1), (3/9.0, 2), (5/9.0, 3)]97 xticks = [(0.125, 1), (0.375, 2), (0.625, 3)] 91 98 for i in range(len(xticks)): 92 99 self.assertAlmostEqual(ch.xticks[i][0], xticks[i][0], 4) … … 111 118 ch._updateChart() 112 119 self.assertEqual(ch.xrange, 3) 113 self.assertAlmostEqual(ch.xscale, 2 / 9.0, 4) 120 self.assertAlmostEqual(ch.xscale, 0.25, 4) 121 self.assertAlmostEqual(ch.yscale, 0.25, 4) 114 122 self.assertEqual(ch.minxdelta, 1) 123 self.assertAlmostEqual(ch.barWidthForSet, 0.09375, 4) 124 self.assertAlmostEqual(ch.barMargin, 0.03125, 4) 115 125 116 126 bars = ( 117 pycha.bar.Rect(0, 0. 25/9, 1.0/4, 0.0833, 0, 1, 'dataset1'),118 pycha.bar.Rect(0, 0.2 5, 1.0/4, 0.0833, 1, 1, 'dataset1'),119 pycha.bar.Rect(0, 4.25/9, 3.0/4, 0.0833, 2, 3, 'dataset1'),127 pycha.bar.Rect(0, 0.03125, 0.25, 0.09375, 0, 1, 'dataset1'), 128 pycha.bar.Rect(0, 0.28125, 0.25, 0.09375, 1, 1, 'dataset1'), 129 pycha.bar.Rect(0, 0.53125, 0.75, 0.09375, 2, 3, 'dataset1'), 120 130 121 pycha.bar.Rect(0, 1.0/9, 2.0/4, 0.0833, 0, 2, 'dataset2'),122 pycha.bar.Rect(0, 3.0/9, 0, 0.0833, 1, 0, 'dataset2'),123 pycha.bar.Rect(0, 7.0/9, 1, 0.0833, 3, 4, 'dataset2'),131 pycha.bar.Rect(0, 0.125, 0.5, 0.09375, 0, 2, 'dataset2'), 132 pycha.bar.Rect(0, 0.375, 0.0, 0.09375, 1, 0, 'dataset2'), 133 pycha.bar.Rect(0, 0.875, 1.0, 0.09375, 3, 4, 'dataset2'), 124 134 ) 125 135 … … 145 155 ch._updateChart() 146 156 ch._updateTicks() 147 yticks = [( 1/9.0, 1), (3/9.0, 2), (5/9.0, 3)]157 yticks = [(0.125, 1), (0.375, 2), (0.625, 3)] 148 158 for i in range(len(yticks)): 149 159 self.assertAlmostEqual(ch.yticks[i][0], yticks[i][0], 4)
