def initializeDims(self, data, optimize_bar_area_width=True): self.bars_width = int(self.bar_area_ratio * self.width) self.bar_height = int(self.bar_height_ratio * self.bars_width) self.height = len(data) * self.bar_height self.img = Image.new('RGBA', (self.width, self.height), (255, 255, 255)) self.draw = ImageDraw.Draw(self.img) longest_label = self.get_longest_label(data) optimal_font_size = get_optimal_font_size(self.draw, self.font_type, self.width - self.bars_width, self.bar_height, longest_label, self.min_font_size) if not optimal_font_size and optimize_bar_area_width: # we did not find an optimal font size - shrink the bar area to find an acceptable font size label_width, label_height = get_width_for_font_size( self.draw, self.font_type, self.min_font_size, longest_label) new_bar_width = self.width - label_width - 3 #3 is some right border bar_width = max(0.3 * self.width, new_bar_width) self.bar_area_ratio = float(bar_width) / float(self.width) self.initializeDims(data, False) self.font_size = optimal_font_size or self.min_font_size
def drawEmptyRectangle(self, width, bar_height, height_offset, font_size, bottom_border_offset): rect_start = self.fill_value * width / 100.0 rect_width = width - rect_start empty_top_left = (rect_start + self.border + 1, self.border + height_offset) empty_bottom_right = (width - self.border, height_offset + bar_height - self.border - bottom_border_offset) self.draw.rectangle((empty_top_left, empty_bottom_right), fill=self.empty_color) rect_label = "%i%%" % (100 - self.fill_value) optimal_font_size = get_optimal_font_size( self.draw, self.font_type, rect_width, bar_height, rect_label, 7, font_size ) if not optimal_font_size: return font = ImageFont.truetype(self.font_type, optimal_font_size) label_width, label_height = self.draw.textsize(rect_label, font=font) label_margin = rect_start + (rect_width - label_width) / 2 label_offset = (bar_height - label_height) / 2 self.draw.text( (label_margin, height_offset + self.border + 1 + label_offset), rect_label, font=font, fill=self.dark_font_color, )
def initializeDims(self, data, optimize_bar_area_width = True): self.bars_width = int(self.bar_area_ratio * self.width) self.bar_height = int(self.bar_height_ratio * self.bars_width) self.height = len(data) * self.bar_height self.img = Image.new('RGBA', (self.width, self.height), (255, 255, 255)) self.draw = ImageDraw.Draw(self.img) longest_label = self.get_longest_label(data) optimal_font_size = get_optimal_font_size( self.draw, self.font_type, self.width - self.bars_width, self.bar_height, longest_label, self.min_font_size ) if not optimal_font_size and optimize_bar_area_width: # we did not find an optimal font size - shrink the bar area to find an acceptable font size label_width, label_height = get_width_for_font_size(self.draw, self.font_type, self.min_font_size, longest_label) new_bar_width = self.width - label_width - 3 #3 is some right border bar_width = max(0.3 * self.width, new_bar_width) self.bar_area_ratio = float(bar_width) / float(self.width) self.initializeDims(data, False) self.font_size = optimal_font_size or self.min_font_size
def drawFullRectangle(self, width, bar_height, height_offset, font_size, bottom_border_offset): rect_width = self.fill_value * width / 100.0 full_top_left = (self.border, self.border + height_offset) full_bottom_right = (rect_width, height_offset + bar_height - self.border - bottom_border_offset) self.draw.rectangle((full_top_left, full_bottom_right), fill=self.full_color) # Draw text rect_label = "%i%%" % self.fill_value optimal_font_size = get_optimal_font_size( self.draw, self.font_type, rect_width, bar_height, rect_label, 7, font_size ) if not optimal_font_size: return font = ImageFont.truetype(self.font_type, optimal_font_size) label_width, label_height = self.draw.textsize(rect_label, font=font) label_margin = (rect_width - label_width) / 2 label_offset = (bar_height - label_height) / 2 self.draw.text( (self.border + 1 + label_margin, height_offset + self.border + 1 + label_offset), rect_label, font=font, fill=self.light_font_color, )