彩票走势图

PPT处理控件Aspose.Slides功能演示:使用C ++在PowerPoint演示文稿中创建图表

翻译|使用教程|编辑:李显亮|2021-04-08 10:41:25.563|阅读 172 次

概述:在演示诸如公司的增长趋势或产品采用率之类的数据时,向演示文稿中添加图表可能会有所帮助。为此,本文将教您如何 使用C ++在PowerPoint演示文稿中创建图表。

# 慧都年终大促·界面/图表报表/文档/IDE等千款热门软控件火热促销中 >>

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

PPT处理控件Aspose.Slides功能演示:使用C ++在PowerPoint演示文稿中创建图表

  • 使用C ++在PowerPoint演示文稿中创建柱形图
  • 使用C ++在PowerPoint演示文稿中创建饼图
  • 使用C ++在PowerPoint演示文稿中创建散点图
  • 在PowerPoint演示文稿中创建直方图图表

Aspose.Slides for C ++ 是本机C ++库,支持创建,读取和操作PowerPoint文件。该API还支持在PowerPoint演示文稿中创建图表。您可以点击下方按钮下载体验。

下载最新版Aspose.Slides

整合所有格式API处理套包Aspose.Slides正在慧都网火热销售中,新购优惠折上折!立马1分钟了解全部咨询!

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

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

  • 首先,创建Presentation 类的实例。
  • 使用Presentation-> get_Slides()-> idx_get(int32_t索引)访问要在其上添加柱形图的幻灯片。
  • 使用ISlide-> get_Shapes()-> AddChart(Charts :: ChartType类型,float x,float y,float宽度,float高度)方法将ClusteredColumn图表添加到幻灯片中。
  • 使用IChart-> get_ChartData()-> get_ChartDataWorkbook()方法访问图表数据工作簿。
  • 使用IChart-> get_ChartTitle()-> AddTextFrameForOverriding(System :: String文本)方法设置图表标题。
  • 分别使用IChart-> get_ChartData()-> get_Series()-> Clear()和IChart-> get_ChartData()-> get_Categories()-> Clear()方法从图表数据中清除默认系列和类别。
  • 使用IChart-> get_ChartData()-> get_Series()-> Add(System :: SharedPtrcellWithSeriesName,ChartType type)和IChart-> get_ChartData()-> get_Categories()-> Add(System)添加新系列和类别:: SharedPtrchartDataCell)方法。
  • 使用IChart-> get_ChartData()-> get_Series()->; idx_get(int32_t索引)方法访问每个系列。
  • 为每个系列添加数据点,填充颜色和标签。
  • 最后,使用Presentation-> Save(系统::字符串名称,导出:: SaveFormat格式)方法保存包含柱形图的演示文稿。

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

// Output File Path.
const String outputFilePath = u"OutputDirectory\\column_chart.pptx";

// Instantiate Presentation class that represents PPTX file
SharedPtrpres = MakeObject();

// Access first slide
SharedPtrslide = pres->get_Slides()->idx_get(0);

// Add chart with default data
SharedPtrchart = slide->get_Shapes()->AddChart(Aspose::Slides::Charts::ChartType::ClusteredColumn, 0, 0, 500, 500);

// Setting the index of chart data sheet
int defaultWorksheetIndex = 0;

// Getting the chart data workbook
SharedPtrfact = chart->get_ChartData()->get_ChartDataWorkbook();

// Setting 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 default generated series and categories
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();

// Add series
chart->get_ChartData()->get_Series()->Add(fact->GetCell(defaultWorksheetIndex, 0, 1, ObjectExt::Box(u"Series 1")), chart->get_Type());
chart->get_ChartData()->get_Series()->Add(fact->GetCell(defaultWorksheetIndex, 0, 2, ObjectExt::Box(u"Series 2")), chart->get_Type());

// Add categories
chart->get_ChartData()->get_Categories()->Add(fact->GetCell(defaultWorksheetIndex, 1, 0, ObjectExt::Box(u"Category 1")));
chart->get_ChartData()->get_Categories()->Add(fact->GetCell(defaultWorksheetIndex, 2, 0, ObjectExt::Box(u"Category 2")));
chart->get_ChartData()->get_Categories()->Add(fact->GetCell(defaultWorksheetIndex, 3, 0, ObjectExt::Box(u"Category 3")));

// Take first chart series
SharedPtrseries = chart->get_ChartData()->get_Series()->idx_get(0);

// Populate series data
series->get_DataPoints()->AddDataPointForBarSeries(fact->GetCell(defaultWorksheetIndex, 1, 1, ObjectExt::Box(20)));
series->get_DataPoints()->AddDataPointForBarSeries(fact->GetCell(defaultWorksheetIndex, 2, 1, ObjectExt::Box(50)));
series->get_DataPoints()->AddDataPointForBarSeries(fact->GetCell(defaultWorksheetIndex, 3, 1, ObjectExt::Box(30)));

// Setting fill color for series
series->get_Format()->get_Fill()->set_FillType(FillType::Solid);
series->get_Format()->get_Fill()->get_SolidFillColor()->set_Color(System::Drawing::Color::get_Blue());

// Take second chart series
series = chart->get_ChartData()->get_Series()->idx_get(1);

// Populate series data
series->get_DataPoints()->AddDataPointForBarSeries(fact->GetCell(defaultWorksheetIndex, 1, 2, ObjectExt::Box(30)));
series->get_DataPoints()->AddDataPointForBarSeries(fact->GetCell(defaultWorksheetIndex, 2, 2, ObjectExt::Box(10)));
series->get_DataPoints()->AddDataPointForBarSeries(fact->GetCell(defaultWorksheetIndex, 3, 2, ObjectExt::Box(60)));

// Setting fill color for series
series->get_Format()->get_Fill()->set_FillType(FillType::Solid);
series->get_Format()->get_Fill()->get_SolidFillColor()->set_Color(System::Drawing::Color::get_Orange());

// First label will be show Category name
SharedPtrlbl = 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);

// Show value for third label
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"/");

// Save PPTX file
pres->Save(outputFilePath, Aspose::Slides::Export::SaveFormat::Pptx);

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

PPT处理控件Aspose.Slides功能演示:使用C ++在PowerPoint演示文稿中创建图表

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

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

  • 首先,创建Presentation 类的实例
  • 使用Presentation-> get_Slides()-> idx_get(int32_t索引)访问要在其上添加饼图的幻灯片
  • 使用ISlide-> get_Shapes()-> AddChart(图表:: ChartType类型,float x,float y,float宽度,float高度)方法饼图添加到幻灯片中
  • 使用IChart-> get_ChartTitle()-> AddTextFrameForOverriding(System :: String文本)方法设置图表标题
  • 分别使用IChart-> get_ChartData()-> get_Series()-> Clear()IChart-> get_ChartData()-> get_Categories()-> Clear()方法从图表数据中清除默认系列和类别
  • 使用IChart-> get_ChartData()-> get_ChartDataWorkbook()方法访问图表数据工作簿
  • 使用IChart-> get_ChartData()-> get_Series()-> Add(System :: SharedPtr <IChartDataCell> cellWithSeriesName,ChartType type)IChart-> get_ChartData()-> get_Categories()-> Add(System)添加新系列和类别:: SharedPtr &lt;IChartDataCell> chartDataCell)方法。
  • 使用IChart-> get_ChartData()-> get_Series()-> idx_get(int32_t索引)方法访问每个系列
  • 使用IChartSeries-> get_DataPoints()-> AddDataPointForPieSeries(System :: SharedPtr <IChartDataCell>值)方法添加数据点
  • 格式化数据点,添加引出线,并设置旋转角度。
  • 最后,使用Presentation-> Save(系统::字符串名称,导出:: SaveFormat格式)方法保存包含饼图的演示文稿

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

// Output File Path.
const String outputFilePath = u"OutputDirectory\\pie_chart.pptx";

// Instantiate Presentation class that represents PPTX file
SharedPtr<Presentation> pres = MakeObject<Presentation>();

// Access first slide
SharedPtr<ISlide> slide = pres->get_Slides()->idx_get(0);

// Add chart with default data
SharedPtr<IChart> chart = slide->get_Shapes()->AddChart(Aspose::Slides::Charts::ChartType::Pie, 0, 0, 500, 500);

// Setting 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 default generated series and categories
chart->get_ChartData()->get_Series()->Clear();
chart->get_ChartData()->get_Categories()->Clear();

// Setting the index of chart data sheet
int defaultWorksheetIndex = 0;

// Getting the chart data workbook
SharedPtr<IChartDataWorkbook> fact = chart->get_ChartData()->get_ChartDataWorkbook();

// Add categories
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")));

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

// Take first chart series
SharedPtr<IChartSeries> series = chart->get_ChartData()->get_Series()->idx_get(0);

// Populate series data
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());

// Setting Sector border
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());

// Setting Sector border
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());

// Setting Sector border
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);

// Create custom labels for each category in the series
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);

// Showing Leader Lines for Chart
series->get_Labels()->get_DefaultDataLabelFormat()->set_ShowLeaderLines(true);

// Setting Rotation Angle for Pie Chart Sectors
chart->get_ChartData()->get_SeriesGroups()->idx_get(0)->set_FirstSliceAngle(180);

// Save PPTX file
pres->Save(outputFilePath, Aspose::Slides::Export::SaveFormat::Pptx);

PPT处理控件Aspose.Slides功能演示:使用C ++在PowerPoint演示文稿中创建图表

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

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

  • 首先,创建一个Presentation类的实例。
  • 使用Presentation->get_Slides()->idx_get (int32_t index)访问要添加散点图的幻灯片。
  • 使用ISlide->get_Shapes()->AddChart (Charts::ChartType type, float x, float y, float width, float height)方法将ScatterWithSmoothLines图表添加到幻灯片上。
  • 使用 IChart->get_ChartData()->get_Series()->Clear()方法清除图表数据中的默认系列。
  • 使用 IChart->get_ChartData()-&gt;get_ChartDataWorkbook()方法访问图表数据工作簿。
  • 使用 IChart->get_ChartData()->get_Series()->Add (System::SharedPtr<IChartDataCell> cellWithSeriesName, ChartType type)方法添加新的系列。
  • 使用 IChart->get_ChartData()->get_Series()->idx_get (int32_t index) 方法访问每个系列。
  • 使用 IChartSeries->get_DataPoints()->AddDataPointForScatterSeries (System::SharedPtr<IChartDataCell> xValue, System::SharedPtr<IChartDataCell> yValue) 方法添加数据点。
  • 为该系列设置标记。
  • 最后,使用Presentation->Save(System::String name, Export::SaveFormat format)方法保存包含散点图的演示文稿。

以下是使用C ++将分散的图表添加到PowerPoint幻灯片的示例代码。

// Output File Path.
const String outputFilePath = u"OutputDirectory\\scattered_chart.pptx";

// Instantiate Presentation class that represents PPTX file
SharedPtr<Presentation> pres = MakeObject<Presentation>();

// Access first slide
SharedPtr<ISlide> slide = pres->get_Slides()->idx_get(0);

// Add chart with default data
SharedPtr<IChart> chart = slide->get_Shapes()->AddChart(Aspose::Slides::Charts::ChartType::ScatterWithSmoothLines, 0, 0, 500, 500);

// Delete default generated series 
chart->get_ChartData()->get_Series()->Clear();

// Setting the index of chart data sheet
int defaultWorksheetIndex = 0;

// Getting the chart data workbook
SharedPtr<IChartDataWorkbook> fact = chart->get_ChartData()->get_ChartDataWorkbook();

// Add series
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());

// Take first chart series
SharedPtr<IChartSeries> series = chart->get_ChartData()->get_Series()->idx_get(0);

// Add new point (1:3) there.
series->get_DataPoints()->AddDataPointForScatterSeries(fact->GetCell(defaultWorksheetIndex, 2, 1, ObjectExt::Box<double>(1)), fact->GetCell(defaultWorksheetIndex, 2, 2, ObjectExt::Box<double>(3)));

// Add new point (2:10)
series->get_DataPoints()->AddDataPointForScatterSeries(fact->GetCell(defaultWorksheetIndex, 3, 1, ObjectExt::Box<double>(2)), fact->GetCell(defaultWorksheetIndex, 3, 2, ObjectExt::Box<double>(10)));

// Edit the type of series
series->set_Type(ChartType::ScatterWithStraightLinesAndMarkers);

// Changing the chart series marker
series->get_Marker()->set_Size(10);
series->get_Marker()->set_Symbol(MarkerStyleType::Star);

// Take second chart series
series = chart->get_ChartData()->get_Series()->idx_get(1);

// Add new point (5:2) there.
series->get_DataPoints()->AddDataPointForScatterSeries(fact->GetCell(defaultWorksheetIndex, 2, 3, ObjectExt::Box<double>(5)), fact->GetCell(defaultWorksheetIndex, 2, 4, ObjectExt::Box<double>(2)));

// Add new point (3:1)
series->get_DataPoints()->AddDataPointForScatterSeries(fact->GetCell(defaultWorksheetIndex, 3, 3, ObjectExt::Box<double>(3)), fact->GetCell(defaultWorksheetIndex, 3, 4, ObjectExt::Box<double>(1)));

// Add new point (2:2)
series->get_DataPoints()->AddDataPointForScatterSeries(fact->GetCell(defaultWorksheetIndex, 4, 3, ObjectExt::Box<double>(2)), fact->GetCell(defaultWorksheetIndex, 4, 4, ObjectExt::Box<double>(2)));

// Add new point (5:1)
series->get_DataPoints()->AddDataPointForScatterSeries(fact->GetCell(defaultWorksheetIndex, 5, 3, ObjectExt::Box<double>(5)), fact->GetCell(defaultWorksheetIndex, 5, 4, ObjectExt::Box<double>(1)));

// Changing the chart series marker
series->get_Marker()->set_Size(10);
series->get_Marker()->set_Symbol(MarkerStyleType::Circle);

// Save PPTX file
pres->Save(outputFilePath, Aspose::Slides::Export::SaveFormat::Pptx);

PPT处理控件Aspose.Slides功能演示:使用C ++在PowerPoint演示文稿中创建图表

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

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

  • 首先,创建一个Presentation类的实例。
  • 使用Presentation->get_Slides()->idx_get (int32_t index)访问要添加直方图图表的幻灯片。
  • 使用ISlide->;get_Shapes()->AddChart (Charts::ChartType type, float x, float y, float width, float height)方法向幻灯片添加直方图。
  • 分别使用 IChart->get_ChartData()->get_Series()->Clear()和 IChart->get_ChartData()->get_Categories()->Clear()方法清除图表数据中默认的系列和类别。
  • 使用 IChart->get_ChartData()->get_ChartDataWorkbook()方法访问图表数据工作簿。
  • 使用 IChart->get_ChartData()->get_Series()->Add(ChartType type)方法添加新的系列。
  • 使用 IChartSeries->get_DataPoints()->AddDataPointForHistogramSeries(System::SharedPtr<IChartDataCell>值)方法添加数据点。
  • 使用IChart->get_Axes()->get_HorizontalAxis()->set_AggregationType(AxisAggregationType值)方法设置图表轴的聚合类型。
  • 最后,使用Presentation->Save(System::String name, Export::SaveFormat format)方法保存包含直方图的演示文稿。

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

// Output File Path.
const String outputFilePath = u"OutputDirectory\\histogram_chart.pptx";

// Instantiate Presentation class that represents PPTX file
SharedPtr<Presentation> pres = MakeObject<Presentation>();

// Access first slide
SharedPtr<ISlide> slide = pres->get_Slides()->idx_get(0);

// Add chart with default data
System::SharedPtr<IChart> chart = slide->get_Shapes()->AddChart(Aspose::Slides::Charts::ChartType::Histogram, 50, 50, 500, 400);

// Delete default generated series and categories
chart->get_ChartData()->get_Categories()->Clear();
chart->get_ChartData()->get_Series()->Clear();

// Getting the chart data workbook
System::SharedPtr<IChartDataWorkbook> wb = chart->get_ChartData()->get_ChartDataWorkbook();

wb->Clear(0);

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

// Populate series data
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)));

// Set axis aggregation type
chart->get_Axes()->get_HorizontalAxis()->set_AggregationType(Aspose::Slides::Charts::AxisAggregationType::Automatic);

// Save PPTX file
pres->Save(outputFilePath, Aspose::Slides::Export::SaveFormat::Pptx);

PPT处理控件Aspose.Slides功能演示:使用C ++在PowerPoint演示文稿中创建图表

如果你想试用Aspose的全部完整功能,可联系在线客服获取30天临时授权体验。


还想要更多吗?您可以点击阅读【2020 · Aspose最新资源整合】查找需要的教程资源。如果您有任何疑问或需求,请随时加入Aspose技术交流群(761297826),我们很高兴为您提供查询和咨询

标签:

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@pclwef.cn


为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP