Wednesday, March 15, 2017

Pivot charts in LibreOffice: Part 1

About

Pivot tables are a powerful tool to reorganise, manipulate and summarise the data set in spreadsheets to get the valuable information from it. To get a quick visual representation of the information, pivot charts can be used. A pivot chart can be created from the output of the pivot tables, and if the pivot table gets changed, so does the pivot chart.

Support for pivot tables in LibreOffice is available for a long time, but there was no support for pivot charts until now. For the past week I was working on pivot charts in a feature branch (feature/pivotcharts) and I got to a first milestone. Pivot charts will be released in LibreOffice 5.4.

Pivot chart data provider

From development point of view, pivot charts are just like normal charts but with a different data provider (source of data), so this was the task with which I started. Normal charts use a data provider which is based around reading from cell ranges, but for pivot charts I created a new data provider, which reads the output data from the pivot table and prepares it for the chart. The data columns are mapped to data series and the data rows become the number of data series in chart (See Figure 1).

Figure1: Pivot table to pivot chart data mapping
Now what is left is naming of each axis and data series in chart. The y-axis categories are mapped to row field names in the pivot table and the data series names, which are shown in the chart are combined names of all column field names of the pivot table.

Each data point and row or column field name also has an associated number format, which needs to be assign to chart data, otherwise the the number format would not the values correctly as in pivot table (this is especially important with date and time).

Updating a pivot chart

Once I managed to do the mapping correctly, the pivot chart showed up as expected, but the pivot chart wasn't updated when I update the pivot table. So to solve this, I had to implement a listener of pivot table updates in the pivot chart data provider, and for every update send the signal to chart to update the data again (which it gets from the pivot chart data provider). The whole update procedure sounds like a ping-pong play between components, but it works quite well.

Demo

In the following video you can see the current status of development:



Credits

One of the real privileges here is working on LibreOffice for a Collabora Productivity customer who funds significant feature work. Many thanks to Nantes Métropole and Ville de Nantes for their investment here, and making this feature available to all LibreOffice users. You can read more about Nantes deployment here.

To be continued...

6 comments:

  1. Congrats for the work done so far Tomaz. Pivot charts are long time due indeed and this is really impressive.

    Simple question: Are we restricted to bar charts (if another type is admissible for the data)?

    Also, opened a bug to record the need of a help page, and it refers to this post. (#106623)

    ReplyDelete
  2. Thanks Olivier.

    No we aren't restricted - any chart type is possible, but so of them don't make sense like XY. I'll remove those from UI in the future.

    Regards, Tomaž

    ReplyDelete
  3. My congratulations! That pivot chart is great!

    ReplyDelete
  4. Please don't withdraw XY scattergrams, they can be useful.
    If you have unevenly spread axes, for instance data for the years 1913, 1920, 1947 and then for every year and want to draw a line chart a XY scatter with a line linking the data points is a way to skirt the limits of the standard line draw procedure.
    Users often reinvent the uses in a way the inventor-developper did not think of. So, leave it to the users, and give best practice examples in the help section, please.

    But for the rest, thanks for the good work.

    ReplyDelete
    Replies
    1. Thanks for the feedback. In the end I didn't restrict anything so you can use any chart type you want.

      Delete
  5. I was wondering how to hide field buttons in a pivot chart; Version: 5.4.2.2. I cant export a diagram without the field buttons.

    ReplyDelete