def detailchart(self, chartable): theme.reinitialize() min_y = 0 max_y = 0 capture = chartable.get('capture') for sortname, sortfn in self.sorts: data = capture[sortname] m = median(data) if m > max_y: max_y = m max_x = max(self.limits) min_x = min(self.limits) ipoints = 10.0 x_interval = (max_x - min_x) / ipoints y_interval = (max_y - min_y) / ipoints xaxis = axis.X(label='Limit', tic_interval = x_interval, format='/4{}%d') yaxis = axis.Y(label='Seconds', tic_interval = y_interval, format='/4{}%0.3f') ar = area.T( x_range = (min_x, max_x), y_range = (min_y, max_y), x_axis = xaxis, y_axis = yaxis, legend = legend.T(), ) tb = text_box.T(loc=(140,90), text='Rlen\n%s' % chartable['rlen']) for sortname, sortfn in self.sorts: data = capture[sortname] linedata = [ (self.limits[x], data[x]) for x in range(len(data)) ] ar.add_plot( line_plot.T(label="%s" % sortname, data=linedata) ) fd = open('detail-%s-%s.pdf' % (self.dbkey, chartable['rlen']), 'w') can = canvas.init(fd, 'pdf') ar.draw(can) tb.draw(can) can.close()
def set_Y(self, label, **kwargs): ''' set Y @param label: Y label @param label_offset: The location for drawing the axis label, relative to the middle point of the axis. If the value is None, the label is displayed below (or to the left of) of axis at the middle. @tic_len:The length of tick lines. The value can be negative, in which case the tick lines are drawn right of (or above) the axis. @param format: The format string for tick labels @param offset: The location of the axis. The value of 0 draws the axis at the left (for the Y axis) or bottom (for the X axis) edge of the drawing area. @param tic_interval: ''' #return axis.Y(label=label, label_offset=label_offset, format=format, # tic_len=tic_len, offset=offset, tic_interval=tic_len/6) return axis.Y(label=label, format="%.1e", **kwargs)
def create_graph(label_x, label_y, data_x, alldata_y, filename, title, start_date, end_date, start_y, end_y): """ main func """ # alter file name (linpng do not seems to like spaces in filenames filename = filename.replace(' ', '_') # Graph style theme.get_options() theme.use_color = True theme.default_font_size = 12 theme.default_font_family = "AvantGarde-Book" theme.reinitialize() colors = [ color.blue, color.red, color.green, color.magenta, color.cyan1, color.orange, ] can = canvas.init("%s" % filename) # Draw graph title newtitle = "/hL/20%s" % title left = WIDTH / 2 - font.text_width(newtitle) / 2 can.show(left, HEIGHT + DELTA, newtitle) int_to_date = lambda x: '/a60{}' + time.strftime("%H:%M", time.localtime(x) ) xaxis = axis.X(format=int_to_date, label="/20%s" % label_x, label_offset=(0, -DELTA), minor_tic_interval=X_MINOR_TICK_INTERVAL, tic_interval=X_TICK_INTERVAL) yaxis = axis.Y( label="/20%s" % label_y, label_offset=(-DELTA, 0), minor_tic_interval=(end_y - start_y) / 20, tic_interval=(end_y - start_y) / 5, ) ar = area.T(size=(WIDTH, HEIGHT), x_axis=xaxis, y_axis=yaxis, x_grid_style=line_style.gray70_dash3, x_range=(start_date, end_date), y_range=(start_y, end_y), x_grid_interval=X_GRID_INTERVAL, y_grid_interval=(end_y - start_y) / 5) i = 0 # Draw a line for each columns for title, data_y in alldata_y.iteritems(): plot = line_plot.T(label=title, data=zip(data_x, data_y), line_style=line_style.T(color=colors[i], width=1)) ar.add_plot(plot) i += 1 ar.draw() can.close() return True
def comparisonchart(self, sortname1, sortname2): linedata = [] test_total = 0 test_wrong = 0 for rlendata in self.main: rlen = rlendata['rlen'] capture = rlendata['capture'] values1 = capture[sortname1] values2 = capture[sortname2] doc_ratio = rlen / float(self.numdocs) cutoff = None wins = [] #test = sortname1 == 'fwscan' and sortname2 in ('nbest', 'timsort') #test_fn = fwscan_wins test = sortname1 == 'nbest' and sortname2 == 'timsort' test_fn = nbest_ascending_wins for x in xrange(0, min(len(values1), len(values2))): t1 = values1[x] t2 = values2[x] limit = self.limits[x] limitratio = limit / float(self.numdocs) won = t1 < t2 if won: wins.append(limit) wrongmsg = "wrong %s? rlen %s, limit %s (%0.5f > %0.5f)%s" if test: test_total += 1 curvewin = test_fn(limit, rlen, self.numdocs) if won and (not curvewin): extra = '' if (t1 / t2) < .90: # more than 10% difference extra = " * (%0.2f)" % (t1 / t2) print wrongmsg % ('curvelose', rlen, limit, t2, t1, extra) test_wrong += 1 elif (not won) and curvewin: extra = '' if (t2 / t1) < .90: # more than 10% difference extra = " * (%0.2f)" % (t2 / t1) print wrongmsg % ('curvewin', rlen, limit, t1, t2, extra) test_wrong += 1 for limit in wins: limitratio = limit / float(self.numdocs) linedata.append((doc_ratio, limitratio)) if test: if test_total: test_right = test_total - test_wrong test_percent = test_right / float(test_total) print "test percentage %0.2f: (%s wrong out of %s)" % ( test_percent, test_wrong, test_total) comparename = 'compare-%s-%s-beats-%s' % (self.dbkey, sortname1, sortname2) xaxis = axis.X(label='Doc Ratio (rlen//numdocs)', tic_interval=.1, format='/4{}%0.2f') yaxis = axis.Y(label='Limit Ratio (limit//numdocs)', tic_interval=.1, format='/4{}%0.2f') ar = area.T( x_range=(0, 1), y_range=(0, 1), x_axis=xaxis, y_axis=yaxis, legend=legend.T(), ) ar.add_plot( line_plot.T(label="%s \nbeats \n%s" % (sortname1, sortname2), data=linedata), ) tb = text_box.T(loc=(140, 90), text='Numdocs\n%s' % self.numdocs) fd = open('%s.pdf' % comparename, 'w') can = canvas.init(fd, 'pdf') ar.draw(can) tb.draw(can) can.close()
def _create_bars(self, cr, uid, ids, report, fields, results, context): env = openerp.api.Environment(cr, uid, context or {}) pdf_string = cStringIO.StringIO() can = canvas.init(fname=pdf_string, format='pdf') can.show(80, 380, '/16/H' + report['title']) process_date = { 'D': lambda x: reduce(lambda xx, yy: xx + '-' + yy, x.split('-')[1:3]), 'M': lambda x: x.split('-')[1], 'Y': lambda x: x.split('-')[0] } ar = area.T(size=(350, 350), x_axis=axis.X(label=fields[0]['name'], format="/a-30{}%s"), y_axis=axis.Y( label=', '.join(map(lambda x: x['name'], fields[1:])))) idx = 0 date_idx = None fct = {} for f in fields: field_id = (f['field_child3'] and f['field_child3'][0]) or ( f['field_child2'] and f['field_child2'][0]) or ( f['field_child1'] and f['field_child1'][0]) or (f['field_child0'] and f['field_child0'][0]) if field_id: ttype = env['ir.model.fields'].browse(field_id).ttype if ttype == 'date': date_idx = idx fct[idx] = process_date[report['frequency']] else: fct[idx] = lambda x: x else: fct[idx] = lambda x: x idx += 1 # plot are usually displayed year by year # so we do so if the first field is a date data_by_year = {} if date_idx is not None: for r in results: key = process_date['Y'](r[date_idx]) if key not in data_by_year: data_by_year[key] = [] for i in range(len(r)): r[i] = fct[i](r[i]) data_by_year[key].append(r) else: data_by_year[''] = results nb_bar = len(data_by_year) * (len(fields) - 1) colors = map(lambda x: fill_style.Plain(bgcolor=x), misc.choice_colors(nb_bar)) abscissa = {} for line in data_by_year.keys(): fields_bar = [] # sum data and save it in a list. An item for a fields for d in data_by_year[line]: for idx in range(len(fields) - 1): fields_bar.append({}) if d[0] in fields_bar[idx]: fields_bar[idx][d[0]] += d[idx + 1] else: fields_bar[idx][d[0]] = d[idx + 1] for idx in range(len(fields) - 1): data = {} for k in fields_bar[idx].keys(): if k in data: data[k] += fields_bar[idx][k] else: data[k] = fields_bar[idx][k] data_cum = [] prev = 0.0 keys = data.keys() keys.sort() # cumulate if necessary for k in keys: data_cum.append([k, float(data[k]) + float(prev)]) if fields[idx + 1]['cumulate']: prev += data[k] idx0 = 0 plot = bar_plot.T( label=fields[idx + 1]['name'] + ' ' + str(line), data=data_cum, cluster=(idx0 * (len(fields) - 1) + idx, nb_bar), fill_style=colors[idx0 * (len(fields) - 1) + idx]) ar.add_plot(plot) abscissa.update(fields_bar[idx]) idx0 += 1 abscissa = map(lambda x: [x, None], abscissa) abscissa.sort() ar.x_coord = category_coord.T(abscissa, 0) ar.draw(can) can.close() self.obj = external_pdf(pdf_string.getvalue()) self.obj.render() pdf_string.close() return True