Customize Error Bars in Presentation Charts in .NET
Overview
This article explains how to work with error bars in presentation charts by using Aspose.Slides. It shows how to add error bars to a chart series, configure X and Y error bar settings, and apply different value types such as fixed, percentage, and custom values.
It also demonstrates how to assign custom error bar values for individual data points in a series by using the corresponding data point collection. In addition, the article includes brief notes about how error bars behave during export, their compatibility with markers and data labels, and where to find the related API reference classes and enums.
Add Error Bars
Aspose.Slides for .NET provides a simple API for managing error bar values. The sample code applies when using a custom value type. To specify a value, use the ErrorBarCustomValues property of a specific data point in the DataPoints collection of series:
- Create an instance of the Presentation class.
- Add a bubble chart on desired slide.
- Access the first chart series and set the error bar X format.
- Access the first chart series and set the error bar Y format.
- Setting bars values and format.
- Write the modified presentation to a PPTX file.
// Creating empty presentation
using (Presentation presentation = new Presentation())
{
// Creating a bubble chart
IChart chart = presentation.Slides[0].Shapes.AddChart(ChartType.Bubble, 50, 50, 400, 300, true);
// Adding Error bars and setting its format
IErrorBarsFormat errBarX = chart.ChartData.Series[0].ErrorBarsXFormat;
IErrorBarsFormat errBarY = chart.ChartData.Series[0].ErrorBarsYFormat;
errBarX.IsVisible = true;
errBarY.IsVisible = true;
errBarX.ValueType = ErrorBarValueType.Fixed;
errBarX.Value = 0.1f;
errBarY.ValueType = ErrorBarValueType.Percentage;
errBarY.Value = 5;
errBarX.Type = ErrorBarType.Plus;
errBarY.Format.Line.Width = 2;
errBarX.HasEndCap = true;
// Saving presentation
presentation.Save("ErrorBars_out.pptx", SaveFormat.Pptx);
}
Add Custom Error Bar Values
Aspose.Slides for .NET provides a simple API for managing custom error bar values. The sample code applies when the IErrorBarsFormat.ValueType property is equal to Custom. To specify a value, use the ErrorBarCustomValues property of a specific data point in the DataPoints collection of series:
- Create an instance of the Presentation class.
- Add a bubble chart on desired slide.
- Access the first chart series and set the error bar X format.
- Access the first chart series and set the error bar Y format.
- Access the chart series individual data points and setting the Error Bar values for individual series data point.
- Setting bars values and format.
- Write the modified presentation to a PPTX file.
// Creating empty presentation
using (Presentation presentation = new Presentation())
{
// Creating a bubble chart
IChart chart = presentation.Slides[0].Shapes.AddChart(ChartType.Bubble, 50, 50, 400, 300, true);
// Adding custom Error bars and setting its format
IChartSeries series = chart.ChartData.Series[0];
IErrorBarsFormat errBarX = series.ErrorBarsXFormat;
IErrorBarsFormat errBarY = series.ErrorBarsYFormat;
errBarX.IsVisible = true;
errBarY.IsVisible = true;
errBarX.ValueType = ErrorBarValueType.Custom;
errBarY.ValueType = ErrorBarValueType.Custom;
// Accessing chart series data point and setting error bars values for individual point
IChartDataPointCollection points = series.DataPoints;
points.DataSourceTypeForErrorBarsCustomValues.DataSourceTypeForXPlusValues = DataSourceType.DoubleLiterals;
points.DataSourceTypeForErrorBarsCustomValues.DataSourceTypeForXMinusValues = DataSourceType.DoubleLiterals;
points.DataSourceTypeForErrorBarsCustomValues.DataSourceTypeForYPlusValues = DataSourceType.DoubleLiterals;
points.DataSourceTypeForErrorBarsCustomValues.DataSourceTypeForYMinusValues = DataSourceType.DoubleLiterals;
// Setting error bars for chart series points
for (int i = 0; i < points.Count; i++)
{
points[i].ErrorBarsCustomValues.XMinus.AsLiteralDouble = i + 1;
points[i].ErrorBarsCustomValues.XPlus.AsLiteralDouble = i + 1;
points[i].ErrorBarsCustomValues.YMinus.AsLiteralDouble = i + 1;
points[i].ErrorBarsCustomValues.YPlus.AsLiteralDouble = i + 1;
}
// Saving presentation
presentation.Save("ErrorBarsCustomValues_out.pptx", SaveFormat.Pptx);
}
FAQ
What happens to error bars when exporting a presentation to PDF or images?
They are rendered as part of the chart and preserved during conversion along with the rest of the chart formatting, assuming a compatible version or renderer.
Can error bars be combined with markers and data labels?
Yes. Error bars are a separate element and are compatible with markers and data labels; if elements overlap, you may need to adjust formatting.
Where can I find the list of properties and enums for working with error bars in the API?
In the API reference: the ErrorBarsFormat class and the related enums ErrorBarType and ErrorBarValueType.