~kolmis/cairoplot/speedup

« back to all changes in this revision

Viewing changes to trunk/cairoplot.py

  • Committer: Rodrigo Moreira Araujo
  • Date: 2009-03-10 23:04:33 UTC
  • Revision ID: rodrigo@scrooge-20090310230433-sw9e8j1unaihas3j
CairoPlot.py:
 - Color themes now work correctly for backgrounds. Besides allowing the user to change the background color using strings, it's also possible to define gradients using a string of colors like "red white" which would create a red-to-white gradient. Furthermore, background=None now implies on transparent backgrounds.

Show diffs side-by-side

added added

removed removed

Lines of Context:
43
43
          "maroon" : (0.5,0.0,0.0,1.0), "green"   : (0.0,0.5,0.0,1.0), "navy"   : (0.0,0.0,0.5,1.0),
44
44
          "yellow" : (1.0,1.0,0.0,1.0), "magenta" : (1.0,0.0,1.0,1.0), "cyan"   : (0.0,1.0,1.0,1.0),
45
45
          "orange" : (1.0,0.5,0.0,1.0), "white"   : (1.0,1.0,1.0,1.0), "black"  : (0.0,0.0,0.0,1.0),
 
46
          "gray" : (0.5,0.5,0.5,1.0), "light_gray" : (0.9,0.9,0.9,1.0),
46
47
          "transparent" : (0.0,0.0,0.0,0.0)}
47
48
 
48
49
THEMES = {"black_red"         : [(0.0,0.0,0.0,1.0), (1.0,0.0,0.0,1.0)],
214
215
 
215
216
    def set_background(self, background):
216
217
        if background is None:
217
 
            self.background = cairo.LinearGradient(self.dimensions[HORZ] / 2, 0, self.dimensions[HORZ] / 2, self.dimensions[VERT])
218
 
            self.background.add_color_stop_rgba(0,1.0,1.0,1.0,1.0)
219
 
            self.background.add_color_stop_rgba(1.0,0.9,0.9,0.9,1.0)
220
 
        elif not hasattr(background,"__iter__") and background.lower() in COLORS:
221
 
            self.background = COLORS[background]
 
218
            self.background = (0.0,0.0,0.0,0.0)
 
219
        elif type(background) in (cairo.LinearGradient, tuple):
 
220
            self.background = background
 
221
        elif not hasattr(background,"__iter__"):
 
222
            colors = background.split(" ")
 
223
            if len(colors) == 1 and colors[0] in COLORS:
 
224
                self.background = COLORS[background]
 
225
            elif len(colors) > 1:
 
226
                self.background = cairo.LinearGradient(self.dimensions[HORZ] / 2, 0, self.dimensions[HORZ] / 2, self.dimensions[VERT])
 
227
                for index,color in enumerate(colors):
 
228
                    self.background.add_color_stop_rgba(float(index)/(len(colors)-1),*COLORS[color])
222
229
        else:
223
 
            if type(background) in (cairo.LinearGradient, tuple):
224
 
                self.background = background
225
 
            else:
226
 
                raise TypeError ("Background should be either cairo.LinearGradient or a 3-tuple, not %s" % type(background))
 
230
            raise TypeError ("Background should be either cairo.LinearGradient or a 3-tuple, not %s" % type(background))
227
231
        
228
232
    def render_background(self):
229
233
        if isinstance(self.background, cairo.LinearGradient):
809
813
                 data = None,
810
814
                 width = 640,
811
815
                 height = 480,
812
 
                 background = None,
 
816
                 background = "white light_gray",
813
817
                 border = 0,
814
818
                 grid = False,
815
819
                 rounded_corners = False,
968
972
                 data = None,
969
973
                 width = 640,
970
974
                 height = 480,
971
 
                 background = None,
 
975
                 background = "white light_gray",
972
976
                 border = 0,
973
977
                 grid = False,
974
978
                 rounded_corners = False,
1113
1117
                 data = None,
1114
1118
                 width = 640,
1115
1119
                 height = 480,
1116
 
                 background = None,
 
1120
                 background = "white light_gray",
1117
1121
                 border = 0,
1118
1122
                 grid = False,
1119
1123
                 rounded_corners = False,
1271
1275
            data = None, 
1272
1276
            width = 640, 
1273
1277
            height = 480, 
1274
 
            background = None,
 
1278
            background = "white light_gray",
1275
1279
            gradient = False,
1276
1280
            shadow = False,
1277
1281
            colors = None):
1358
1362
            data = None, 
1359
1363
            width = 640, 
1360
1364
            height = 480,
1361
 
            background = None,
 
1365
            background = "white light_gray",
1362
1366
            gradient = False,
1363
1367
            shadow = False,
1364
1368
            colors = None,
1592
1596
                 errory = None,
1593
1597
                 width  = 640,
1594
1598
                 height = 480,
1595
 
                 background = None,
 
1599
                 background = "white light_gray",
1596
1600
                 border = 0,
1597
1601
                 axis = False,
1598
1602
                 dash = False,
1635
1639
                  data,
1636
1640
                  width,
1637
1641
                  height,
1638
 
                  background = None,
 
1642
                  background = "white light_gray",
1639
1643
                  border = 0,
1640
1644
                  axis = False,
1641
1645
                  dash = False,
1652
1656
    '''
1653
1657
        - Function to plot graphics using dots and lines.
1654
1658
        
1655
 
        dot_line_plot (name, data, width, height, background = None, border = 0, axis = False, grid = False, x_labels = None, y_labels = None, x_bounds = None, y_bounds = None)
 
1659
        dot_line_plot (name, data, width, height, background = "white light_gray", border = 0, axis = False, grid = False, x_labels = None, y_labels = None, x_bounds = None, y_bounds = None)
1656
1660
 
1657
1661
        - Parameters
1658
1662
 
1692
1696
                  data,
1693
1697
                  width,
1694
1698
                  height,
1695
 
                  background = None,
 
1699
                  background = "white light_gray",
1696
1700
                  border = 0,
1697
1701
                  axis = True,
1698
1702
                  dots = False,
1711
1715
    '''
1712
1716
        - Function to plot functions.
1713
1717
        
1714
 
        function_plot(name, data, width, height, background = None, border = 0, axis = True, grid = False, dots = False, x_labels = None, y_labels = None, x_bounds = None, y_bounds = None, step = 1, discrete = False)
 
1718
        function_plot(name, data, width, height, background = "white light_gray", border = 0, axis = True, grid = False, dots = False, x_labels = None, y_labels = None, x_bounds = None, y_bounds = None, step = 1, discrete = False)
1715
1719
 
1716
1720
        - Parameters
1717
1721
        
1741
1745
    plot.render()
1742
1746
    plot.commit()
1743
1747
 
1744
 
def pie_plot( name, data, width, height, background = None, gradient = False, shadow = False, colors = None ):
 
1748
def pie_plot( name, data, width, height, background = "white light_gray", gradient = False, shadow = False, colors = None ):
1745
1749
 
1746
1750
    '''
1747
1751
        - Function to plot pie graphics.
1748
1752
        
1749
 
        pie_plot(name, data, width, height, background = None, gradient = False, colors = None)
 
1753
        pie_plot(name, data, width, height, background = "white light_gray", gradient = False, colors = None)
1750
1754
 
1751
1755
        - Parameters
1752
1756
        
1769
1773
    plot.render()
1770
1774
    plot.commit()
1771
1775
 
1772
 
def donut_plot(name, data, width, height, background = None, gradient = False, shadow = False, colors = None, inner_radius = -1):
 
1776
def donut_plot(name, data, width, height, background = "white light_gray", gradient = False, shadow = False, colors = None, inner_radius = -1):
1773
1777
 
1774
1778
    '''
1775
1779
        - Function to plot donut graphics.
1776
1780
        
1777
 
        donut_plot(name, data, width, height, background = None, gradient = False, inner_radius = -1)
 
1781
        donut_plot(name, data, width, height, background = "white light_gray", gradient = False, inner_radius = -1)
1778
1782
 
1779
1783
        - Parameters
1780
1784
        
1831
1835
                      data, 
1832
1836
                      width, 
1833
1837
                      height, 
1834
 
                      background = None, 
 
1838
                      background = "white light_gray", 
1835
1839
                      border = 0, 
1836
1840
                      grid = False,
1837
1841
                      rounded_corners = False,
1880
1884
                       data, 
1881
1885
                       width, 
1882
1886
                       height, 
1883
 
                       background = None, 
 
1887
                       background = "white light_gray", 
1884
1888
                       border = 0, 
1885
1889
                       grid = False,
1886
1890
                       rounded_corners = False,