示例#1
0
    def _create_pie(self, cr, uid, ids, report, fields, results, context):
        pdf_string = cStringIO.StringIO()
        can = canvas.init(fname=pdf_string, format='pdf')
        ar = area.T(size=(350, 350),
                    legend=legend.T(),
                    x_grid_style=None,
                    y_grid_style=None)
        colors = map(lambda x: fill_style.Plain(bgcolor=x),
                     misc.choice_colors(len(results)))

        if reduce(lambda x, y: x + y, map(lambda x: x[1], results)) == 0.0:
            raise UserError(
                _("The sum of the data (2nd field) is null.\nWe can't draw a pie chart !"
                  ))

        plot = pie_plot.T(data=results,
                          arc_offsets=[0, 10, 0, 10],
                          shadow=(2, -2, fill_style.gray50),
                          label_offset=25,
                          arrow_style=arrow.a3,
                          fill_styles=colors)
        ar.add_plot(plot)
        ar.draw(can)
        can.close()
        self.obj = external_pdf(pdf_string.getvalue())
        self.obj.render()
        pdf_string.close()
        return True
示例#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