在 PowerPoint 演示文稿中创建图表

图表是简洁显示数据的绝佳工具。此外,它们通过直观地表示大量数据,从而更容易消耗大量数据。在展示公司的增长趋势或产品采用率等数据时,在演示文稿中添加图表可能会很有帮助。为此,本文将教您如何使用 C++ 在 PowerPoint 演示文稿中创建图表。

用于在 PowerPoint 演示文稿中创建图表的 C++ API

Aspose.Slides for C++ 是一个原生 C++ 库,支持创建、读取和操作 PowerPoint 文件。 API 还支持在 PowerPoint 演示文稿中创建图表。您可以通过 NuGet 安装 API,也可以直接从 下载 部分下载。

PM> Install-Package Aspose.Slides.Cpp

使用 C++ 在 PowerPoint 演示文稿中创建柱形图

以下是在 PowerPoint 演示文稿中创建柱形图的步骤。

以下是使用 C++ 在 PowerPoint 演示文稿中添加柱形图的示例代码。

// 输出文件路径。
const String outputFilePath = u"OutputDirectory\\column_chart.pptx";

// 实例化表示 PPTX 文件的 Presentation 类
SharedPtr<Presentation> pres = MakeObject<Presentation>();

// 访问第一张幻灯片
SharedPtr<ISlide> slide = pres->get_Slides()->idx_get(0);

// 添加带有默认数据的图表
SharedPtr<IChart> chart = slide->get_Shapes()->AddChart(Aspose::Slides::Charts::ChartType::ClusteredColumn, 0, 0, 500, 500);

// 设置图表数据表的索引
int defaultWorksheetIndex = 0;

// 获取图表数据工作簿
SharedPtr<IChartDataWorkbook> fact = chart->get_ChartData()->get_ChartDataWorkbook();

// 设置图表标题
chart->get_ChartTitle()->AddTextFrameForOverriding(u"Sample Title");
chart->get_ChartTitle()->get_TextFrameForOverriding()->get_TextFrameFormat()->set_CenterText(NullableBool::True);
chart->get_ChartTitle()->set_Height(20);
chart->set_HasTitle(true);

// 删除默认生成的系列和类别
chart->get_ChartData()->get_Series()->Clear();
chart->get_ChartData()->get_Categories()->Clear();
int s = chart->get_ChartData()->get_Series()->get_Count();
s = chart->get_ChartData()->get_Categories()->get_Count();

// 添加系列
chart->get_ChartData()->get_Series()->Add(fact->GetCell(defaultWorksheetIndex, 0, 1, ObjectExt::Box<System::String>(u"Series 1")), chart->get_Type());
chart->get_ChartData()->get_Series()->Add(fact->GetCell(defaultWorksheetIndex, 0, 2, ObjectExt::Box<System::String>(u"Series 2")), chart->get_Type());

// 添加类别
chart->get_ChartData()->get_Categories()->Add(fact->GetCell(defaultWorksheetIndex, 1, 0, ObjectExt::Box<System::String>(u"Category 1")));
chart->get_ChartData()->get_Categories()->Add(fact->GetCell(defaultWorksheetIndex, 2, 0, ObjectExt::Box<System::String>(u"Category 2")));
chart->get_ChartData()->get_Categories()->Add(fact->GetCell(defaultWorksheetIndex, 3, 0, ObjectExt::Box<System::String>(u"Category 3")));

// 获取第一个图表系列
SharedPtr<IChartSeries> series = chart->get_ChartData()->get_Series()->idx_get(0);

// 填充系列数据
series->get_DataPoints()->AddDataPointForBarSeries(fact->GetCell(defaultWorksheetIndex, 1, 1, ObjectExt::Box<double>(20)));
series->get_DataPoints()->AddDataPointForBarSeries(fact->GetCell(defaultWorksheetIndex, 2, 1, ObjectExt::Box<double>(50)));
series->get_DataPoints()->AddDataPointForBarSeries(fact->GetCell(defaultWorksheetIndex, 3, 1, ObjectExt::Box<double>(30)));

// 设置系列的填充颜色
series->get_Format()->get_Fill()->set_FillType(FillType::Solid);
series->get_Format()->get_Fill()->get_SolidFillColor()->set_Color(System::Drawing::Color::get_Blue());

// 采取第二个图表系列
series = chart->get_ChartData()->get_Series()->idx_get(1);

// 填充系列数据
series->get_DataPoints()->AddDataPointForBarSeries(fact->GetCell(defaultWorksheetIndex, 1, 2, ObjectExt::Box<double>(30)));
series->get_DataPoints()->AddDataPointForBarSeries(fact->GetCell(defaultWorksheetIndex, 2, 2, ObjectExt::Box<double>(10)));
series->get_DataPoints()->AddDataPointForBarSeries(fact->GetCell(defaultWorksheetIndex, 3, 2, ObjectExt::Box<double>(60)));

// 设置系列的填充颜色
series->get_Format()->get_Fill()->set_FillType(FillType::Solid);
series->get_Format()->get_Fill()->get_SolidFillColor()->set_Color(System::Drawing::Color::get_Orange());

// 第一个标签将显示类别名称
SharedPtr<IDataLabel> lbl = series->get_DataPoints()->idx_get(0)->get_Label();
lbl->get_DataLabelFormat()->set_ShowCategoryName(true);

lbl = series->get_DataPoints()->idx_get(1)->get_Label();
lbl->get_DataLabelFormat()->set_ShowSeriesName(true);

// 显示第三个标签的值
lbl = series->get_DataPoints()->idx_get(2)->get_Label();
lbl->get_DataLabelFormat()->set_ShowValue(true);
lbl->get_DataLabelFormat()->set_ShowSeriesName(true);
lbl->get_DataLabelFormat()->set_Separator(u"/");

// 保存 PPTX 文件
pres->Save(outputFilePath, Aspose::Slides::Export::SaveFormat::Pptx);

下面是示例代码生成的柱形图的图像。

在 PowerPoint 演示文稿中创建柱形图

使用 C++ 在 PowerPoint 演示文稿中创建饼图

以下是将饼图添加到 PowerPoint 幻灯片的步骤。

以下是使用 C++ 在 PowerPoint 幻灯片中添加饼图的示例代码。

// 输出文件路径。
const String outputFilePath = u"OutputDirectory\\pie_chart.pptx";

// 实例化表示 PPTX 文件的 Presentation 类
SharedPtr<Presentation> pres = MakeObject<Presentation>();

// 访问第一张幻灯片
SharedPtr<ISlide> slide = pres->get_Slides()->idx_get(0);

// 添加带有默认数据的图表
SharedPtr<IChart> chart = slide->get_Shapes()->AddChart(Aspose::Slides::Charts::ChartType::Pie, 0, 0, 500, 500);

// 设置图表标题
chart->get_ChartTitle()->AddTextFrameForOverriding(u"Sample Title");
chart->get_ChartTitle()->get_TextFrameForOverriding()->get_TextFrameFormat()->set_CenterText(NullableBool::True);
chart->get_ChartTitle()->set_Height(20);
chart->set_HasTitle(true);

// 删除默认生成的系列和类别
chart->get_ChartData()->get_Series()->Clear();
chart->get_ChartData()->get_Categories()->Clear();

// 设置图表数据表的索引
int defaultWorksheetIndex = 0;

// 获取图表数据工作簿
SharedPtr<IChartDataWorkbook> fact = chart->get_ChartData()->get_ChartDataWorkbook();

// 添加类别
chart->get_ChartData()->get_Categories()->Add(fact->GetCell(defaultWorksheetIndex, 1, 0, ObjectExt::Box<System::String>(u"First Qtr")));
chart->get_ChartData()->get_Categories()->Add(fact->GetCell(defaultWorksheetIndex, 2, 0, ObjectExt::Box<System::String>(u"2nd Qtr")));
chart->get_ChartData()->get_Categories()->Add(fact->GetCell(defaultWorksheetIndex, 3, 0, ObjectExt::Box<System::String>(u"3rd Qtr")));

// 添加系列
chart->get_ChartData()->get_Series()->Add(fact->GetCell(defaultWorksheetIndex, 0, 1, ObjectExt::Box<System::String>(u"Series 1")), chart->get_Type());

// 获取第一个图表系列
SharedPtr<IChartSeries> series = chart->get_ChartData()->get_Series()->idx_get(0);

// 填充系列数据
series->get_DataPoints()->AddDataPointForPieSeries(fact->GetCell(defaultWorksheetIndex, 1, 1, ObjectExt::Box<double>(20)));
series->get_DataPoints()->AddDataPointForPieSeries(fact->GetCell(defaultWorksheetIndex, 2, 1, ObjectExt::Box<double>(50)));
series->get_DataPoints()->AddDataPointForPieSeries(fact->GetCell(defaultWorksheetIndex, 3, 1, ObjectExt::Box<double>(30)));

chart->get_ChartData()->get_SeriesGroups()->idx_get(0)->set_IsColorVaried(true);

SharedPtr<IChartDataPoint> point = series->get_DataPoints()->idx_get(0);
point->get_Format()->get_Fill()->set_FillType(FillType::Solid);
point->get_Format()->get_Fill()->get_SolidFillColor()->set_Color(System::Drawing::Color::get_Orange());

// 设置扇区边界
point->get_Format()->get_Line()->get_FillFormat()->set_FillType(FillType::Solid);
point->get_Format()->get_Line()->get_FillFormat()->get_SolidFillColor()->set_Color(System::Drawing::Color::get_Gray());
point->get_Format()->get_Line()->set_Width(3.0);

SharedPtr<IChartDataPoint> point1 = series->get_DataPoints()->idx_get(1);
point1->get_Format()->get_Fill()->set_FillType(FillType::Solid);
point1->get_Format()->get_Fill()->get_SolidFillColor()->set_Color(System::Drawing::Color::get_BlueViolet());

// 设置扇区边界
point1->get_Format()->get_Line()->get_FillFormat()->set_FillType(FillType::Solid);
point1->get_Format()->get_Line()->get_FillFormat()->get_SolidFillColor()->set_Color(System::Drawing::Color::get_Blue());
point1->get_Format()->get_Line()->set_Width(3.0);

SharedPtr<IChartDataPoint> point2 = series->get_DataPoints()->idx_get(2);
point2->get_Format()->get_Fill()->set_FillType(FillType::Solid);
point2->get_Format()->get_Fill()->get_SolidFillColor()->set_Color(System::Drawing::Color::get_YellowGreen());

// 设置扇区边界
point2->get_Format()->get_Line()->get_FillFormat()->set_FillType(FillType::Solid);
point2->get_Format()->get_Line()->get_FillFormat()->get_SolidFillColor()->set_Color(System::Drawing::Color::get_Red());
point2->get_Format()->get_Line()->set_Width(2.0);

// 为系列中的每个类别创建自定义标签
SharedPtr<IDataLabel> lbl1 = series->get_DataPoints()->idx_get(0)->get_Label();

// lbl.ShowCategoryName = true;
lbl1->get_DataLabelFormat()->set_ShowValue(true);

SharedPtr<IDataLabel> lbl2 = series->get_DataPoints()->idx_get(1)->get_Label();
lbl2->get_DataLabelFormat()->set_ShowValue(true);
lbl2->get_DataLabelFormat()->set_ShowLegendKey(true);
lbl2->get_DataLabelFormat()->set_ShowPercentage(true);

SharedPtr<IDataLabel> lbl3 = series->get_DataPoints()->idx_get(2)->get_Label();

lbl3->get_DataLabelFormat()->set_ShowSeriesName(true);
lbl3->get_DataLabelFormat()->set_ShowPercentage(true);

// 显示图表的引导线
series->get_Labels()->get_DefaultDataLabelFormat()->set_ShowLeaderLines(true);

// 设置饼图扇区的旋转角度
chart->get_ChartData()->get_SeriesGroups()->idx_get(0)->set_FirstSliceAngle(180);

// 保存 PPTX 文件
pres->Save(outputFilePath, Aspose::Slides::Export::SaveFormat::Pptx);

下面是示例代码生成的饼图的图像。

在 PowerPoint 演示文稿中创建饼图

使用 C++ 在 PowerPoint 演示文稿中创建散点图

以下是将散点图添加到 PowerPoint 幻灯片的步骤。

以下是使用 C++ 向 PowerPoint 幻灯片添加散点图的示例代码。

// 输出文件路径。
const String outputFilePath = u"OutputDirectory\\scattered_chart.pptx";

// 实例化表示 PPTX 文件的 Presentation 类
SharedPtr<Presentation> pres = MakeObject<Presentation>();

// 访问第一张幻灯片
SharedPtr<ISlide> slide = pres->get_Slides()->idx_get(0);

// 添加带有默认数据的图表
SharedPtr<IChart> chart = slide->get_Shapes()->AddChart(Aspose::Slides::Charts::ChartType::ScatterWithSmoothLines, 0, 0, 500, 500);

// 删除默认生成的系列 
chart->get_ChartData()->get_Series()->Clear();

// 设置图表数据表的索引
int defaultWorksheetIndex = 0;

// 获取图表数据工作簿
SharedPtr<IChartDataWorkbook> fact = chart->get_ChartData()->get_ChartDataWorkbook();

// 添加系列
chart->get_ChartData()->get_Series()->Add(fact->GetCell(defaultWorksheetIndex, 1, 1, ObjectExt::Box<System::String>(u"Series 1")), chart->get_Type());
chart->get_ChartData()->get_Series()->Add(fact->GetCell(defaultWorksheetIndex, 1, 3, ObjectExt::Box<System::String>(u"Series 2")), chart->get_Type());

// 获取第一个图表系列
SharedPtr<IChartSeries> series = chart->get_ChartData()->get_Series()->idx_get(0);

// 在那里添加新点 (1:3)。
series->get_DataPoints()->AddDataPointForScatterSeries(fact->GetCell(defaultWorksheetIndex, 2, 1, ObjectExt::Box<double>(1)), fact->GetCell(defaultWorksheetIndex, 2, 2, ObjectExt::Box<double>(3)));

// 添加新点 (2:10)
series->get_DataPoints()->AddDataPointForScatterSeries(fact->GetCell(defaultWorksheetIndex, 3, 1, ObjectExt::Box<double>(2)), fact->GetCell(defaultWorksheetIndex, 3, 2, ObjectExt::Box<double>(10)));

// 编辑系列类型
series->set_Type(ChartType::ScatterWithStraightLinesAndMarkers);

// 更改图表系列标记
series->get_Marker()->set_Size(10);
series->get_Marker()->set_Symbol(MarkerStyleType::Star);

// 采取第二个图表系列
series = chart->get_ChartData()->get_Series()->idx_get(1);

// 在那里添加新点 (5:2)。
series->get_DataPoints()->AddDataPointForScatterSeries(fact->GetCell(defaultWorksheetIndex, 2, 3, ObjectExt::Box<double>(5)), fact->GetCell(defaultWorksheetIndex, 2, 4, ObjectExt::Box<double>(2)));

// 添加新点 (3:1)
series->get_DataPoints()->AddDataPointForScatterSeries(fact->GetCell(defaultWorksheetIndex, 3, 3, ObjectExt::Box<double>(3)), fact->GetCell(defaultWorksheetIndex, 3, 4, ObjectExt::Box<double>(1)));

// 添加新点 (2:2)
series->get_DataPoints()->AddDataPointForScatterSeries(fact->GetCell(defaultWorksheetIndex, 4, 3, ObjectExt::Box<double>(2)), fact->GetCell(defaultWorksheetIndex, 4, 4, ObjectExt::Box<double>(2)));

// 添加新点 (5:1)
series->get_DataPoints()->AddDataPointForScatterSeries(fact->GetCell(defaultWorksheetIndex, 5, 3, ObjectExt::Box<double>(5)), fact->GetCell(defaultWorksheetIndex, 5, 4, ObjectExt::Box<double>(1)));

// 更改图表系列标记
series->get_Marker()->set_Size(10);
series->get_Marker()->set_Symbol(MarkerStyleType::Circle);

// 保存 PPTX 文件
pres->Save(outputFilePath, Aspose::Slides::Export::SaveFormat::Pptx);

下面是示例代码生成的散点图的图像。

在 PowerPoint 演示文稿中创建散点图

在 PowerPoint 演示文稿中创建直方图

以下是在 PowerPoint 演示文稿中创建直方图的步骤。

以下是使用 C++ 在 PowerPoint 演示文稿中创建直方图的示例代码。

// 输出文件路径。
const String outputFilePath = u"OutputDirectory\\histogram_chart.pptx";

// 实例化表示 PPTX 文件的 Presentation 类
SharedPtr<Presentation> pres = MakeObject<Presentation>();

// 访问第一张幻灯片
SharedPtr<ISlide> slide = pres->get_Slides()->idx_get(0);

// 添加带有默认数据的图表
System::SharedPtr<IChart> chart = slide->get_Shapes()->AddChart(Aspose::Slides::Charts::ChartType::Histogram, 50, 50, 500, 400);

// 删除默认生成的系列和类别
chart->get_ChartData()->get_Categories()->Clear();
chart->get_ChartData()->get_Series()->Clear();

// 获取图表数据工作簿
System::SharedPtr<IChartDataWorkbook> wb = chart->get_ChartData()->get_ChartDataWorkbook();

wb->Clear(0);

// 添加系列
System::SharedPtr<IChartSeries> series = chart->get_ChartData()->get_Series()->Add(Aspose::Slides::Charts::ChartType::Histogram);

// 填充系列数据
series->get_DataPoints()->AddDataPointForHistogramSeries(wb->GetCell(0, u"A1", System::ObjectExt::Box<int32_t>(15)));
series->get_DataPoints()->AddDataPointForHistogramSeries(wb->GetCell(0, u"A2", System::ObjectExt::Box<int32_t>(-41)));
series->get_DataPoints()->AddDataPointForHistogramSeries(wb->GetCell(0, u"A3", System::ObjectExt::Box<int32_t>(16)));
series->get_DataPoints()->AddDataPointForHistogramSeries(wb->GetCell(0, u"A4", System::ObjectExt::Box<int32_t>(10)));
series->get_DataPoints()->AddDataPointForHistogramSeries(wb->GetCell(0, u"A5", System::ObjectExt::Box<int32_t>(-23)));
series->get_DataPoints()->AddDataPointForHistogramSeries(wb->GetCell(0, u"A6", System::ObjectExt::Box<int32_t>(16)));

// 设置轴聚合类型
chart->get_Axes()->get_HorizontalAxis()->set_AggregationType(Aspose::Slides::Charts::AxisAggregationType::Automatic);

// 保存 PPTX 文件
pres->Save(outputFilePath, Aspose::Slides::Export::SaveFormat::Pptx);

下面是示例代码生成的直方图的图像。

在 PowerPoint 演示文稿中创建直方图

其他支持的图表

除了上面显示的图表,Aspose.Slides for C++ 支持更多的图表类型。您可以通过阅读 this 文档文章来查看带有示例代码的支持图表类型的完整列表。

获得免费许可证

您可以请求 免费的临时许可证 试用 API,而不受评估限制。

结论

在本文中,您学习了如何使用 C++ 在 PowerPoint 幻灯片中添加图表。具体来说,您已经学习了如何在 PowerPoint 演示文稿中添加柱形图、散点图、饼图和直方图。此外,您已经看到 Aspose.Slides for C++ API 提供了更多图表类型供您在 PowerPoint 演示文稿中使用。除了图表,API 还提供了一系列增强 PowerPoint 演示文稿的功能。您可以使用 官方文档 详细探索 API。如有任何问题,请随时在 免费支持论坛 上与我们联系。

也可以看看