def get_chart_image(self, data, **kw): """Return a image file path Arguments:: data -- a list containing the X,Y data representation """ from pygooglechart import SimpleLineChart, Axis # Set the vertical range from 0 to 100 try: max_y = max(data['y']) min_y = min(data['y']) except: min_y = 0 max_y = 100 width = int(kw.get('width', 600)) height = int(kw.get('height', 250)) # Chart size of widthxheight pixels and specifying the range for the Y axis chart = SimpleLineChart(width, height, y_range=[0, max_y]) # Add the chart data chart.add_data(data['y']) # Set the line colour to blue chart.set_colours(['0000FF']) try: step_x = int(100/(len(data['x'])-1)) except: step_x = 0 chart.set_grid(step_x, 10, 5, 5) # The Y axis labels contains min_y to max_y spling it into 10 equal parts, #but remove the first number because it's obvious and gets in the way #of the first X label. left_axis = [utils.intcomma(x) for x in range(0, max_y + 1, (max_y)/10)] left_axis[0] = '' chart.set_axis_labels(Axis.LEFT, left_axis) # X axis labels chart.set_axis_labels(Axis.BOTTOM, data['x']) #Generate an hash from arguments kw_hash = hash(tuple(sorted(kw.items()))) data_hash = hash(tuple(sorted([(k, tuple(v)) for k, v in data.iteritems()]))) args_hash = str(kw_hash) + str(data_hash) image_path = os.path.join(TARGET_DIR, "%s.png" % args_hash) if bool(kw.get('refresh', False)) or args_hash not in self.charts: #Get image from google chart api chart.download(image_path) if args_hash not in self.charts: self.charts.append(args_hash) self._p_changed = True return image_path
def get_grouped_bar_chart_image(self, data, **kw): """ Return image path of downloaded image from google charts api""" if len(data['y']) == 0: return '' from pygooglechart import GroupedHorizontalBarChart, Axis width = int(kw.get('width', 400)) height = int(kw.get('height', 250)) # How many members do we have in a group? members_cnt = len(data['y']) max_y = max(max(data['y'])) # since data['y'] is 2D chart = GroupedHorizontalBarChart(width, height, x_range=[0, max_y], y_range=[0, len(data['x'])]) # currently only 2 max members supported in a group colours = ['4D89D9', 'C6D9FD'] for member in data['y']: chart.add_data(member) chart.set_colours(colours[:members_cnt]) chart.set_axis_labels(Axis.LEFT, data['x']) bottom_labels = [ utils.intcomma(x) for x in range(0, max_y + 1, (max_y) / 5) ] chart.set_axis_labels(Axis.BOTTOM, bottom_labels) if kw.has_key('bar_spacing'): chart.set_bar_spacing(kw['bar_spacing']) if kw.has_key('group_spacing'): chart.set_group_spacing(kw['group_spacing']) if kw.has_key('bar_width'): chart.set_bar_width(kw['bar_width']) #Generate an hash from arguments kw_hash = hash(tuple(sorted(kw.items()))) for i in range(members_cnt): data['y'][i] = tuple(data['y'][i]) data_hash = hash( tuple(sorted([(k, tuple(v)) for k, v in data.iteritems()]))) args_hash = str(kw_hash) + str(data_hash) image_path = os.path.join(TARGET_DIR, "%s.png" % args_hash) if bool(kw.get('refresh', False)) or args_hash not in self.charts: #Get image from google chart api chart.download(image_path) if args_hash not in self.charts: self.charts.append(args_hash) self._p_changed = True return image_path
def get_grouped_bar_chart_image(self, data, **kw): """ Return image path of downloaded image from google charts api""" if len(data['y']) == 0: return '' from pygooglechart import GroupedHorizontalBarChart, Axis width = int(kw.get('width', 400)) height = int(kw.get('height', 250)) # How many members do we have in a group? members_cnt = len(data['y']) max_y = max(max(data['y'])) # since data['y'] is 2D chart = GroupedHorizontalBarChart(width, height, x_range=[0, max_y], y_range=[0, len(data['x'])]) # currently only 2 max members supported in a group colours = ['4D89D9', 'C6D9FD'] for member in data['y']: chart.add_data(member) chart.set_colours(colours[:members_cnt]) chart.set_axis_labels(Axis.LEFT, data['x']) bottom_labels = [utils.intcomma(x) for x in range(0, max_y + 1, (max_y)/5)] chart.set_axis_labels(Axis.BOTTOM, bottom_labels) if kw.has_key('bar_spacing'): chart.set_bar_spacing(kw['bar_spacing']) if kw.has_key('group_spacing'): chart.set_group_spacing(kw['group_spacing']) if kw.has_key('bar_width'): chart.set_bar_width(kw['bar_width']) #Generate an hash from arguments kw_hash = hash(tuple(sorted(kw.items()))) for i in range(members_cnt): data['y'][i] = tuple(data['y'][i]) data_hash = hash(tuple(sorted([(k, tuple(v)) for k, v in data.iteritems()]))) args_hash = str(kw_hash) + str(data_hash) image_path = os.path.join(TARGET_DIR, "%s.png" % args_hash) if bool(kw.get('refresh', False)) or args_hash not in self.charts: #Get image from google chart api chart.download(image_path) if args_hash not in self.charts: self.charts.append(args_hash) self._p_changed = True return image_path
def get_bar_chart_image(self, data, **kw): """ Return image path of downloaded image from google charts api""" if len(data['y']) == 0: return '' from pygooglechart import StackedHorizontalBarChart, Axis width = int(kw.get('width', 400)) height = int(kw.get('height', 250)) max_y = max(data['y']) chart = StackedHorizontalBarChart(width, height, x_range=[0, max_y], y_range=[0, len(data['x'])]) chart.add_data(data['y']) chart.set_colours(['76A4FB']) chart.set_axis_labels(Axis.LEFT, data['x']) bottom_labels = [ utils.intcomma(x) for x in range(0, max_y + 1, (max_y) / 5) ] chart.set_axis_labels(Axis.BOTTOM, bottom_labels) #Generate an hash from arguments kw_hash = hash(tuple(sorted(kw.items()))) data_hash = hash( tuple(sorted([(k, tuple(v)) for k, v in data.iteritems()]))) args_hash = str(kw_hash) + str(data_hash) image_path = os.path.join(TARGET_DIR, "%s.png" % args_hash) if bool(kw.get('refresh', False)) or args_hash not in self.charts: #Get image from google chart api chart.download(image_path) if args_hash not in self.charts: self.charts.append(args_hash) self._p_changed = True return image_path
def get_bar_chart_image(self, data, **kw): """ Return image path of downloaded image from google charts api""" if len(data['y']) == 0: return '' from pygooglechart import StackedHorizontalBarChart, Axis width = int(kw.get('width', 400)) height = int(kw.get('height', 250)) max_y = max(data['y']) chart = StackedHorizontalBarChart(width, height, x_range=[0, max_y], y_range=[0, len(data['x'])]) chart.add_data(data['y']) chart.set_colours(['76A4FB']) chart.set_axis_labels(Axis.LEFT, data['x']) bottom_labels = [utils.intcomma(x) for x in range(0, max_y + 1, (max_y)/5)] chart.set_axis_labels(Axis.BOTTOM, bottom_labels) #Generate an hash from arguments kw_hash = hash(tuple(sorted(kw.items()))) data_hash = hash(tuple(sorted([(k, tuple(v)) for k, v in data.iteritems()]))) args_hash = str(kw_hash) + str(data_hash) image_path = os.path.join(TARGET_DIR, "%s.png" % args_hash) if bool(kw.get('refresh', False)) or args_hash not in self.charts: #Get image from google chart api chart.download(image_path) if args_hash not in self.charts: self.charts.append(args_hash) self._p_changed = True return image_path
def get_chart_image(self, data, **kw): """Return a image file path Arguments:: data -- a list containing the X,Y data representation """ from pygooglechart import SimpleLineChart, Axis # Set the vertical range from 0 to 100 try: max_y = max(data['y']) min_y = min(data['y']) except: min_y = 0 max_y = 100 width = int(kw.get('width', 600)) height = int(kw.get('height', 250)) # Chart size of widthxheight pixels and specifying the range for the Y axis chart = SimpleLineChart(width, height, y_range=[0, max_y]) # Add the chart data chart.add_data(data['y']) # Set the line colour to blue chart.set_colours(['0000FF']) try: step_x = int(100 / (len(data['x']) - 1)) except: step_x = 0 chart.set_grid(step_x, 10, 5, 5) # The Y axis labels contains min_y to max_y spling it into 10 equal parts, #but remove the first number because it's obvious and gets in the way #of the first X label. left_axis = [ utils.intcomma(x) for x in range(0, max_y + 1, (max_y) / 10) ] left_axis[0] = '' chart.set_axis_labels(Axis.LEFT, left_axis) # X axis labels chart.set_axis_labels(Axis.BOTTOM, data['x']) #Generate an hash from arguments kw_hash = hash(tuple(sorted(kw.items()))) data_hash = hash( tuple(sorted([(k, tuple(v)) for k, v in data.iteritems()]))) args_hash = str(kw_hash) + str(data_hash) image_path = os.path.join(TARGET_DIR, "%s.png" % args_hash) if bool(kw.get('refresh', False)) or args_hash not in self.charts: #Get image from google chart api chart.download(image_path) if args_hash not in self.charts: self.charts.append(args_hash) self._p_changed = True return image_path