Home > Code > C# > How to Add Fixed or Custom Error Bar Value for Chart in Presentation using .NET

How to Add Fixed or Custom Error Bar Value for Chart in Presentation using .NET

by sher azam   on Apr 06, 2016   Category: C#   |  Views: 470    |  Points: 25   |  Gold 


This technical tip explains how .NET developers can add Error Bars for charts inside .NET applications. Aspose.Slides for .NET lets developers add the error bars for the chart series data. This article explains how to add different chart error bar lines. 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.

// Adding Fixed Error Bar Value for Chart

//[C# Code Sample]


using (Presentation pres = new Presentation())
{
//Creating a bubble chart
IChart chart = pres.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
pres.Save("ErrorBars.pptx", SaveFormat.Pptx);
}

//[VB.NET Code Sample]

'Creating empty presentation
Using pres As Presentation = New Presentation()

'Creating a bubble chart
Dim chart As IChart = pres.Slides(0).Shapes.AddChart(ChartType.Bubble, 50, 50, 400, 300, True)

'Adding Error bars and setting its format
Dim errBarX As IErrorBarsFormat = chart.ChartData.Series(0).ErrorBarsXFormat
Dim errBarY As IErrorBarsFormat = 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
pres.Save("ErrorBars.pptx", SaveFormat.Pptx)
End Using

// Adding Custom Error Bar Value for Chart

//[C# Code Sample]

//Aspose.Slides for .NET provides a simple API for managing custom error bar values.

//Creating empty presentation
using (Presentation pres = new Presentation())
{
//Creating a bubble chart
IChart chart = pres.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.ErrorBarsCustomValues.XMinus.AsLiteralDouble = i + 1;
points.ErrorBarsCustomValues.XPlus.AsLiteralDouble = i + 1;
points.ErrorBarsCustomValues.YMinus.AsLiteralDouble = i + 1;
points.ErrorBarsCustomValues.YPlus.AsLiteralDouble = i + 1;
}

//Saving presentation
pres.Save("ErrorBarsCustomValues", SaveFormat.Pptx);
}

//[VB.NET Code Sample]

'Creating empty presentation
Using pres As Presentation = New Presentation()

'Creating a bubble chart
Dim chart As IChart = pres.Slides(0).Shapes.AddChart(ChartType.Bubble, 50, 50, 400, 300, True)
Dim series As IChartSeries = chart.ChartData.Series(0)

'Adding custom Error bars and setting its format
Dim errBarX As IErrorBarsFormat = series.ErrorBarsXFormat
Dim errBarY As IErrorBarsFormat = 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

Dim points As IChartDataPointCollection = 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 i As Integer = 0 To points.Count


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

Next

'Saving presentation
pres.Save("ErrorBarsCustomValues", SaveFormat.Pptx)

End Using



Overview: Aspose.Slides for .NET

Aspose.Slides is a .NET component to read, write and modify a PowerPoint document without using MS PowerPoint. PowerPoint versions from 97-2007 and all three PowerPoint formats: PPT, POT, PPS are also supported. Now you can create, access, copy, clone, edit and delete slides in your presentations. Other features include PPT/PPTX printing, PPT to XPS format, saving PowerPoint slides into PDF, adding & modifying audio & video frames, using shapes like rectangles or ellipses and saving presentations in SVG format, streams or images.

- Homepage of Aspose.Slides for .NET: http://www.aspose.com/.net/powerpoint-component.aspx

- Download Aspose.Slides for .NET: http://www.aspose.com/community/files/51/.net-components/aspose.slides-for-.net/default.aspx



Post Code  |  Code Snippet Home

User Responses


No response found, be the first to review this code snippet.

Submit feedback about this code snippet

Please sign in to post feedback

Latest Posts