使用 C++ 在 PPT 演示文稿中创建图表
无需 Microsoft PowerPoint,使用服务器端 Aspose.Slides for C++ API 在 PPT 演示文稿中创建图表。
使用 C++ 创建 PPT 图表
使用 Aspose.Slides for C++ 在 PPT 演示文稿中创建图表。从 NuGet 安装软件包,或在程序包管理器控制台中使用以下命令。
命令
PM> Install-Package Aspose.Slides.Cpp在 PPT 中创建图表 – C++
auto presentation = MakeObject<Presentation>();
auto slide = presentation->get_Slide(0);
// Add a chart with default data.
auto chart = slide->get_Shapes()->AddChart(ChartType::ClusteredColumn, 0, 0, 500, 500);
// Set the index of the chart data sheet.
int defaultWorksheetIndex = 0;
// Get the chart data workbook.
auto chartDataWorkbook = chart->get_ChartData()->get_ChartDataWorkbook();
// Set the chart title.
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);
// Delete the default generated series and categories.
chart->get_ChartData()->get_Series()->Clear();
chart->get_ChartData()->get_Categories()->Clear();
// Add series.
chart->get_ChartData()->get_Series()->Add(chartDataWorkbook->GetCell(defaultWorksheetIndex, 0, 1, ObjectExt::Box(String(u"Series 1"))), chart->get_Type());
chart->get_ChartData()->get_Series()->Add(chartDataWorkbook->GetCell(defaultWorksheetIndex, 0, 2, ObjectExt::Box(String(u"Series 2"))), chart->get_Type());
// Add categories.
chart->get_ChartData()->get_Categories()->Add(chartDataWorkbook->GetCell(defaultWorksheetIndex, 1, 0, ObjectExt::Box(String(u"Category 1"))));
chart->get_ChartData()->get_Categories()->Add(chartDataWorkbook->GetCell(defaultWorksheetIndex, 2, 0, ObjectExt::Box(String(u"Category 2"))));
chart->get_ChartData()->get_Categories()->Add(chartDataWorkbook->GetCell(defaultWorksheetIndex, 3, 0, ObjectExt::Box(String(u"Category 3"))));
// Take the first chart series.
auto series = chart->get_ChartData()->get_Series()->idx_get(0);
// Populate series data.
series->get_DataPoints()->AddDataPointForBarSeries(chartDataWorkbook->GetCell(defaultWorksheetIndex, 1, 1, ObjectExt::Box(20)));
series->get_DataPoints()->AddDataPointForBarSeries(chartDataWorkbook->GetCell(defaultWorksheetIndex, 2, 1, ObjectExt::Box(50)));
series->get_DataPoints()->AddDataPointForBarSeries(chartDataWorkbook->GetCell(defaultWorksheetIndex, 3, 1, ObjectExt::Box(30)));
// Set the fill color for the series.
series->get_Format()->get_Fill()->set_FillType(FillType::Solid);
series->get_Format()->get_Fill()->get_SolidFillColor()->set_Color(Color::get_Blue());
// Take the second chart series.
series = chart->get_ChartData()->get_Series()->idx_get(1);
// Populate series data.
series->get_DataPoints()->AddDataPointForBarSeries(chartDataWorkbook->GetCell(defaultWorksheetIndex, 1, 2, ObjectExt::Box(30)));
series->get_DataPoints()->AddDataPointForBarSeries(chartDataWorkbook->GetCell(defaultWorksheetIndex, 2, 2, ObjectExt::Box(10)));
series->get_DataPoints()->AddDataPointForBarSeries(chartDataWorkbook->GetCell(defaultWorksheetIndex, 3, 2, ObjectExt::Box(60)));
// Set the fill color for the series.
series->get_Format()->get_Fill()->set_FillType(FillType::Solid);
series->get_Format()->get_Fill()->get_SolidFillColor()->set_Color(Color::get_Orange());
// Show the category name for the first label.
auto dataLabel = series->get_DataPoints()->idx_get(0)->get_Label();
dataLabel->get_DataLabelFormat()->set_ShowCategoryName(true);
dataLabel = series->get_DataPoints()->idx_get(1)->get_Label();
dataLabel->get_DataLabelFormat()->set_ShowSeriesName(true);
// Show the value for the third label.
dataLabel = series->get_DataPoints()->idx_get(2)->get_Label();
dataLabel->get_DataLabelFormat()->set_ShowValue(true);
dataLabel->get_DataLabelFormat()->set_ShowSeriesName(true);
dataLabel->get_DataLabelFormat()->set_Separator(u"/");
// Save the PPT file.
presentation->Save(u"column_chart.ppt", SaveFormat::Ppt);
presentation->Dispose();如何使用 C++ 在 PPT 文件中创建图表
使用 Aspose.Slides for C++,只需几行代码即可在 PPT 演示文稿中创建图表。
- 创建
Presentation对象。 - 访问第一张幻灯片。
- 添加带有默认数据的图表。
- 设置图表数据工作表的索引。
- 获取图表数据工作簿。
- 设置图表标题。
- 删除默认生成的系列和类别。
- 添加系列和类别。
- 获取第一个图表系列并填充其数据。
- 设置系列的填充颜色。
- 在第二个系列的数据标签中显示类别名称、系列名称和值。
- 保存 PPT 文件。
系统要求
Aspose.Slides for C++ 支持主要的平台和操作系统。请确保您的环境满足以下先决条件。
- 装有受支持 C++ 运行时的 Microsoft Windows、Linux、macOS 或其他兼容操作系统。
- 在项目中引用 Aspose.Slides for C++ 库。
基于 Aspose.Slides 构建
在线创建 PPT 图表
免费的 Aspose.Slides Chart 应用基于 Aspose.Slides for C++ 构建,与上方代码使用的是同一 API。打开它,看看 API 能如何处理您自己的 PPT 文件。
- 在浏览器中运行,无需安装任何软件。
- 在编写代码之前,先了解 Aspose.Slides 如何创建图表。
- 根据 Excel 或表格数据创建图表。
- 将其下载为 PPT 文件或图像。
什么是PPT文件格式
PPT 文件包含用于幻灯片放映的一组幻灯片。它使用 Microsoft PowerPoint 97-2003 的二进制文件格式。PPT 文件可以包含文本、项目符号、图像、多媒体和其他嵌入的 OLE 对象。Microsoft 在 PowerPoint 2007 中推出了较新的 PPTX 格式。PPTX 基于 Office Open XML,与较旧的二进制格式不同。OpenOffice Impress 和 Apple Keynote 等其他应用程序也可以创建 PPT 文件。
图表支持的其他格式
使用 C++,您还可以在其他格式中创建图表。