Changeset 73 for trunk/src/bar.py

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

Files:
1 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