Changeset 169 for trunk/pycha/chart.py

Show
Ignore:
Timestamp:
03/17/09 05:27:31 (3 years ago)
Author:
lgs
Message:

Big refactor about how the colors scheme are created and used. See #29

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/pycha/chart.py

    r149 r169  
    2121import cairo 
    2222 
    23 from pycha.color import (defaultColorscheme, getColorscheme, hex2rgb, 
    24                          DEFAULT_COLOR) 
     23from pycha.color import ColorScheme, hex2rgb, DEFAULT_COLOR 
    2524 
    2625 
     
    5756        self._initSurface(surface) 
    5857 
     58        self.colorScheme = None 
     59 
    5960    def addDataset(self, dataset): 
    6061        """Adds an object containing chart data to the storage hash""" 
     
    113114 
    114115    def _setColorscheme(self): 
    115         """Sets the colorScheme used for the chart using the color in the 
     116        """Sets the colorScheme used for the chart using the 
    116117        options.colorScheme option 
    117118        """ 
    118         scheme = self.options.colorScheme 
     119        name = self.options.colorScheme.name 
    119120        keys = self._getDatasetsKeys() 
    120         if isinstance(scheme, dict): 
    121             if not scheme: 
    122                 self.options.colorScheme = defaultColorscheme(keys) 
    123         elif isinstance(scheme, basestring): 
    124             self.options.colorScheme = getColorscheme(scheme, keys) 
    125         else: 
    126             raise TypeError("Color scheme is invalid!") 
     121        colorSchemeClass = ColorScheme.getColorScheme(name, None) 
     122        if colorSchemeClass is None: 
     123            raise ValueError('Color scheme is invalid!') 
     124 
     125        kwargs = dict(self.options.colorScheme.args) 
     126        self.colorScheme = colorSchemeClass(keys, **kwargs) 
    127127 
    128128    def _initSurface(self, surface): 
     
    552552        def drawKey(key, x, y, text_height): 
    553553            cx.rectangle(x, y, bullet, bullet) 
    554             cx.set_source_rgb(*self.options.colorScheme[key]) 
     554            cx.set_source_rgb(*self.colorScheme[key]) 
    555555            cx.fill_preserve() 
    556556            cx.set_source_rgb(0, 0, 0) 
     
    670670    barWidthFillFraction=0.75, 
    671671    pieRadius=0.4, 
    672     colorScheme=DEFAULT_COLOR, 
     672    colorScheme=Option( 
     673        name='gradient', 
     674        args=Option(initialColor=DEFAULT_COLOR), 
     675    ), 
    673676    title=None, 
    674677    titleFont='Tahoma',