Changeset 73

Show
Ignore:
Timestamp:
12/20/07 06:00:51 (4 years ago)
Author:
lgs
Message:

Refactor out the _updateChart method to the superclass and fix a rendering bug that caused the bars to be displayed non centered

Location:
trunk
Files:
4 modified

Legend:

Unmodified
Added
Removed
  • trunk/src/bar.py

    r65 r73  
    2424        super(BarChart, self).__init__(surface, options) 
    2525        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 = [] 
    2753 
    2854    def _renderChart(self, cx): 
     
    6591    def _updateChart(self): 
    6692        """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() 
    9694 
    9795        for i, (name, store) in enumerate(self.datasets): 
     
    9997                xval, yval = item 
    10098                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 
    104101                h = (yval - self.minyval) * self.yscale 
     102                y = 1.0 - h 
    105103                rect = Rect(x, y, w, h, xval, yval, name) 
    106104                 
     
    122120    def _updateChart(self): 
    123121        """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() 
    145123 
    146124        for i, (name, store) in enumerate(self.datasets): 
    147125            for item in store: 
    148126                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) 
    150129                x = 0.0 
    151                 h = barWidthForSet 
     130                h = self.barWidthForSet 
    152131                w = (yval - self.minyval) * self.yscale 
    153                 y = clamp(0.0, 1.0, y) 
    154132                rect = Rect(x, y, w, h, xval, yval, name) 
    155133                 
    156                 if (0.0 <= rect.x <= 1.0): 
     134                if (0.0 <= rect.x <= 1.0) and (0.0 <= rect.y <= 1.0): 
    157135                    self.bars.append(rect) 
    158136 
  • trunk/tests/bar.py

    r65 r73  
    4646        surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 500, 500) 
    4747        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])), 
    5050            ) 
    5151        ch = pycha.bar.VerticalBarChart(surface) 
     
    5353        ch._updateXY() 
    5454        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) 
    5758        self.assertEqual(ch.minxdelta, 1) 
     59        self.assertAlmostEqual(ch.barWidthForSet, 0.075, 4) 
     60        self.assertAlmostEqual(ch.barMargin, 0.025, 4) 
    5861 
    5962        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'), 
    6368 
    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'), 
    6774            ) 
    6875 
     
    8895        ch._updateChart() 
    8996        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)] 
    9198        for i in range(len(xticks)): 
    9299            self.assertAlmostEqual(ch.xticks[i][0], xticks[i][0], 4) 
     
    111118        ch._updateChart() 
    112119        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) 
    114122        self.assertEqual(ch.minxdelta, 1) 
     123        self.assertAlmostEqual(ch.barWidthForSet, 0.09375, 4) 
     124        self.assertAlmostEqual(ch.barMargin, 0.03125, 4) 
    115125 
    116126        bars = ( 
    117             pycha.bar.Rect(0, 0.25/9, 1.0/4, 0.0833, 0, 1, 'dataset1'), 
    118             pycha.bar.Rect(0, 0.25,   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'), 
    120130 
    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'), 
    124134            ) 
    125135 
     
    145155        ch._updateChart() 
    146156        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)] 
    148158        for i in range(len(yticks)): 
    149159            self.assertAlmostEqual(ch.yticks[i][0], yticks[i][0], 4)