Create Dynamic Charts with Golang via C++
Dynamic (or interactive) charts have the ability to change when you change the scope of data. In other words, dynamic charts can automatically reflect changes when the data source is changed. In order to trigger the change in the data source, one can use the filtering option of Excel Tables or use a control such as a ComboBox or a dropdown list.
This article demonstrates the usage of Aspose.Cells for Go via Go APIs to create dynamic charts using both of the aforementioned approaches.
Using Excel Tables
ListObjects provide the in‑built functionality to sort & filter the data upon user interaction. Both sorting & filtering options are provided through the drop‑down lists which are automatically added to the header row of the ListObject. Due to these features (sorting & filtering), the ListObject seems to be the perfect candidate to serve as the data source to a dynamic chart because when sorting or filtering is changed, the representation of data in the chart will be changed to reflect the current state of the ListObject.
In order to keep the demonstration simple to understand, we will create the Workbook from scratch and move forward step by step as outlined below.
- Create an empty Workbook.
- Access the Cells of the first Worksheet in the Workbook.
- Insert some data to the cells.
- Create a ListObject based on the inserted data.
- Create a Chart based on the data range of the ListObject.
- Save the result on the disk.
Using Dynamic Formulas
In case you do not wish to use the ListObject as a data source for the dynamic chart, the other option is to use Excel functions (or formulas) to create a dynamic range of data, and a control (such as a ComboBox) to trigger the change in data. In this scenario, we will use the VLOOKUP function to fetch the appropriate values based on the selection of the ComboBox. When the selection is changed, the VLOOKUP function will refresh the cell value. If a range of cells is using the VLOOKUP function, the whole range can be refreshed upon user interaction; therefore, it can be used as a source for the dynamic chart.
In order to keep the demonstration simple to understand, we will create the Workbook from scratch and move forward step by step as outlined below.
- Create an empty Workbook.
- Access the Cells of the first Worksheet in the Workbook.
- Insert some data to the cells by creating a Named Range. This data will serve as a series for the dynamic chart.
- Create a ComboBox based on the Named Range created in the previous step.
- Insert some more data to the cells that will serve as a source for the VLOOKUP function.
- Insert VLOOKUP functions (with appropriate parameters) to a range of cells. This range will serve as a source for the dynamic chart.
- Create a Chart based on the range created in the previous step.
- Save the result on the disk.