Graph and pivot views

In this recipe, we'll have a look at Odoo's business intelligence views. These are read only views meant to present data.

Getting ready

We're still making use of the project module here. First, we need some configuration. Activate the checkbox Manage time estimation on tasks in Configuration | Settings and also install the module project_timesheet in order to be able to record some time on different tasks.

Now create some projects meant to be umbrella projects for the customers. Then, create a couple of tasks for each customer project. They are meant to model different activities you have per customer. Assign different managers to these projects and activate the box Use tasks. Finally, create tasks in these projects, give a time estimation per task, and write some work hours on these tasks.

How to do it...

  1. Define a graph view using bars:
    <record id="graph_project_task_bar" model="ir.ui.view">
        <field name="model">project.task</field>
        <field name="arch" type="xml">
            <graph type="bar">
                <field name="project_id" type="row" />
                <field name="user_id" type="row" />
                <field name="effective_hours" type="measure" />
            </graph>
        </field>
    </record>
  2. Define a graph view using pivot tables:
    <record id="graph_project_task_pivot" model="ir.ui.view">
        <field name="model">project.task</field>
        <field name="arch" type="xml">
            <graph type="pivot">
                <field name="parent_id" type="row" />
                <field name="user_id" type="row" />
                <field name="planned_hours" type="measure" />
                <field name="effective_hours" type="measure" />
            </graph>
        </field>
    </record>
  3. Add menus and actions using this view. This is left as an exercise for the reader.

If everything went well, you should see graphs that show you how many hours were worked per customer (the projects we defined) under the responsibility of the different project managers.

How it works...

The type attribute determines the initial mode of a graph view. Possible values are pivot, bar, line, and chart, while bar is the default. The graph view is highly interactive, so the user can switch between the different modes and also add and remove fields.

The field elements tell Odoo what to display on which axis. For all the graph modes, you need at least one field with type row and one with type measure to see anything useful. Fields of type row determine the grouping, while type measure stands for the value(s) to be shown. Line graphs only support one field of each type, while charts and bars handle two group fields with one measure nicely. The pivot table supports an arbitrary amount of group and measure fields. No worries, things don't break if you switch to a mode that doesn't support the amount of groups and measures you defined. It's just that they'll ignore some of the fields and won't always show a very interesting result.

There's more...

For all the graph types, the Datetime fields are tricky for grouping, because you'll rarely encounter the same field value here. So if you have a Datetime field of type row, also specify the interval attribute with one of the following values: day, week, month, quarter, or year. This will cause the grouping to take place in the given interval.

The pivot table also supports grouping in columns. Use type col for the fields you want to have there.

Note

Grouping, like sorting, relies heavily on PostgreSQL. So here also the rule applies that a field must live in the database and in the current table in order to be usable.

It is common practice to define database views that collect all the data you need and define a model on top of this view in order to have all the necessary fields available.

Depending on the complexity of your view and the grouping, building the graph can be quite an expensive exercise. Consider setting the attribute auto_search to False in those cases, so that the user can first adjust all the parameters and only afterwards trigger a search.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.142.198.129