
图表用于总结和直观地表示 PowerPoint 演示文稿中的数据。因此,PowerPoint 提供了广泛的图表类型来可视化数据。其中,最常用的图表类型包括饼图、折线图、条形图、直方图、股票图表等。在本文中,您将学习如何使用 C# 在 PowerPoint 演示文稿中创建这些图表。
- 在 PowerPoint 中创建图表的 C# API
- 使用 C# 在 PowerPoint 中创建柱形图
- 使用 C# 在 PowerPoint 中创建散点图
- 使用 C# 在 PowerPoint 中添加饼图
- 使用 C# 在 PowerPoint 中添加直方图
- 使用 C# 在 PowerPoint 中创建股票图表
- 获取免费 API 许可证
在 PowerPoint 中创建图表的 C# API
Aspose.Slides for .NET 是一个 C# 类库,可让您在 .NET 应用程序中创建和操作 PowerPoint 演示文稿。此外,API 允许您无缝地创建图表并将其添加到演示文稿中。 Aspose.Slides for .NET 可以下载为 DLL 或通过 NuGet 安装。
PM> Install-Package Aspose.Slides
使用 C# 在 PowerPoint PPT 中创建柱形图
在本节中,您将学习如何创建柱形图并添加类别和系列来填充该图表。以下是执行此操作的步骤。
- 首先,创建一个 Presentation 类的实例。
- 获取 ISlide 对象中幻灯片的引用。
- 添加一个带有默认数据的 ClusteredColumn 图表,并在 IChart 对象中获取它的引用。
- 使用 IChart.ChartTitle.AddTextFrameForOverriding(String) 方法设置图表标题并设置其属性。
- 将图表数据工作簿访问到 IChartDataWorkbook 对象中。
- 分别使用 IChart.ChartData.Series.Clear() 和 IChart.ChartData.Categories.Clear() 方法从图表数据中清除所有默认系列和类别。
- 添加新的系列和类别。
- 将每个图表系列访问到 IChartSeries 对象并向其添加数据点。
- 为图表系列添加填充颜色并设置标签。
- 最后,使用 Presentation.Save(String, SaveFormat) 方法保存演示文稿。
为了演示,以下代码示例展示了如何使用 C# 在 PowerPoint 演示文稿中创建柱形图。
// 实例化表示 PPTX 文件的 Presentation 类
Presentation pres = new Presentation();
// 访问第一张幻灯片
ISlide sld = pres.Slides[0];
// 添加带有默认数据的图表
IChart chart = sld.Shapes.AddChart(ChartType.ClusteredColumn, 0, 0, 500, 500);
// 设置图表标题
chart.ChartTitle.AddTextFrameForOverriding("Sample Title");
chart.ChartTitle.TextFrameForOverriding.TextFrameFormat.CenterText = NullableBool.True;
chart.ChartTitle.Height = 20;
chart.HasTitle = true;
// 将第一个系列设置为显示值
chart.ChartData.Series[0].Labels.DefaultDataLabelFormat.ShowValue = true;
// 设置图表数据表的索引
int defaultWorksheetIndex = 0;
// 获取图表数据工作表
IChartDataWorkbook fact = chart.ChartData.ChartDataWorkbook;
// 删除默认生成的系列和类别
chart.ChartData.Series.Clear();
chart.ChartData.Categories.Clear();
int s = chart.ChartData.Series.Count;
s = chart.ChartData.Categories.Count;
// 添加新系列
chart.ChartData.Series.Add(fact.GetCell(defaultWorksheetIndex, 0, 1, "Series 1"), chart.Type);
chart.ChartData.Series.Add(fact.GetCell(defaultWorksheetIndex, 0, 2, "Series 2"), chart.Type);
// 添加新类别
chart.ChartData.Categories.Add(fact.GetCell(defaultWorksheetIndex, 1, 0, "Caetegoty 1"));
chart.ChartData.Categories.Add(fact.GetCell(defaultWorksheetIndex, 2, 0, "Caetegoty 2"));
chart.ChartData.Categories.Add(fact.GetCell(defaultWorksheetIndex, 3, 0, "Caetegoty 3"));
// 获取第一个图表系列
IChartSeries series = chart.ChartData.Series[0];
// 现在填充系列数据
series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 1, 1, 20));
series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 2, 1, 50));
series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 3, 1, 30));
// 设置系列的填充颜色
series.Format.Fill.FillType = FillType.Solid;
series.Format.Fill.SolidFillColor.Color = System.Drawing.Color.Blue;
// 采取第二个图表系列
series = chart.ChartData.Series[1];
// 现在填充系列数据
series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 1, 2, 30));
series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 2, 2, 10));
series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 3, 2, 60));
// 设置系列的填充颜色
series.Format.Fill.FillType = FillType.Solid;
series.Format.Fill.SolidFillColor.Color = Color.Orange;
// 第一个标签将显示类别名称
IDataLabel lbl = series.DataPoints[0].Label;
lbl.DataLabelFormat.ShowCategoryName = true;
lbl = series.DataPoints[1].Label;
lbl.DataLabelFormat.ShowSeriesName = true;
// 显示第三个标签的值
lbl = series.DataPoints[2].Label;
lbl.DataLabelFormat.ShowValue = true;
lbl.DataLabelFormat.ShowSeriesName = true;
lbl.DataLabelFormat.Separator = "/";
// 使用图表保存演示文稿
pres.Save("column-chart.pptx", SaveFormat.Pptx);
以下是生成的柱形图的屏幕截图。

使用 C# 在 PowerPoint PPT 中创建散点图
以下是使用 C# 在 PowerPoint 演示文稿中创建散点图的步骤。
- 使用 Presentation 类创建一个新的演示文稿。
- 获取 ISlide 对象中幻灯片的引用。
- 添加具有默认数据的 ScatterWithSmoothLines 图表类型并在 IChart 对象中获取其引用。
- 将图表数据工作簿访问到 IChartDataWorkbook 对象并清除默认系列。
- 向图表数据添加新系列。
- 将每个系列访问到 IChartSeries 对象并将数据点添加到系列。
- 使用 IChartSeries.Marker 属性设置系列的标记。
- 使用 Presentation.Save(String, SaveFormat) 方法保存演示文稿。
以下代码示例演示如何使用 C# 在 PowerPoint 演示文稿中创建散点图。
// 实例化表示 PPTX 文件的 Presentation 类
Presentation pres = new Presentation();
// 访问第一张幻灯片
ISlide sld = pres.Slides[0];
// 添加带有默认数据的图表
IChart chart = sld.Shapes.AddChart(ChartType.ScatterWithSmoothLines, 0, 0, 400, 400);
// 获取默认图表数据工作表索引
int defaultWorksheetIndex = 0;
// 获取图表数据工作表
IChartDataWorkbook fact = chart.ChartData.ChartDataWorkbook;
// 删除演示系列
chart.ChartData.Series.Clear();
// 添加新系列
chart.ChartData.Series.Add(fact.GetCell(defaultWorksheetIndex, 1, 1, "Series 1"), chart.Type);
chart.ChartData.Series.Add(fact.GetCell(defaultWorksheetIndex, 1, 3, "Series 2"), chart.Type);
// 获取第一个图表系列
IChartSeries series = chart.ChartData.Series[0];
// 在那里添加新点 (1:3)。
series.DataPoints.AddDataPointForScatterSeries(fact.GetCell(defaultWorksheetIndex, 2, 1, 1), fact.GetCell(defaultWorksheetIndex, 2, 2, 3));
// 添加新点 (2:10)
series.DataPoints.AddDataPointForScatterSeries(fact.GetCell(defaultWorksheetIndex, 3, 1, 2), fact.GetCell(defaultWorksheetIndex, 3, 2, 10));
// 编辑系列类型
series.Type = ChartType.ScatterWithStraightLinesAndMarkers;
// 更改图表系列标记
series.Marker.Size = 10;
series.Marker.Symbol = MarkerStyleType.Star;
// 采取第二个图表系列
series = chart.ChartData.Series[1];
// 在那里添加新点 (5:2)。
series.DataPoints.AddDataPointForScatterSeries(fact.GetCell(defaultWorksheetIndex, 2, 3, 5), fact.GetCell(defaultWorksheetIndex, 2, 4, 2));
// 添加新点 (3:1)
series.DataPoints.AddDataPointForScatterSeries(fact.GetCell(defaultWorksheetIndex, 3, 3, 3), fact.GetCell(defaultWorksheetIndex, 3, 4, 1));
// 添加新点 (2:2)
series.DataPoints.AddDataPointForScatterSeries(fact.GetCell(defaultWorksheetIndex, 4, 3, 2), fact.GetCell(defaultWorksheetIndex, 4, 4, 2));
// 添加新点 (5:1)
series.DataPoints.AddDataPointForScatterSeries(fact.GetCell(defaultWorksheetIndex, 5, 3, 5), fact.GetCell(defaultWorksheetIndex, 5, 4, 1));
// 更改图表系列标记
series.Marker.Size = 10;
series.Marker.Symbol = MarkerStyleType.Circle;
// 使用图表保存演示文稿
pres.Save("scattered-chart.pptx", SaveFormat.Pptx);
以下屏幕截图显示了生成的散点图。

使用 C# 在 PowerPoint PPT 中创建饼图
以下是使用 C# 在 PowerPoint 演示文稿中创建饼图的步骤。
- 使用 Presentation 类创建一个新的演示文稿。
- 获取 ISlide 对象中幻灯片的引用。
- 添加带有默认数据的饼图类型并在 IChart 对象中获取其引用。
- 使用 IChart.ChartTitle.AddTextFrameForOverriding(String) 方法设置图表标题并设置其属性。
- 设置值的可见性。
- 分别使用 IChart.ChartData.Series.Clear() 和 IChart.ChartData.Categories.Clear() 方法清除默认系列和类别。
- 将图表数据工作簿访问到 IChartDataWorkbook 对象中。
- 将图表数据的新类别添加到 IChart.ChartData.Categories 集合中。
- 将图表数据的新系列添加到 IChart.ChartData.Series 集合中。
- 将每个系列获取到 IChartSeries 对象中,并将数据点添加到系列中。
- 将每个数据点访问到 IChartDataPoint 对象并设置其格式。
- 使用 IDataLabel 对象访问数据点中的数据标签并设置其格式。
- 设置引导线和旋转角度。
- 使用 Presentation.Save(String, SaveFormat) 方法保存演示文稿。
以下代码示例演示如何使用 C# 在 PowerPoint 演示文稿中创建饼图。
// 实例化表示 PPTX 文件的 Presentation 类
Presentation presentation = new Presentation();
// 访问第一张幻灯片
ISlide slides = presentation.Slides[0];
// 添加带有默认数据的图表
IChart chart = slides.Shapes.AddChart(ChartType.Pie, 100, 100, 400, 400);
// 设置图表标题
chart.ChartTitle.AddTextFrameForOverriding("Sample Title");
chart.ChartTitle.TextFrameForOverriding.TextFrameFormat.CenterText = NullableBool.True;
chart.ChartTitle.Height = 20;
chart.HasTitle = true;
// 将第一个系列设置为显示值
chart.ChartData.Series[0].Labels.DefaultDataLabelFormat.ShowValue = true;
// 设置图表数据表的索引
int defaultWorksheetIndex = 0;
// 获取图表数据工作表
IChartDataWorkbook fact = chart.ChartData.ChartDataWorkbook;
// 删除默认生成的系列和类别
chart.ChartData.Series.Clear();
chart.ChartData.Categories.Clear();
// 添加新类别
chart.ChartData.Categories.Add(fact.GetCell(0, 1, 0, "First Qtr"));
chart.ChartData.Categories.Add(fact.GetCell(0, 2, 0, "2nd Qtr"));
chart.ChartData.Categories.Add(fact.GetCell(0, 3, 0, "3rd Qtr"));
// 添加新系列
IChartSeries series = chart.ChartData.Series.Add(fact.GetCell(0, 0, 1, "Series 1"), chart.Type);
// 现在填充系列数据
series.DataPoints.AddDataPointForPieSeries(fact.GetCell(defaultWorksheetIndex, 1, 1, 20));
series.DataPoints.AddDataPointForPieSeries(fact.GetCell(defaultWorksheetIndex, 2, 1, 50));
series.DataPoints.AddDataPointForPieSeries(fact.GetCell(defaultWorksheetIndex, 3, 1, 30));
// 在新版本中无法使用
// 添加新点并设置扇区颜色
// 系列.IsColorVaried = true;
chart.ChartData.SeriesGroups[0].IsColorVaried = true;
IChartDataPoint point = series.DataPoints[0];
point.Format.Fill.FillType = FillType.Solid;
point.Format.Fill.SolidFillColor.Color = Color.Orange;
// 设置扇区边界
point.Format.Line.FillFormat.FillType = FillType.Solid;
point.Format.Line.FillFormat.SolidFillColor.Color = Color.Gray;
point.Format.Line.Width = 3.0;
//point.Format.Line.Style = LineStyle.ThinThick;
//point.Format.Line.DashStyle = LineDashStyle.DashDot;
IChartDataPoint point1 = series.DataPoints[1];
point1.Format.Fill.FillType = FillType.Solid;
point1.Format.Fill.SolidFillColor.Color = Color.BlueViolet;
// 设置扇区边界
point1.Format.Line.FillFormat.FillType = FillType.Solid;
point1.Format.Line.FillFormat.SolidFillColor.Color = Color.Blue;
point1.Format.Line.Width = 3.0;
//point1.Format.Line.Style = LineStyle.Single;
//point1.Format.Line.DashStyle = LineDashStyle.LargeDashDot;
IChartDataPoint point2 = series.DataPoints[2];
point2.Format.Fill.FillType = FillType.Solid;
point2.Format.Fill.SolidFillColor.Color = Color.YellowGreen;
// 设置扇区边界
point2.Format.Line.FillFormat.FillType = FillType.Solid;
point2.Format.Line.FillFormat.SolidFillColor.Color = Color.Red;
point2.Format.Line.Width = 2.0;
//point2.Format.Line.Style = LineStyle.ThinThin;
//point2.Format.Line.DashStyle = LineDashStyle.LargeDashDotDot;
// 为新系列的每个类别创建自定义标签
IDataLabel lbl1 = series.DataPoints[0].Label;
// lbl.ShowCategoryName = true;
lbl1.DataLabelFormat.ShowValue = true;
IDataLabel lbl2 = series.DataPoints[1].Label;
lbl2.DataLabelFormat.ShowValue = true;
lbl2.DataLabelFormat.ShowLegendKey = true;
lbl2.DataLabelFormat.ShowPercentage = true;
IDataLabel lbl3 = series.DataPoints[2].Label;
lbl3.DataLabelFormat.ShowSeriesName = true;
lbl3.DataLabelFormat.ShowPercentage = true;
// 显示图表的引导线
//series.Labels.DefaultDataLabelFormat.ShowLeaderLines = true;
// 设置饼图扇区的旋转角度
chart.ChartData.SeriesGroups[0].FirstSliceAngle = 180;
// 使用图表保存演示文稿
presentation.Save("pie-chart.pptx", SaveFormat.Pptx);
以下是生成的饼图的截图。

使用 C# 在 PowerPoint PPTX 中添加直方图
以下是使用 Aspose.Slides for .NET 在 PowerPoint 演示文稿中创建直方图的步骤。
- 创建 Presentation 类的实例。
- 通过索引获取 ISlide 对象中幻灯片的引用。
- 添加带有默认数据的直方图。
- 清除默认系列和类别。
- 访问 IChartDataWorkbook 对象中的图表数据工作簿。
- 添加新系列并在 IChartSeries 对象中获取其引用。
- 向系列中添加数据点。
- 设置图表轴的聚合类型。
- 使用 Presentation.Save(String, SaveFormat) 方法保存演示文稿。
以下代码示例展示了如何使用 C# 创建直方图。
// 加载或创建演示文稿
using (Presentation pres = new Presentation())
{
// 添加直方图
IChart chart = pres.Slides[0].Shapes.AddChart(ChartType.Histogram, 50, 50, 500, 400);
chart.ChartData.Categories.Clear();
chart.ChartData.Series.Clear();
// 访问图表数据工作簿
IChartDataWorkbook wb = chart.ChartData.ChartDataWorkbook;
// 清除工作簿
wb.Clear(0);
// 添加图表系列
IChartSeries series = chart.ChartData.Series.Add(ChartType.Histogram);
series.DataPoints.AddDataPointForHistogramSeries(wb.GetCell(0, "A1", 15));
series.DataPoints.AddDataPointForHistogramSeries(wb.GetCell(0, "A2", -41));
series.DataPoints.AddDataPointForHistogramSeries(wb.GetCell(0, "A3", 16));
series.DataPoints.AddDataPointForHistogramSeries(wb.GetCell(0, "A4", 10));
series.DataPoints.AddDataPointForHistogramSeries(wb.GetCell(0, "A5", -23));
series.DataPoints.AddDataPointForHistogramSeries(wb.GetCell(0, "A6", 16));
chart.Axes.HorizontalAxis.AggregationType = AxisAggregationType.Automatic;
// 保存演示文稿
pres.Save("histogram-chart.pptx", SaveFormat.Pptx);
}
以下是创建的直方图的截图。

使用 C# 在 PowerPoint 中创建股票图表
股票图表也是 PowerPoint 演示文稿中常用的图表类型之一。以下是创建股票图表的步骤。
- 使用 Presentation 类创建一个新的 PowerPoint 演示文稿。
- 使用幻灯片的索引在 ISlide 对象中获取幻灯片的引用。
- 将 OpenHighLowClose 图表添加到幻灯片并在 IChart 对象中获取其引用。
- 清除默认系列和类别。
- 访问 IChartDataWorkbook 对象中的图表数据。
- 将新系列和类别添加到图表中。
- 访问每个图表系列并添加数据点。
- 指定 HiLowLines 格式。
- 使用 Presentation.Save(String, SaveFormat) 方法保存演示文稿。
以下代码示例演示如何使用 C# 将股票图表添加到 PowerPoint 演示文稿。
// 加载或创建演示文稿
using (Presentation pres = new Presentation())
{
// 添加图表
IChart chart = pres.Slides[0].Shapes.AddChart(ChartType.OpenHighLowClose, 50, 50, 600, 400, false);
// 清晰的类别和系列
chart.ChartData.Series.Clear();
chart.ChartData.Categories.Clear();
// 访问图表数据工作簿
IChartDataWorkbook wb = chart.ChartData.ChartDataWorkbook;
// 添加类别
chart.ChartData.Categories.Add(wb.GetCell(0, 1, 0, "A"));
chart.ChartData.Categories.Add(wb.GetCell(0, 2, 0, "B"));
chart.ChartData.Categories.Add(wb.GetCell(0, 3, 0, "C"));
// 添加系列
chart.ChartData.Series.Add(wb.GetCell(0, 0, 1, "Open"), chart.Type);
chart.ChartData.Series.Add(wb.GetCell(0, 0, 2, "High"), chart.Type);
chart.ChartData.Series.Add(wb.GetCell(0, 0, 3, "Low"), chart.Type);
chart.ChartData.Series.Add(wb.GetCell(0, 0, 4, "Close"), chart.Type);
// 添加数据点
IChartSeries series = chart.ChartData.Series[0];
series.DataPoints.AddDataPointForStockSeries(wb.GetCell(0, 1, 1, 72));
series.DataPoints.AddDataPointForStockSeries(wb.GetCell(0, 2, 1, 25));
series.DataPoints.AddDataPointForStockSeries(wb.GetCell(0, 3, 1, 38));
series = chart.ChartData.Series[1];
series.DataPoints.AddDataPointForStockSeries(wb.GetCell(0, 1, 2, 172));
series.DataPoints.AddDataPointForStockSeries(wb.GetCell(0, 2, 2, 57));
series.DataPoints.AddDataPointForStockSeries(wb.GetCell(0, 3, 2, 57));
series = chart.ChartData.Series[2];
series.DataPoints.AddDataPointForStockSeries(wb.GetCell(0, 1, 3, 12));
series.DataPoints.AddDataPointForStockSeries(wb.GetCell(0, 2, 3, 12));
series.DataPoints.AddDataPointForStockSeries(wb.GetCell(0, 3, 3, 13));
series = chart.ChartData.Series[3];
series.DataPoints.AddDataPointForStockSeries(wb.GetCell(0, 1, 4, 25));
series.DataPoints.AddDataPointForStockSeries(wb.GetCell(0, 2, 4, 38));
series.DataPoints.AddDataPointForStockSeries(wb.GetCell(0, 3, 4, 50));
// 设置图表是否有上升/下降柱
chart.ChartData.SeriesGroups[0].UpDownBars.HasUpDownBars = true;
// 指定高/低线格式
chart.ChartData.SeriesGroups[0].HiLowLinesFormat.Line.FillFormat.FillType = FillType.Solid;
foreach (IChartSeries ser in chart.ChartData.Series)
{
ser.Format.Line.FillFormat.FillType = FillType.NoFill;
}
// 保存演示文稿
pres.Save("stock-chart.pptx", SaveFormat.Pptx);
}
以下是创建的股票图表的屏幕截图。

更多图表类型
除了上述图表之外,您还可以将其他类型的图表添加到 PowerPoint 演示文稿中。要了解有关支持的图表类型的更多信息,您可以访问 this 文档文章。
获取免费 API 许可证
您可以获得免费的临时许可证 以便在没有评估限制的情况下试用 API。
结论
在本文中,您学习了如何使用 C# 在 PowerPoint 演示文稿中创建图表。此外,分步指南和代码示例展示了如何添加柱形图、散点图、饼图、直方图和股票图。您可以使用 documentation 探索更多关于 Aspose.Slides for .NET 的信息。