=== modified file 'CairoPlot.py' --- CairoPlot.py 2008-09-16 18:42:48 +0000 +++ CairoPlot.py 2011-07-09 21:38:35 +0000 @@ -32,6 +32,7 @@ HORZ = 0 VERT = 1 +H_LABEL_MIN_EXTRA_SPACING = 300 #This is a number, specified in percent, that is used to determine the minimum distance between the horizontal labels. It only has effect when there are so many horizontal labels, that they would be drawn on top of each other if they all were to be drawn. If there are too many horizontal labels to be drawn properly, and if this number is 0, the horizontal labels are drawn right next to each other. If it is 100, the distance between each label is equal to the size of one label. If it's 200, the distance between each label is equal to the size of two label, etc. def other_direction(direction): "explicit is better than implicit" @@ -266,14 +267,24 @@ border = self.borders[HORZ] step = (self.width - 2 * border) / len(labels) + minstep = None + x_lastdraw = None x = border + y = self.height - self.borders[VERT]*0.9 #this means the labels are positioned 10% (of the distance from the X-axis to the bottom of the picture) lower that the X-axis for item in labels: - cr.set_source_rgb(*self.label_color) - width = cr.text_extents(item)[2] - cr.move_to(x, self.height - self.borders[VERT] + 10) - cr.rotate(self.h_label_angle) - cr.show_text(item) - cr.rotate(-self.h_label_angle) + + height = cr.text_extents(item)[3] + #the minimum distance we must move on the x-axis before drawing a new label, in order to avoid labels being drawn on top of each other + minstep = (1+float(H_LABEL_MIN_EXTRA_SPACING)/100) * (height / math.sin(self.h_label_angle)) + + if x_lastdraw == None or x-x_lastdraw >= minstep: + cr.set_source_rgb(*self.label_color) + cr.move_to(x, y) + cr.rotate(self.h_label_angle) + cr.show_text(item) + cr.rotate(-self.h_label_angle) + x_lastdraw = x + #FIXME: render grid in a separate method if self.grid and x != border: cr.set_source_rgb(*self.grid_color) @@ -1027,7 +1038,7 @@ If left None, a gray to white gradient will be generated; border - Distance in pixels of a square border into which the graphics will be drawn; axis - Whether or not the axis are to be drawn; - grid - Whether or not the gris is to be drawn; + grid - Whether or not the grid is to be drawn; dots - Whether or not dots should be shown at each point; h_labels, v_labels - lists of strings containing the horizontal and vertical labels for the axis; h_bounds, v_bounds - tuples containing the lower and upper value bounds for the data to be plotted.