Insert Line Chart

This guide will explain how to add line chart inside spreadsheet workbook using UniOffice.

Before you begin

You should get your API key from your UniCloud account.

If this is your first time using UniOffice SDK, follow this guide to set up a local development environment.

Clone the project repository

In your terminal, clone the examples repository. It contains the Go code we will be using for this guide.

git clone

To get the example navigate to the path spreadsheet/line-chart folder in the unioffice-examples directory.

cd unioffice-examples/spreadsheet/line-chart/

How it works

The import section in lines 4-11, imports the necessary libraries.
The init function in lines 13-20 initializes the package by setting the metered license key using license.SetMeteredKey(os.Getenv(`UNIDOC_LICENSE_API_KEY`)).

The main function is defined in lines 22-83. In lines 23-25, a new spreadsheet.Workbook with a new spreadsheet.Sheet is created. The table header and the data rows are created in lines 28-39. A new spreadsheet.Drawing is created and a Chart is added to the drawing. In line 43 a new chart and ancor is created using dwng.AddChart(spreadsheet.AnchorTypeTwoCell). Then a new LineChart is created in line 47.

Three LineChartSeries with texts price, sold, and total are created in lines 48-60. Their labels and values are set accordingly by setting cell references.

In lines 63-69, category axis and value axis are added to line chart using:

ca := chart.AddCategoryAxis()
va := chart.AddValueAxis()

Then the title and legend are set in lines 72-74.
Finally, the chart is added to the sheet and the workbook is saved to file in lines 77-82 using:

if err := ss.Validate(); err != nil {
  log.Fatalf("error validating sheet: %s", err)

Run the code

Use the following command to run the code.

go run main.go

Sample Output

sample spreadsheet with line chart

Got any Questions?

We're here to help you.