~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 00:29:17 UTC
  • Revision ID: rodrigo@scrooge-20090311002917-3warch0r3wek5u8l
CairoPlot.py: 
 - Value labels above the bars are now possible through the display_values attribute.

Show diffs side-by-side

added added

removed removed

Lines of Context:
815
815
                 height = 480,
816
816
                 background = "white light_gray",
817
817
                 border = 0,
 
818
                 display_values = False,
818
819
                 grid = False,
819
820
                 rounded_corners = False,
820
821
                 stack = False,
829
830
        self.bounds = {}
830
831
        self.bounds[HORZ] = x_bounds
831
832
        self.bounds[VERT] = y_bounds
 
833
        self.display_values = display_values
832
834
        self.grid = grid
833
835
        self.rounded_corners = rounded_corners
834
836
        self.stack = stack
835
837
        self.three_dimension = three_dimension
836
838
        self.x_label_angle = math.pi / 2.5
837
 
        self.max_value = {}
838
839
        self.main_dir = main_dir
 
840
        self.max_value = {}
839
841
        self.plot_dimensions = {}
840
842
        self.steps = {}
 
843
        self.value_label_color = (0.5,0.5,0.5,1.0)
841
844
 
842
845
        Plot.__init__(self, surface, data, width, height, background, border, x_labels, y_labels, series_colors)
843
846
 
885
888
        self.calc_horz_extents()
886
889
        self.calc_vert_extents()
887
890
        other_dir = other_direction(self.main_dir)
 
891
        self.value_label = 0
 
892
        if self.display_values:
 
893
            if self.stack:
 
894
                self.value_label = self.context.text_extents(str(max(sum(serie) for serie in self.data)))[2 + self.main_dir]
 
895
            else:
 
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)))
888
898
        if self.labels[self.main_dir]:
889
 
            self.plot_dimensions[self.main_dir] = self.dimensions[self.main_dir] - 2*self.borders[self.main_dir]
 
899
            self.plot_dimensions[self.main_dir] = self.dimensions[self.main_dir] - 2*self.borders[self.main_dir] - self.value_label
890
900
        else:
891
 
            self.plot_dimensions[self.main_dir] = self.dimensions[self.main_dir] - self.borders[self.main_dir] - 1.2*self.border
 
901
            self.plot_dimensions[self.main_dir] = self.dimensions[self.main_dir] - self.borders[self.main_dir] - 1.2*self.border - self.value_label
892
902
        self.plot_dimensions[other_dir] = self.dimensions[other_dir] - self.borders[other_dir] - self.border
893
903
        self.plot_top = self.dimensions[VERT] - self.borders[VERT]
894
904
 
913
923
            self.render_grid()
914
924
        if self.three_dimension:
915
925
            self.render_ground()
 
926
        if self.display_values:
 
927
            self.render_values()
916
928
        self.render_labels()
917
929
        self.render_plot()
918
930
    
934
946
        self.context.line_to(x1-shift, y0+shift)
935
947
        self.context.line_to(x0-shift, y0+shift)
936
948
        self.context.close_path()
 
949
                
 
950
    def draw_round_rectangle(self, x0, y0, x1, y1):
 
951
        self.context.arc(x0+5, y0+5, 5, -math.pi, -math.pi/2)
 
952
        self.context.line_to(x1-5, y0)
 
953
        self.context.arc(x1-5, y0+5, 5, -math.pi/2, 0)
 
954
        self.context.line_to(x1, y1-5)
 
955
        self.context.arc(x1-5, y1-5, 5, 0, math.pi/2)
 
956
        self.context.line_to(x0+5, y1)
 
957
        self.context.arc(x0+5, y1-5, 5, math.pi/2, math.pi)
 
958
        self.context.line_to(x0, y0+5)
 
959
        self.context.close_path()
937
960
 
938
961
    def render_ground(self):
939
962
        self.draw_3d_rectangle_front(self.borders[HORZ], self.dimensions[VERT] - self.borders[VERT], 
954
977
            self.render_horz_labels()
955
978
        if self.labels[VERT]:
956
979
            self.render_vert_labels()
957
 
        
958
 
    def draw_round_rectangle(self, x0, y0, x1, y1):
959
 
        self.context.arc(x0+5, y0+5, 5, -math.pi, -math.pi/2)
960
 
        self.context.line_to(x1-5, y0)
961
 
        self.context.arc(x1-5, y0+5, 5, -math.pi/2, 0)
962
 
        self.context.line_to(x1, y1-5)
963
 
        self.context.arc(x1-5, y1-5, 5, 0, math.pi/2)
964
 
        self.context.line_to(x0+5, y1)
965
 
        self.context.arc(x0+5, y1-5, 5, math.pi/2, math.pi)
966
 
        self.context.line_to(x0, y0+5)
967
 
        self.context.close_path()
968
980
 
969
981
class HorizontalBarPlot(BarPlot):
970
982
    def __init__(self, 
974
986
                 height = 480,
975
987
                 background = "white light_gray",
976
988
                 border = 0,
 
989
                 display_values = False,
977
990
                 grid = False,
978
991
                 rounded_corners = False,
979
992
                 stack = False,
985
998
                 series_colors = None):
986
999
 
987
1000
        BarPlot.__init__(self, surface, data, width, height, background, border, 
988
 
                         grid, rounded_corners, stack, three_dimension,
 
1001
                         display_values, grid, rounded_corners, stack, three_dimension,
989
1002
                         x_labels, y_labels, x_bounds, y_bounds, series_colors, HORZ)
990
1003
 
991
1004
    def calc_vert_extents(self):
1074
1087
            y += step + self.space
1075
1088
        self.labels[VERT].reverse()
1076
1089
 
 
1090
    def render_values(self):
 
1091
        self.context.set_source_rgba(*self.value_label_color)
 
1092
        self.context.set_font_size(self.font_size * 0.8)
 
1093
        if self.stack:
 
1094
            for i,series in enumerate(self.data):
 
1095
                value = sum(series)
 
1096
                height = self.context.text_extents(str(value))[3]
 
1097
                x = self.borders[HORZ] + value*self.steps[HORZ] + 2
 
1098
                y = self.borders[VERT] + (i+0.5)*self.steps[VERT] + (i+1)*self.space + height/2
 
1099
                self.context.move_to(x, y)
 
1100
                self.context.show_text(str(value))
 
1101
        else:
 
1102
            for i,series in enumerate(self.data):
 
1103
                inner_step = self.steps[VERT]/len(series)
 
1104
                y0 = self.border + i*self.steps[VERT] + (i+1)*self.space
 
1105
                for number,key in enumerate(series):
 
1106
                    height = self.context.text_extents(str(key))[3]
 
1107
                    self.context.move_to(self.borders[HORZ] + key*self.steps[HORZ] + 2, y0 + 0.5*inner_step + height/2, )
 
1108
                    self.context.show_text(str(key))
 
1109
                    y0 += inner_step
 
1110
 
1077
1111
    def render_plot(self):
1078
1112
        if self.stack:
1079
1113
            for i,series in enumerate(self.data):
1119
1153
                 height = 480,
1120
1154
                 background = "white light_gray",
1121
1155
                 border = 0,
 
1156
                 display_values = False,
1122
1157
                 grid = False,
1123
1158
                 rounded_corners = False,
1124
1159
                 stack = False,
1130
1165
                 series_colors = None):
1131
1166
 
1132
1167
        BarPlot.__init__(self, surface, data, width, height, background, border, 
1133
 
                         grid, rounded_corners, stack, three_dimension,
 
1168
                         display_values, grid, rounded_corners, stack, three_dimension,
1134
1169
                         x_labels, y_labels, x_bounds, y_bounds, series_colors, VERT)
1135
1170
 
1136
1171
    def calc_vert_extents(self):
1172
1207
        if self.labels[VERT]:
1173
1208
            lines = len(self.labels[VERT])
1174
1209
            vertical_step = float(self.plot_dimensions[self.main_dir])/(lines-1)
1175
 
            y = self.borders[VERT]
 
1210
            y = self.borders[VERT] + self.value_label
1176
1211
        else:
1177
1212
            lines = 11
1178
1213
            vertical_step = float(self.plot_dimensions[self.main_dir])/(lines-1)
1179
 
            y = 1.2*self.border
 
1214
            y = 1.2*self.border + self.value_label
1180
1215
        for x in xrange(0, lines):
1181
1216
            self.context.move_to(self.borders[HORZ], y)
1182
1217
            self.context.line_to(self.dimensions[HORZ] - self.border, y)
1212
1247
            x += step + self.space
1213
1248
            
1214
1249
    def render_vert_labels(self):
 
1250
        self.context.set_source_rgba(*self.label_color)
1215
1251
        y = self.borders[VERT]
1216
1252
        step = (self.dimensions[VERT] - 2*self.borders[VERT])/(len(self.labels[VERT]) - 1)
1217
 
 
1218
1253
        self.labels[VERT].reverse()
1219
1254
        for item in self.labels[VERT]:
1220
 
            self.context.set_source_rgba(*self.label_color)
1221
1255
            width, height = self.context.text_extents(item)[2:4]
1222
1256
            self.context.move_to(self.borders[HORZ] - width - 5, y + height/2)
1223
1257
            self.context.show_text(item)
1224
1258
            y += step
1225
1259
        self.labels[VERT].reverse()
1226
1260
 
 
1261
    def render_values(self):
 
1262
        self.context.set_source_rgba(*self.value_label_color)
 
1263
        self.context.set_font_size(self.font_size * 0.8)
 
1264
        if self.stack:
 
1265
            for i,series in enumerate(self.data):
 
1266
                value = sum(series)
 
1267
                width = self.context.text_extents(str(value))[2]
 
1268
                x = self.borders[HORZ] + (i+0.5)*self.steps[HORZ] + (i+1)*self.space - width/2
 
1269
                y = value*self.steps[VERT] + 2
 
1270
                self.context.move_to(x, self.plot_top-y)
 
1271
                self.context.show_text(str(value))
 
1272
        else:
 
1273
            for i,series in enumerate(self.data):
 
1274
                inner_step = self.steps[HORZ]/len(series)
 
1275
                x0 = self.borders[HORZ] + i*self.steps[HORZ] + (i+1)*self.space
 
1276
                for number,key in enumerate(series):
 
1277
                    width = self.context.text_extents(str(key))[2]
 
1278
                    self.context.move_to(x0 + 0.5*inner_step - width/2, self.plot_top - key*self.steps[VERT] - 2)
 
1279
                    self.context.show_text(str(key))
 
1280
                    x0 += inner_step
 
1281
 
1227
1282
    def render_plot(self):
1228
1283
        if self.stack:
1229
1284
            for i,series in enumerate(self.data):
1837
1892
                      height, 
1838
1893
                      background = "white light_gray", 
1839
1894
                      border = 0, 
 
1895
                      display_values = False,
1840
1896
                      grid = False,
1841
1897
                      rounded_corners = False,
1842
1898
                      stack = False,
1875
1931
    '''
1876
1932
    
1877
1933
    plot = VerticalBarPlot(name, data, width, height, background, border,
1878
 
                           grid, rounded_corners, stack, three_dimension, 
 
1934
                           display_values, grid, rounded_corners, stack, three_dimension, 
1879
1935
                           x_labels, y_labels, x_bounds, y_bounds, colors)
1880
1936
    plot.render()
1881
1937
    plot.commit()
1886
1942
                       height, 
1887
1943
                       background = "white light_gray", 
1888
1944
                       border = 0, 
 
1945
                       display_values = False,
1889
1946
                       grid = False,
1890
1947
                       rounded_corners = False,
1891
1948
                       stack = False,
1925
1982
    '''
1926
1983
    
1927
1984
    plot = HorizontalBarPlot(name, data, width, height, background, border,
1928
 
                             grid, rounded_corners, stack, three_dimension, 
 
1985
                             display_values, grid, rounded_corners, stack, three_dimension, 
1929
1986
                             x_labels, y_labels, x_bounds, y_bounds, colors)
1930
1987
    plot.render()
1931
1988
    plot.commit()