Example #1
0
 def creat_area(self, size, xaxis, yaxis, y_range=(0, None)):
     ar = area.T(size=size,
                 x_coord=category_coord.T(self.data, 0),
                 x_axis=xaxis,
                 y_axis=yaxis,
                 y_range=y_range,
                 bg_style=fill_style.gray90,
                 x_grid_style=line_style.gray70,
                 border_line_style=line_style.default)
     return ar
Example #2
0
    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