Create dynamic reports in LibreOffice

Create pie charts in ODT documents in LibreOffice

The dataset below is used in a chart that represents the quantity of cheese purchased by french households in 2019.

Step 1: Prepare JSON data

Group values by cheese types using the following dataset:

Data

{
  "cheeses": [
    {
      "type": "Camembert",
      "purchasedTonnes": 47503
    },
    {
      "type": "Emmental",
      "purchasedTonnes": 152468
    },
    {
      "type": "Compté",
      "purchasedTonnes": 32925
    },
    {
      "type": "Goat's Buchette",
      "purchasedTonnes": 32095
    },
    {
      "type": "Mozzarella",
      "purchasedTonnes": 31066
    }
  ]
}

Step 2: Create a new ODT template in LibreOffice

Click the Home tab > Chart button. A column chart appears. Change the chart format by clicking the chart type button from the chart toolbar.

Step 3: Edit chart values

Insert the data dynamically using a loop and the bindChart formatter: Click Data Table from the chart toolbar. A popup appears to edit chart values.

The following is not different than Microsoft Word. It's not possible to insert tag {d.value} inside table cells, except in the first column Categories.

So you write the category name, then add bindChart tags. The {bindChart} formatter is used to bind a variable to a reference tag in the table cell, i.e., "the value X in the chart must be replaced by the tag Y."

Screenshot explanations

  • jn the first cell, the following expression is written: {d.cheeses[i].type} {bindChart(3)=d.cheeses[i].purchasedTonnes}

    • the cheese type is printed with {d.cheeses[i].type}, then bindChart is used to bind the variable d.cheeses[i].purchasedTonnes to the cell that has the value 3

      • this means: the purchasedTonnes value is printed instead of 3

  • in the first cell of the second row, the expression {d.cheeses[i+1].type} {bindChart(4)=d.cheeses[i+1].purchasedTonnes} is written

    • the bindChart replaces the 4 value by the d.cheeses[i+1].purchasedTonnes variable

Step 4: Generate the report

Generate the PDF report, and the pie chart is automatically filled.

Last updated