Changeset 176

Show
Ignore:
Timestamp:
03/22/09 03:38:17 (3 years ago)
Author:
lgs
Message:

Fix in the computation of the bar width when there is just one data point and a custom range. Fixes #20

Location:
trunk
Files:
3 modified

Legend:

Unmodified
Added
Removed
  • trunk/pycha/bar.py

    r169 r176  
    4040        uniqx = uniqueIndices(stores) 
    4141 
    42         barWidth = 0 
    4342        if len(uniqx) == 1: 
    44             xdelta = 1.0 
    45             barWidth = 1.0 * self.options.barWidthFillFraction 
    46             self.barWidthForSet = barWidth / len(stores) 
    47             self.barMargin = (1.0 - self.options.barWidthFillFraction) / 2 
     43            self.minxdelta = 1.0 
    4844        else: 
    49             xdelta = min([abs(uniqx[j] - uniqx[j-1]) 
    50                           for j in range(1, len(uniqx))]) 
    51             barWidth = xdelta * self.xscale * self.options.barWidthFillFraction 
    52             self.barWidthForSet = barWidth / len(stores) 
    53             self.barMargin = (xdelta * self.xscale 
    54                               * (1.0 - self.options.barWidthFillFraction) / 2) 
    55  
    56         self.minxdelta = xdelta 
     45            self.minxdelta = min([abs(uniqx[j] - uniqx[j-1]) 
     46                                  for j in range(1, len(uniqx))]) 
     47 
     48        k = self.minxdelta * self.xscale 
     49        barWidth = k * self.options.barWidthFillFraction 
     50        self.barWidthForSet = barWidth / len(stores) 
     51        self.barMargin = k * (1.0 - self.options.barWidthFillFraction) / 2 
     52 
    5753        self.bars = [] 
    5854 
  • trunk/pycha/stackedbar.py

    r164 r176  
    5353        uniqx = uniqueIndices(stores) 
    5454 
    55         barWidth = 0 
    5655        if len(uniqx) == 1: 
    57             xdelta = 1.0 
    58             self.barWidth = 1.0 * self.options.barWidthFillFraction 
    59             self.barMargin = (1.0 - self.options.barWidthFillFraction) / 2 
     56            self.minxdelta = 1.0 
    6057        else: 
    61             xdelta = min([abs(uniqx[j] - uniqx[j-1]) 
    62                           for j in range(1, len(uniqx))]) 
    63             self.barWidth = (xdelta * self.xscale 
    64                              * self.options.barWidthFillFraction) 
    65             self.barMargin = (xdelta * self.xscale 
    66                               * (1.0 - self.options.barWidthFillFraction) / 2) 
     58            self.minxdelta = min([abs(uniqx[j] - uniqx[j-1]) 
     59                                  for j in range(1, len(uniqx))]) 
    6760 
    68         self.minxdelta = xdelta 
     61        k = self.minxdelta * self.xscale 
     62        self.barWidth = k * self.options.barWidthFillFraction 
     63        self.barMargin = k * (1.0 - self.options.barWidthFillFraction) / 2 
     64 
    6965        self.bars = [] 
    7066 
  • trunk/tests/bar.py

    r112 r176  
    1 # Copyright (c) 2007-2008 by Lorenzo Gil Sanchez <lorenzo.gil.sanchez@gmail.com> 
     1# Copyright(c) 2007-2009 by Lorenzo Gil Sanchez <lorenzo.gil.sanchez@gmail.com> 
    22# 
    33# This file is part of PyCha. 
     
    2222import pycha.bar 
    2323 
     24 
    2425class RectTests(unittest.TestCase): 
    2526 
     
    3435        self.assertEqual(r.name, 'test') 
    3536 
     37 
    3638class BarTests(unittest.TestCase): 
    3739 
     
    4547        # An evil dataset with just one point. See bug #9 
    4648        dataset = ( 
    47             ('dataset1', ([0, 0],)), 
     49            ('dataset1', ([0, 0], )), 
    4850        ) 
    4951        ch = pycha.bar.BarChart(surface) 
     
    5456        self.assertEqual(ch.xscale, 1.0) 
    5557        self.assertEqual(ch.minxval, 0) 
     58        self.assertEqual(ch.minxdelta, 1.0) 
    5659        self.assertAlmostEqual(ch.barWidthForSet, 0.75, 4) 
    5760        self.assertAlmostEqual(ch.barMargin, 0.125, 4) 
     61 
     62    def test_customRangeWithOnePoint(self): 
     63        """Weird results with a custom range and just one point. See bug #20""" 
     64        surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 500, 500) 
     65 
     66        dataset = ( 
     67            ('dataset1', ([0, 1], )), 
     68        ) 
     69        options = { 
     70            'axis': { 
     71                'x': { 
     72                    'range': (0.0, 4.0), 
     73                    }, 
     74                }, 
     75            } 
     76        ch = pycha.bar.BarChart(surface, options) 
     77        ch.addDataset(dataset) 
     78        ch._updateXY() 
     79        ch._updateChart() 
     80 
     81        self.assertEqual(ch.xscale, 0.2) 
     82        self.assertEqual(ch.minxval, 0) 
     83        self.assertEqual(ch.minxdelta, 1.0) 
     84        self.assertAlmostEqual(ch.barWidthForSet, 0.15, 2) 
     85        self.assertAlmostEqual(ch.barMargin, 0.025, 3) 
     86 
    5887 
    5988class VerticalBarTests(unittest.TestCase): 
     
    132161            R(0.03125, 0.625, 0.1875, 0.375, 0, -3, 'dataset1'), 
    133162            R(0.28125, 0.625, 0.1875, 0.125, 1, -1, 'dataset1'), 
    134             R(0.53125, 0.250, 0.1875, 0.375, 2,  3, 'dataset1'), 
    135             R(0.78125, 0.000, 0.1875, 0.625, 3,  5, 'dataset1'), 
     163            R(0.53125, 0.250, 0.1875, 0.375, 2, 3, 'dataset1'), 
     164            R(0.78125, 0.000, 0.1875, 0.625, 3, 5, 'dataset1'), 
    136165            ) 
    137166 
     
    165194            (1.0, 0.0), (0.9, 0.4), (0.8, 0.8), (0.7, 1.2), (0.6, 1.6), 
    166195            (0.5, 2.0), (0.4, 2.4), (0.3, 2.8), (0.2, 3.2), (0.1, 3.6), 
    167             (0.0, 4.0) 
     196            (0.0, 4.0), 
    168197            ] 
    169198        for i in range(len(yticks)): 
     
    189218            (1.0, -2.0), (0.9, -1.5), (0.8, -1.0), (0.7, -0.5), (0.6, 0.0), 
    190219            (0.5, 0.5), (0.4, 1.0), (0.3, 1.5), (0.2, 2.0), (0.1, 2.5), 
    191             (0.0, 3.0) 
     220            (0.0, 3.0), 
    192221            ] 
    193222        for i in range(len(yticks)): 
     
    199228        shadow = ch._getShadowRectangle(10, 20, 400, 300) 
    200229        self.assertEqual(shadow, (8, 18, 404, 302)) 
     230 
    201231 
    202232class HorizontalBarTests(unittest.TestCase): 
     
    295325            (0.0, 0.0), (0.1, 0.4), (0.2, 0.8), (0.3, 1.2), (0.4, 1.6), 
    296326            (0.5, 2.0), (0.6, 2.4), (0.7, 2.8), (0.8, 3.2), (0.9, 3.6), 
    297             (1.0, 4.0) 
     327            (1.0, 4.0), 
    298328            ] 
    299329        for i in range(len(xticks)): 
     
    319349            (0.0, -2.0), (0.1, -1.5), (0.2, -1.0), (0.3, -0.5), (0.4, 0.0), 
    320350            (0.5, 0.5), (0.6, 1.0), (0.7, 1.5), (0.8, 2.0), (0.9, 2.5), 
    321             (1.0, 3.0) 
     351            (1.0, 3.0), 
    322352            ] 
    323353        for i in range(len(xticks)): 
     
    334364        shadow = ch._getShadowRectangle(10, 20, 400, 300) 
    335365        self.assertEqual(shadow, (10, 18, 402, 304)) 
     366 
    336367 
    337368def test_suite(): 
     
    345376if __name__ == '__main__': 
    346377    unittest.main(defaultTest='test_suite') 
    347