图表中的误差线可以帮助我们快速查看误差幅度和标准偏差。我们可以在二维面积图、条形图、柱形图、折线图和XY图中使用误差线,其中,只有XY图表(散点图和气泡图)可以显示X(水平)和Y(垂直)误差线,如果图表的类型不是XY,则只能显示Y(垂直)误差线。在PowerPoint中,我们还可以设置误差线的显示方向,如正负偏差、负偏差、正偏差,以及设置误差类型和误差量,如固定值、百分比、标准偏差、标准误差和自定义类型。
本文将介绍如何使用Spire.Presentation给PowerPoint文档中的非XY图表(柱形图)和XY图表(气泡图)添加误差线。
C#
using System.Drawing;
using Spire.Presentation;
using Spire.Presentation.Charts;
using Spire.Presentation.Drawing;
namespace Add_error_bars_to_chart_in_PPT
{
class Program
{
static void Main(string[] args)
{
//创建Presentation实例
Presentation ppt = new Presentation();
//加载PowerPoint文档
ppt.LoadFromFile("Input.pptx");
//获取第一张幻灯片中的柱形图并设置图表的标题
IChart columnChart = ppt.Slides[0].Shapes[0] as IChart;
columnChart.ChartTitle.TextProperties.Text = "垂直误差线";
//添加Y(垂直) 误差线
//获取图表中第一个系列的垂直误差线
IErrorBarsFormat errorBarsYFormat1 = columnChart.Series[0].ErrorBarsYFormat;
//设置末端样式为线端
errorBarsYFormat1.ErrorBarNoEndCap = false;
//指定误差线的方向为正偏差
errorBarsYFormat1.ErrorBarSimType = ErrorBarSimpleType.Plus;
//指定误差类型为标准误差
errorBarsYFormat1.ErrorBarvType = ErrorValueType.StandardError;
//设置误差量
errorBarsYFormat1.ErrorBarVal = 0.3f;
//设置线条格式
errorBarsYFormat1.Line.FillType = FillFormatType.Solid;
errorBarsYFormat1.Line.SolidFillColor.Color = Color.MediumVioletRed;
errorBarsYFormat1.Line.Width = 1;
//获取第二张幻灯片中的气泡图并设置图表的标题
IChart bubbleChart = ppt.Slides[1].Shapes[0] as IChart;
bubbleChart.ChartTitle.TextProperties.Text = "水平和垂直误差线";
//添加X(水平)和Y(垂直)误差线
//获取图表中第一个系列的水平误差线
IErrorBarsFormat errorBarsXFormat = bubbleChart.Series[0].ErrorBarsXFormat;
//设置末端样式为线端
errorBarsXFormat.ErrorBarNoEndCap = false;
//指定误差线的方向为正负偏差
errorBarsXFormat.ErrorBarSimType = ErrorBarSimpleType.Both;
//指定误差类型为标准误差
errorBarsXFormat.ErrorBarvType = ErrorValueType.StandardError;
//设置误差量
errorBarsXFormat.ErrorBarVal = 0.3f;
//获取图表中第一个系列的垂直误差线
IErrorBarsFormat errorBarsYFormat2 = bubbleChart.Series[0].ErrorBarsYFormat;
//设置末端样式为线端
errorBarsYFormat2.ErrorBarNoEndCap = false;
//指定误差线的方向为正负偏差
errorBarsYFormat2.ErrorBarSimType = ErrorBarSimpleType.Both;
//指定误差类型为标准误差
errorBarsYFormat2.ErrorBarvType = ErrorValueType.StandardError;
//设置误差量
errorBarsYFormat2.ErrorBarVal = 0.3f;
//保存文档
ppt.SaveToFile("ErrorBars.pptx", FileFormat.Pptx2013);
}
}
}
VB.NET
Imports System.Drawing
Imports Spire.Presentation
Imports Spire.Presentation.Charts
Imports Spire.Presentation.Drawing
Namespace Add_error_bars_to_chart_in_PPT
Class Program
Private Shared Sub Main(args As String())
'创建Presentation实例
Dim ppt As New Presentation()
'加载PowerPoint文档
ppt.LoadFromFile("Input.pptx")
'获取第一张幻灯片中的柱形图并设置图表的标题
Dim columnChart As IChart = TryCast(ppt.Slides(0).Shapes(0), IChart)
columnChart.ChartTitle.TextProperties.Text = "垂直误差线"
'添加Y(垂直) 误差线
'获取图表中第一个系列的垂直误差线
Dim errorBarsYFormat1 As IErrorBarsFormat = columnChart.Series(0).ErrorBarsYFormat
'设置末端样式为线端
errorBarsYFormat1.ErrorBarNoEndCap = False
'指定误差线的方向为正偏差
errorBarsYFormat1.ErrorBarSimType = ErrorBarSimpleType.Plus
'指定误差类型为标准误差
errorBarsYFormat1.ErrorBarvType = ErrorValueType.StandardError
'设置误差量
errorBarsYFormat1.ErrorBarVal = 0.3F
'设置线条格式
errorBarsYFormat1.Line.FillType = FillFormatType.Solid
errorBarsYFormat1.Line.SolidFillColor.Color = Color.MediumVioletRed
errorBarsYFormat1.Line.Width = 1
'获取第二张幻灯片中的气泡图并设置图表的标题
Dim bubbleChart As IChart = TryCast(ppt.Slides(1).Shapes(0), IChart)
bubbleChart.ChartTitle.TextProperties.Text = "水平和垂直误差线"
'添加X(水平)和Y(垂直)误差线
'获取图表中第一个系列的水平误差线
Dim errorBarsXFormat As IErrorBarsFormat = bubbleChart.Series(0).ErrorBarsXFormat
'设置末端样式为线端
errorBarsXFormat.ErrorBarNoEndCap = False
'指定误差线的方向为正负偏差
errorBarsXFormat.ErrorBarSimType = ErrorBarSimpleType.Both
'指定误差类型为标准误差
errorBarsXFormat.ErrorBarvType = ErrorValueType.StandardError
'设置误差量
errorBarsXFormat.ErrorBarVal = 0.3F
'获取图表中第一个系列的垂直误差线
Dim errorBarsYFormat2 As IErrorBarsFormat = bubbleChart.Series(0).ErrorBarsYFormat
'设置末端样式为线端
errorBarsYFormat2.ErrorBarNoEndCap = False
'指定误差线的方向为正负偏差
errorBarsYFormat2.ErrorBarSimType = ErrorBarSimpleType.Both
'指定误差类型为标准误差
errorBarsYFormat2.ErrorBarvType = ErrorValueType.StandardError
'设置误差量
errorBarsYFormat2.ErrorBarVal = 0.3F
'保存文档
ppt.SaveToFile("ErrorBars.pptx", FileFormat.Pptx2013)
End Sub
End Class
End Namespace
效果图: