Create Dynamic Charts
Dynamic (or interactive) charts have the ability to change when you change the scope of data. In other words, the 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 ComboBox or Dropdown list.
This article demonstrates the usage of Aspose.Cells for Python via .NET APIs to create dynamic charts using both of the aforementioned approaches.
Using Excel Tables
ListObjects provides 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 . 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 .
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 ListObject based on the inserted data.
- Create Chart based on the data range of ListObject.
- Save the result on the disc.
from aspose.cells import Workbook | |
from aspose.cells.charts import ChartType | |
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
# The path to the documents directory. | |
dataDir = RunExamples.GetDataDir(".") | |
# Create an instance of Workbook | |
book = Workbook() | |
# Access first worksheet from the collection | |
sheet = book.worksheets[0] | |
# Access cells collection of the first worksheet | |
cells = sheet.cells | |
# Insert data column wise | |
cells.get("A1").put_value("Category") | |
cells.get("A2").put_value("Fruit") | |
cells.get("A3").put_value("Fruit") | |
cells.get("A4").put_value("Fruit") | |
cells.get("A5").put_value("Fruit") | |
cells.get("A6").put_value("Vegetables") | |
cells.get("A7").put_value("Vegetables") | |
cells.get("A8").put_value("Vegetables") | |
cells.get("A9").put_value("Vegetables") | |
cells.get("A10").put_value("Beverages") | |
cells.get("A11").put_value("Beverages") | |
cells.get("A12").put_value("Beverages") | |
cells.get("B1").put_value("Food") | |
cells.get("B2").put_value("Apple") | |
cells.get("B3").put_value("Banana") | |
cells.get("B4").put_value("Apricot") | |
cells.get("B5").put_value("Grapes") | |
cells.get("B6").put_value("Carrot") | |
cells.get("B7").put_value("Onion") | |
cells.get("B8").put_value("Cabage") | |
cells.get("B9").put_value("Potatoe") | |
cells.get("B10").put_value("Coke") | |
cells.get("B11").put_value("Coladas") | |
cells.get("B12").put_value("Fizz") | |
cells.get("C1").put_value("Cost") | |
cells.get("C2").put_value(2.2) | |
cells.get("C3").put_value(3.1) | |
cells.get("C4").put_value(4.1) | |
cells.get("C5").put_value(5.1) | |
cells.get("C6").put_value(4.4) | |
cells.get("C7").put_value(5.4) | |
cells.get("C8").put_value(6.5) | |
cells.get("C9").put_value(5.3) | |
cells.get("C10").put_value(3.2) | |
cells.get("C11").put_value(3.6) | |
cells.get("C12").put_value(5.2) | |
cells.get("D1").put_value("Profit") | |
cells.get("D2").put_value(0.1) | |
cells.get("D3").put_value(0.4) | |
cells.get("D4").put_value(0.5) | |
cells.get("D5").put_value(0.6) | |
cells.get("D6").put_value(0.7) | |
cells.get("D7").put_value(1.3) | |
cells.get("D8").put_value(0.8) | |
cells.get("D9").put_value(1.3) | |
cells.get("D10").put_value(2.2) | |
cells.get("D11").put_value(2.4) | |
cells.get("D12").put_value(3.3) | |
# Create ListObject, Get the List objects collection in the first worksheet | |
listObjects = sheet.list_objects | |
# Add a List based on the data source range with headers on | |
index = listObjects.add(0, 0, 11, 3, True) | |
sheet.auto_fit_columns() | |
# Create chart based on ListObject | |
index = sheet.charts.add(ChartType.COLUMN, 21, 1, 35, 18) | |
chart = sheet.charts[index] | |
chart.set_chart_data_range("A1:D12", True) | |
chart.n_series.category_data = "A2:B12" | |
# Save spreadsheet | |
book.save(dataDir + "output_out.xlsx") |
Using Dynamic Formulas
In case you do not wish to use the ListObject as a data source to 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 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 ComboBox. When 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 to 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 to the dynamic chart.
- Create ComboBox based on the Named Range created in the previous step.
- Insert some more data to the cells that will serve as a source to the VLOOKUP function.
- Insert VLOOKUP function (with appropriate parameters) to a range of cells. This will range will serve as a source to the dynamic chart.
- Create Chart based on the range created in the previous step.
- Save the result on the disc.
from aspose.cells import Workbook | |
from aspose.cells.charts import ChartType | |
from aspose.pydrawing import Color | |
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
# The path to the documents directory. | |
dataDir = RunExamples.GetDataDir(".") | |
# Create a workbook object | |
workbook = Workbook() | |
# Get the first worksheet | |
worksheet = workbook.worksheets[0] | |
# Create a range in the second worksheet | |
range = worksheet.cells.create_range("C21", "C24") | |
# Name the range | |
range.name = "MyRange" | |
# Fill different cells with data in the range | |
range.get(0, 0).put_value("North") | |
range.get(1, 0).put_value("South") | |
range.get(2, 0).put_value("East") | |
range.get(3, 0).put_value("West") | |
comboBox = worksheet.shapes.add_combo_box(15, 0, 2, 0, 17, 64) | |
comboBox.input_range = "=MyRange" | |
comboBox.linked_cell = "=B16" | |
comboBox.selected_index = 0 | |
cell = worksheet.cells.get("B16") | |
style = cell.get_style() | |
style.font.color = Color.white | |
cell.set_style(style) | |
worksheet.cells.get("C16").formula = "=INDEX(Sheet1!$C$21:$C$24,$B$16,1)" | |
# Put some data for chart source | |
# Data Headers | |
worksheet.cells.get("D15").put_value("Jan") | |
worksheet.cells.get("D20").put_value("Jan") | |
worksheet.cells.get("E15").put_value("Feb") | |
worksheet.cells.get("E20").put_value("Feb") | |
worksheet.cells.get("F15").put_value("Mar") | |
worksheet.cells.get("F20").put_value("Mar") | |
worksheet.cells.get("G15").put_value("Apr") | |
worksheet.cells.get("G20").put_value("Apr") | |
worksheet.cells.get("H15").put_value("May") | |
worksheet.cells.get("H20").put_value("May") | |
worksheet.cells.get("I15").put_value("Jun") | |
worksheet.cells.get("I20").put_value("Jun") | |
# Data | |
worksheet.cells.get("D21").put_value(304) | |
worksheet.cells.get("D22").put_value(402) | |
worksheet.cells.get("D23").put_value(321) | |
worksheet.cells.get("D24").put_value(123) | |
worksheet.cells.get("E21").put_value(300) | |
worksheet.cells.get("E22").put_value(500) | |
worksheet.cells.get("E23").put_value(219) | |
worksheet.cells.get("E24").put_value(422) | |
worksheet.cells.get("F21").put_value(222) | |
worksheet.cells.get("F22").put_value(331) | |
worksheet.cells.get("F23").put_value(112) | |
worksheet.cells.get("F24").put_value(350) | |
worksheet.cells.get("G21").put_value(100) | |
worksheet.cells.get("G22").put_value(200) | |
worksheet.cells.get("G23").put_value(300) | |
worksheet.cells.get("G24").put_value(400) | |
worksheet.cells.get("H21").put_value(200) | |
worksheet.cells.get("H22").put_value(300) | |
worksheet.cells.get("H23").put_value(400) | |
worksheet.cells.get("H24").put_value(500) | |
worksheet.cells.get("I21").put_value(400) | |
worksheet.cells.get("I22").put_value(200) | |
worksheet.cells.get("I23").put_value(200) | |
worksheet.cells.get("I24").put_value(100) | |
# Dynamically load data on selection of Dropdown value | |
worksheet.cells.get("D16").formula = "=IFERROR(VLOOKUP($C$16,$C$21:$I$24,2,FALSE),0)" | |
worksheet.cells.get("E16").formula = "=IFERROR(VLOOKUP($C$16,$C$21:$I$24,3,FALSE),0)" | |
worksheet.cells.get("F16").formula = "=IFERROR(VLOOKUP($C$16,$C$21:$I$24,4,FALSE),0)" | |
worksheet.cells.get("G16").formula = "=IFERROR(VLOOKUP($C$16,$C$21:$I$24,5,FALSE),0)" | |
worksheet.cells.get("H16").formula = "=IFERROR(VLOOKUP($C$16,$C$21:$I$24,6,FALSE),0)" | |
worksheet.cells.get("I16").formula = "=IFERROR(VLOOKUP($C$16,$C$21:$I$24,7,FALSE),0)" | |
# Create Chart | |
index = worksheet.charts.add(ChartType.COLUMN, 0, 3, 12, 9) | |
chart = worksheet.charts[index] | |
chart.n_series.add("='Sheet1'!$D$16:$I$16", False) | |
chart.n_series[0].name = "=C16" | |
chart.n_series.category_data = "=$D$15:$I$15" | |
# Save result on disc | |
workbook.save(dataDir + "output_out.xlsx") |