~alf-rodrigo/cairoplot/trunk

« back to all changes in this revision

Viewing changes to trunk/cairoplot.py

  • Committer: Rodrigo Moreira Araujo
  • Date: 2009-03-11 01:04:08 UTC
  • Revision ID: rodrigo@scrooge-20090311010408-uyxtymz04m614gor
CairoPlot.py: The series_labels parameter is now enabled for BarPlot charts. Whenever it's present, a legend box is plotted on the right upper corner.

Show diffs side-by-side

added added

removed removed

Lines of Context:
894
894
                self.value_label = self.context.text_extents(str(max(sum(serie) for serie in self.data)))[2 + self.main_dir]
895
895
            else:
896
896
                self.value_label = self.context.text_extents(str(max(max(serie) for serie in self.data)))[2 + self.main_dir]
897
 
        print self.context.text_extents(str(max(max(serie) for serie in self.data)))
898
897
        if self.labels[self.main_dir]:
899
898
            self.plot_dimensions[self.main_dir] = self.dimensions[self.main_dir] - 2*self.borders[self.main_dir] - self.value_label
900
899
        else:
912
911
        series_length = len(self.data)
913
912
        self.steps[other_dir] = float(self.plot_dimensions[other_dir])/(series_length + 0.1*(series_length + 1))
914
913
        self.space = 0.1*self.steps[other_dir]
915
 
 
916
914
        
917
915
    def render(self):
918
916
        self.calc_all_extents()
927
925
            self.render_values()
928
926
        self.render_labels()
929
927
        self.render_plot()
 
928
        if self.series_labels:
 
929
            self.render_legend()
930
930
    
931
931
    def draw_3d_rectangle_front(self, x0, y0, x1, y1, shift):
932
932
        self.context.rectangle(x0-shift, y0+shift, x1-x0, y1-y0)
977
977
            self.render_horz_labels()
978
978
        if self.labels[VERT]:
979
979
            self.render_vert_labels()
 
980
            
 
981
    def render_legend(self):
 
982
        cr = self.context
 
983
        cr.set_font_size(self.font_size)
 
984
        cr.set_line_width(self.line_width)
 
985
 
 
986
        widest_word = max(self.series_labels, key = lambda item: self.context.text_extents(item)[2])
 
987
        tallest_word = max(self.series_labels, key = lambda item: self.context.text_extents(item)[3])
 
988
        max_width = self.context.text_extents(widest_word)[2]
 
989
        max_height = self.context.text_extents(tallest_word)[3] * 1.1 + 5
 
990
        
 
991
        color_box_height = max_height / 2
 
992
        color_box_width = color_box_height * 2
 
993
        
 
994
        #Draw a bounding box
 
995
        bounding_box_width = max_width + color_box_width + 15
 
996
        bounding_box_height = (len(self.series_labels)+0.5) * max_height
 
997
        cr.set_source_rgba(1,1,1)
 
998
        cr.rectangle(self.dimensions[HORZ] - self.border - bounding_box_width, self.border,
 
999
                            bounding_box_width, bounding_box_height)
 
1000
        cr.fill()
 
1001
        
 
1002
        cr.set_source_rgba(*self.line_color)
 
1003
        cr.set_line_width(self.line_width)
 
1004
        cr.rectangle(self.dimensions[HORZ] - self.border - bounding_box_width, self.border,
 
1005
                            bounding_box_width, bounding_box_height)
 
1006
        cr.stroke()
 
1007
 
 
1008
        for idx,key in enumerate(self.series_labels):
 
1009
            #Draw color box
 
1010
            cr.set_source_rgba(*self.series_colors[idx])
 
1011
            cr.rectangle(self.dimensions[HORZ] - self.border - max_width - color_box_width - 10, 
 
1012
                                self.border + color_box_height + (idx*max_height) ,
 
1013
                                color_box_width, color_box_height)
 
1014
            cr.fill()
 
1015
            
 
1016
            cr.set_source_rgba(0, 0, 0)
 
1017
            cr.rectangle(self.dimensions[HORZ] - self.border - max_width - color_box_width - 10, 
 
1018
                                self.border + color_box_height + (idx*max_height),
 
1019
                                color_box_width, color_box_height)
 
1020
            cr.stroke()
 
1021
            
 
1022
            #Draw series labels
 
1023
            cr.set_source_rgba(0, 0, 0)
 
1024
            cr.move_to(self.dimensions[HORZ] - self.border - max_width - 5, self.border + ((idx+1)*max_height))
 
1025
            cr.show_text(key)
 
1026
 
980
1027
 
981
1028
class HorizontalBarPlot(BarPlot):
982
1029
    def __init__(self, 
986
1033
                 height = 480,
987
1034
                 background = "white light_gray",
988
1035
                 border = 0,
 
1036
                 series_labels = False,
989
1037
                 display_values = False,
990
1038
                 grid = False,
991
1039
                 rounded_corners = False,
1000
1048
        BarPlot.__init__(self, surface, data, width, height, background, border, 
1001
1049
                         display_values, grid, rounded_corners, stack, three_dimension,
1002
1050
                         x_labels, y_labels, x_bounds, y_bounds, series_colors, HORZ)
 
1051
        self.series_labels = series_labels
1003
1052
 
1004
1053
    def calc_vert_extents(self):
1005
1054
        self.calc_extents(VERT)
1039
1088
        self.context.set_source_rgba(0.8, 0.8, 0.8)
1040
1089
        if self.labels[HORZ]:
1041
1090
            self.context.set_font_size(self.font_size * 0.8)
1042
 
            step = (self.dimensions[HORZ] - 2*self.borders[HORZ])/(len(self.labels[HORZ])-1)
 
1091
            step = (self.dimensions[HORZ] - 2*self.borders[HORZ] - self.value_label)/(len(self.labels[HORZ])-1)
1043
1092
            x = self.borders[HORZ]
1044
1093
            next_x = 0
1045
1094
            for item in self.labels[HORZ]:
1153
1202
                 height = 480,
1154
1203
                 background = "white light_gray",
1155
1204
                 border = 0,
 
1205
                 series_labels = None,
1156
1206
                 display_values = False,
1157
1207
                 grid = False,
1158
1208
                 rounded_corners = False,
1167
1217
        BarPlot.__init__(self, surface, data, width, height, background, border, 
1168
1218
                         display_values, grid, rounded_corners, stack, three_dimension,
1169
1219
                         x_labels, y_labels, x_bounds, y_bounds, series_colors, VERT)
 
1220
        self.series_labels = series_labels
1170
1221
 
1171
1222
    def calc_vert_extents(self):
1172
1223
        self.calc_extents(VERT)
1248
1299
            
1249
1300
    def render_vert_labels(self):
1250
1301
        self.context.set_source_rgba(*self.label_color)
1251
 
        y = self.borders[VERT]
1252
 
        step = (self.dimensions[VERT] - 2*self.borders[VERT])/(len(self.labels[VERT]) - 1)
 
1302
        y = self.borders[VERT] + self.value_label
 
1303
        step = (self.dimensions[VERT] - 2*self.borders[VERT] - self.value_label)/(len(self.labels[VERT]) - 1)
1253
1304
        self.labels[VERT].reverse()
1254
1305
        for item in self.labels[VERT]:
1255
1306
            width, height = self.context.text_extents(item)[2:4]
1892
1943
                      height, 
1893
1944
                      background = "white light_gray", 
1894
1945
                      border = 0, 
 
1946
                      series_labels = None,
1895
1947
                      display_values = False,
1896
1948
                      grid = False,
1897
1949
                      rounded_corners = False,
1930
1982
        CairoPlot.vertical_bar_plot ('bar2', data, 400, 300, border = 20, grid = True, rounded_corners = False)
1931
1983
    '''
1932
1984
    
1933
 
    plot = VerticalBarPlot(name, data, width, height, background, border,
 
1985
    plot = VerticalBarPlot(name, data, width, height, background, border, series_labels,
1934
1986
                           display_values, grid, rounded_corners, stack, three_dimension, 
1935
1987
                           x_labels, y_labels, x_bounds, y_bounds, colors)
1936
1988
    plot.render()
1941
1993
                       width, 
1942
1994
                       height, 
1943
1995
                       background = "white light_gray", 
1944
 
                       border = 0, 
 
1996
                       border = 0,
 
1997
                       series_labels = None,
1945
1998
                       display_values = False,
1946
1999
                       grid = False,
1947
2000
                       rounded_corners = False,
1981
2034
        CairoPlot.bar_plot ('bar2', data, 400, 300, border = 20, grid = True, rounded_corners = False)
1982
2035
    '''
1983
2036
    
1984
 
    plot = HorizontalBarPlot(name, data, width, height, background, border,
 
2037
    plot = HorizontalBarPlot(name, data, width, height, background, border, series_labels,
1985
2038
                             display_values, grid, rounded_corners, stack, three_dimension, 
1986
2039
                             x_labels, y_labels, x_bounds, y_bounds, colors)
1987
2040
    plot.render()