Optimize Chart Calculations for Presentations in C++
Overview
Aspose.Slides provides APIs for working with chart calculations and layout data in presentations. This article shows how to retrieve the actual values of chart elements, including the real position and size of elements that implement IActualLayout and the actual values of chart axes. It also explains that these values are populated after chart layout validation.
In addition, the article demonstrates how to get the actual position of parent chart elements and how to hide chart components such as the title, axes, legend, and grid lines. Together, these examples help you inspect chart layout information and control the visibility of chart elements in PowerPoint presentations programmatically.
Calculate Actual Values of Chart Elements
Aspose.Slides for C++ provides a simple API for getting these properties. This will help you to calculate actual values of chart elements. The actual values include position of elements that implement IActualLayout interface (IActualLayout::get_ActualX(), IActualLayout::get_ActualY(), IActualLayout::get_ActualWidth(), IActualLayout::get_ActualHeight()) and actual axes values (IAxis::get_ActualMaxValue(), IAxis::get_ActualMinValue(), IAxis::get_ActualMajorUnit(), IAxis::get_ActualMinorUnit(), IAxis::get_ActualMajorUnitScale(), IAxis::get_ActualMinorUnitScale()).
auto pres = System::MakeObject<Presentation>(u"test.pptx");
auto chart = System::ExplicitCast<Chart>(pres->get_Slides()->idx_get(0)->get_Shapes()->AddChart(ChartType::ClusteredColumn, 100.0f, 100.0f, 500.0f, 350.0f));
chart->ValidateChartLayout();
double x = chart->get_PlotArea()->get_ActualX();
double y = chart->get_PlotArea()->get_ActualY();
double w = chart->get_PlotArea()->get_ActualWidth();
double h = chart->get_PlotArea()->get_ActualHeight();
// Saving presentation
pres->Save(u"Result.pptx", SaveFormat::Pptx);
Calculate the Actual Position of Parent Chart Elements
Aspose.Slides for C++ provides a simple API for getting these properties. Methods of IActualLayout provide information about actual position of parent chart element. It is necessary to call method IChart::ValidateChartLayout() previously to fill properties with actual values.
// Creating empty presentation
auto pres = System::MakeObject<Presentation>();
auto chart = System::ExplicitCast<Chart>(pres->get_Slides()->idx_get(0)->get_Shapes()->AddChart(ChartType::ClusteredColumn, 100.0f, 100.0f, 500.0f, 350.0f));
chart->ValidateChartLayout();
double x = chart->get_PlotArea()->get_ActualX();
double y = chart->get_PlotArea()->get_ActualY();
double w = chart->get_PlotArea()->get_ActualWidth();
double h = chart->get_PlotArea()->get_ActualHeight();
Hide Chart Elements
This topic helps you to understand how to hide information from chart. Using Aspose.Slides for C++ you can hide Title, Vertical Axis, Horizontal Axis and Grid Lines from chart. Below code example shows how to use these properties.
Set a Data Range for a Chart
Aspose.Slides for C++ has provided the simplest API to set the data range for chart in an easiest way. To set the data range for chart:
- Open an instance of Presentation class containing chart.
- Obtain the reference of a slide by using its Index.
- Traverse through all shapes to find desired chart.
- Access the chart data and set the range.
- Save the modified presentation as a PPTX file.
The code examples that follow how to update a chart.
FAQ
Do external Excel workbooks work as a data source, and how does that affect recalculation?
Yes. A chart can reference an external workbook: when you connect or refresh the external source, formulas and values are taken from that workbook, and the chart reflects the updates during open/edit operations. The API lets you specify the external workbook path and manage the linked data.
Can I compute and display trendlines without implementing regression myself?
Yes. Trendlines (linear, exponential, and others) are added and updated by Aspose.Slides; their parameters are recalculated from the series data automatically, so you don’t need to implement your own calculations.
If a presentation has multiple charts with external links, can I control which workbook each chart uses for computed values?
Yes. Each chart can point to its own external workbook, or you can create/replace an external workbook per chart independently of the others.