Barchart

This guide will demonstrate the process of creating spreadsheet bar-charts 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 https://github.com/unidoc/unioffice-examples

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

cd unioffice-examples/spreadsheet/bar-chart/

How it works

In lines 4-11, the necessary libraries and packages are imported. The metered API license key is set in lines 13-20, in the init function.

The main function is defined in lines 22-82. In line 23 of this function a new spreadsheet.Workbook object is created using spreadsheet.New() function call. Then a new spreadsheet.Sheet is added to the work book object. A new header row is created in lines 28-32. The for loop in lines 33-39, adds all the row data one row at a time.

A new spreadsheet.Drawing is created in line 42. This drawing will hold the charts to be drawn. Then in lines 43-44, a new chart.Chart is created and the cells’ width is set using anc.SetWidthCells(10). Using this chart, a new bar chart is created in line 46 using chart.AddBarChart(). A new series is created in line 47 and the text of the price is set in line 48 using priceSeries.SetText("Price"). The next two lines set the category axis reference for the first series i.e. the price series.

In lines 33-59, two more series are added. Category axis and value axis are added to the chart in lines 62-68. The title and legend of the chart are set in lines 71-73.

The chart is added to the sheet, in line 76 using sheet.SetDrawing(dwng). Finally, the sheet is validated and saved to file 78-81.

Run the code

Run the following command to create the spreed sheet file.

go run main.go

Sample output

Spreadsheet file.

sample output file

Got any Questions?

We're here to help you.