def plot_sparkline_discrete(results, args, longlines=False): """The source data is a list of values between 0 and 100 (or 'limits' if given). Values greater than 95 (or 'upper' if given) are displayed in red, otherwise they are displayed in green""" width = int(args.get('width', '2')) height = int(args.get('height', '14')) upper = int(args.get('upper', '50')) below_color = args.get('below-color', 'gray') above_color = args.get('above-color', 'red') gap = 4 if longlines: gap = 0 im = PNGCanvas(len(results)*width-1, height) im.color = rgb.colors['white'] im.filledRectangle(0, 0, im.width-1, im.height-1) (dmin, dmax) = [int(x) for x in args.get('limits', '0,100').split(',')] if dmax < dmin: dmax = dmin zero = im.height - 1 if dmin < 0 and dmax > 0: zero = im.height - (0 - dmin) / (float(dmax - dmin + 1) / (height - gap)) for (r, i) in zip(results, range(0, len(results)*width, width)): color = (r >= upper) and above_color or below_color if r < 0: y_coord = im.height - (r - dmin) / (float(dmax - dmin + 1) / (height - gap)) else: y_coord = im.height - (r - dmin) / (float(dmax - dmin + 1) / (height - gap)) im.color = rgb.colors[color] if longlines: im.filledRectangle(i, zero, i+width-2, y_coord) else: im.rectangle(i, y_coord - gap, i+width-2, y_coord) return im.dump()
def plot_sparkline_smooth(results, args): step = int(args.get('step', '2')) height = int(args.get('height', '20')) (dmin, dmax) = [int(x) for x in args.get('limits', '0,100').split(',')] im = PNGCanvas((len(results)-1)*step+4, height) im.color = rgb.colors['white'] im.filledRectangle(0, 0, im.width-1, im.height-1) coords = zip(range(1,len(results)*step+1, step), [height - 3 - (y-dmin)/(float(dmax - dmin +1)/(height-4)) for y in results]) im.color = [128, 128, 128, 255] lastx, lasty = coords[0] for x0, y0, in coords: im.line(lastx, lasty, x0, y0) lastx, lasty = x0, y0 min_color = rgb.colors[args.get('min-color', 'green')] max_color = rgb.colors[args.get('max-color', 'red')] last_color = rgb.colors[args.get('last-color', 'blue')] has_min = args.get('min-m', 'false') has_max = args.get('max-m', 'false') has_last = args.get('last-m', 'false') if has_min == 'true': min_pt = coords[results.index(min(results))] im.color = min_color im.rectangle(min_pt[0]-1, min_pt[1]-1, min_pt[0]+1, min_pt[1]+1) if has_max == 'true': im.color = max_color max_pt = coords[results.index(max(results))] im.rectangle(max_pt[0]-1, max_pt[1]-1, max_pt[0]+1, max_pt[1]+1) if has_last == 'true': im.color = last_color end = coords[-1] im.rectangle(end[0]-1, end[1]-1, end[0]+1, end[1]+1) return im.dump()
def plot_sparkline_smooth(results, args): step = int(args.get('step', '2')) height = int(args.get('height', '20')) (dmin, dmax) = [int(x) for x in args.get('limits', '0,100').split(',')] im = PNGCanvas((len(results)-1)*step+4, height) im.color = rgb.colors('white') im.filledRectangle(0, 0, im.width-1, im.height-1) coords = zip(range(1,len(results)*step+1, step), [height - 3 - (y-dmin)/(float(dmax - dmin +1)/(height-4)) for y in results]) im.color = rgb.colors('gray53') lastx, lasty = coords[0] for x0, y0, in coords: im.line(lastx, lasty, x0, y0) lastx, lasty = x0, y0 min_color = rgb.colors(args.get('min-color', 'green')) max_color = rgb.colors(args.get('max-color', 'red')) last_color = rgb.colors(args.get('last-color', 'blue')) has_min = args.get('min-m', 'false') has_max = args.get('max-m', 'false') has_last = args.get('last-m', 'false') if has_min == 'true': min_pt = coords[results.index(min(results))] im.color = min_color im.filledRectangle(min_pt[0]-1, min_pt[1]-1, min_pt[0]+1, min_pt[1]+1) if has_max == 'true': im.color = max_color max_pt = coords[results.index(max(results))] im.filledRectangle(max_pt[0]-1, max_pt[1]-1, max_pt[0]+1, max_pt[1]+1) if has_last == 'true': im.color = last_color end = coords[-1] im.filledRectangle(end[0]-1, end[1]-1, end[0]+1, end[1]+1) return im.dump()
def make_image(self): """ Creates PNG image with QR Code """ boxsize = 10 # pixels per box offset = 4 # boxes as border pixelsize = (self.getModuleCount() + offset + offset) * boxsize canvas = PNGCanvas(pixelsize, pixelsize) for row in range(self.getModuleCount()): for column in range(self.getModuleCount()): if self.isDark(row, column): pos_x = (column + offset) * boxsize pos_y = (row + offset) * boxsize canvas.filledRectangle(pos_x, pos_y, pos_x + boxsize, pos_y + boxsize) return canvas.dump()
def make_image(self): ''' Creates PNG image with QR Code ''' boxsize = 10 # pixels per box offset = 1 # boxes as border pixelsize = (self.getModuleCount() + offset + offset) * boxsize canvas = PNGCanvas(pixelsize, pixelsize) for row in range(self.getModuleCount()): for column in range(self.getModuleCount()): if (self.isDark(row, column) ): pos_x = (column + offset) * boxsize pos_y = (row + offset) * boxsize canvas.filledRectangle(pos_x, pos_y, pos_x + boxsize, pos_y + boxsize) return canvas.dump()
def make_ranking_icon(request, id = None, icon = False): if id or icon: # make the smaller version for display (the id) or for saving to the server width = 80 height = 40 else: width = 200 height = 200 colors = ([0x25, 0x67, 0x79, 0xff], [0xaf, 0xf3, 0x9b, 0xff], [0xed, 0x1e, 0x79, 0xff], [0x29, 0xaa, 0xe2, 0xff], [0x4d, 0x42, 0x4c, 0xff]) weights = request.session.get('source_weights', DefaultWeights) sources_to_aggregate = request.session.get('sources_to_aggregate', DefaultSources) c = PNGCanvas(width,height) num_colors = len(colors) # f = open("/var/www/media/handle.png", "rb") # handle = PNGCanvas(12,12) # handle.load(f) num_sources = len(sources_to_aggregate) if num_sources == 0: return HttpResponse(mimetype='image/png', content=c.dump()) bar_width = width / num_sources half = bar_width / 2 i = 0 for source in sources_to_aggregate: c.color = DataSourceColor.objects.get(datasource=source).color_as_array() #colors[i % num_colors] bar_height = height - height * (weights[source] / 10.0) c.filledRectangle(bar_width * i, bar_height, bar_width * (i + 1) - 1, height) # handle.copyRect(0,0,11,11,10,10,c) #int(bar_height + 6), bar_width * i + half,c) i += 1 if id: f = open("/var/www/media/ranking_icons/icon%d.png" % id, "wb") f.write(c.dump()) f.close() else: return HttpResponse(mimetype='image/png', content=c.dump())
def plot_sparkline_discrete(results, args, longlines=False): """The source data is a list of values between 0 and 100 (or 'limits' if given). Values greater than 95 (or 'upper' if given) are displayed in red, otherwise they are displayed in green""" width = int(args.get('width', '2')) height = int(args.get('height', '14')) upper = int(args.get('upper', '50')) below_color = args.get('below-color', 'dark gray') above_color = args.get('above-color', 'red') gap = 4 if longlines: gap = 0 im = PNGCanvas(len(results)*width-1, height) im.color = rgb.colors('white') im.filledRectangle(0, 0, im.width-1, im.height-1) (dmin, dmax) = [int(x) for x in args.get('limits', '0,100').split(',')] if dmax < dmin: dmax = dmin zero = im.height - 1 if dmin < 0 and dmax > 0: zero = im.height - (0 - dmin) / (float(dmax - dmin + 1) / (height - gap)) for (r, i) in zip(results, range(0, len(results)*width, width)): color = (r >= upper) and above_color or below_color if r < 0: y_coord = im.height - (r - dmin) / (float(dmax - dmin + 1) / (height - gap)) else: y_coord = im.height - (r - dmin) / (float(dmax - dmin + 1) / (height - gap)) im.color = rgb.colors(color) if longlines: im.filledRectangle(i, zero, i+width-2, y_coord) else: im.filledRectangle(i, y_coord - gap, i+width-2, y_coord) return im.dump()