- 使用C ++在PowerPoint演示文稿中创建柱形图
- 使用C ++在PowerPoint演示文稿中创建饼图
- 使用C ++在PowerPoint演示文稿中创建散点图
- 在PowerPoint演示文稿中创建直方图图表
Aspose.Slides for C ++ 是本机C ++库,支持创建,读取和操作PowerPoint文件。该API还支持在PowerPoint演示文稿中创建图表。您可以点击下方按钮下载体验。
整合所有格式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 fileSharedPtrpres = MakeObject();// Access first slideSharedPtrslide = pres->get_Slides()->idx_get(0);// Add chart with default dataSharedPtrchart = slide->get_Shapes()->AddChart(Aspose::Slides::Charts::ChartType::ClusteredColumn, 0, 0, 500, 500);// Setting the index of chart data sheetint defaultWorksheetIndex = 0;// Getting the chart data workbookSharedPtrfact = chart->get_ChartData()->get_ChartDataWorkbook();// Setting chart Titlechart->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 categorieschart->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 serieschart->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 categorieschart->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 seriesSharedPtrseries = chart->get_ChartData()->get_Series()->idx_get(0);// Populate series dataseries->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 seriesseries->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 seriesseries = chart->get_ChartData()->get_Series()->idx_get(1);// Populate series dataseries->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 seriesseries->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 nameSharedPtrlbl = 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 labellbl = 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 filepres->Save(outputFilePath, Aspose::Slides::Export::SaveFormat::Pptx);
下面是示例代码生成的柱形图的图像。
使用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 <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 fileSharedPtr<Presentation> pres = MakeObject<Presentation>();// Access first slideSharedPtr<ISlide> slide = pres->get_Slides()->idx_get(0);// Add chart with default dataSharedPtr<IChart> chart = slide->get_Shapes()->AddChart(Aspose::Slides::Charts::ChartType::Pie, 0, 0, 500, 500);// Setting chart Titlechart->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 categorieschart->get_ChartData()->get_Series()->Clear();chart->get_ChartData()->get_Categories()->Clear();// Setting the index of chart data sheetint defaultWorksheetIndex = 0;// Getting the chart data workbookSharedPtr<IChartDataWorkbook> fact = chart->get_ChartData()->get_ChartDataWorkbook();// Add categorieschart->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 serieschart->get_ChartData()->get_Series()->Add(fact->GetCell(defaultWorksheetIndex, 0, 1, ObjectExt::Box<System::String>(u"Series 1")), chart->get_Type());// Take first chart seriesSharedPtr<IChartSeries> series = chart->get_ChartData()->get_Series()->idx_get(0);// Populate series dataseries->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 borderpoint->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 borderpoint1->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 borderpoint2->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 seriesSharedPtr<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 Chartseries->get_Labels()->get_DefaultDataLabelFormat()->set_ShowLeaderLines(true);// Setting Rotation Angle for Pie Chart Sectorschart->get_ChartData()->get_SeriesGroups()->idx_get(0)->set_FirstSliceAngle(180);// Save PPTX filepres->Save(outputFilePath, Aspose::Slides::Export::SaveFormat::Pptx);
使用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()->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 fileSharedPtr<Presentation> pres = MakeObject<Presentation>();// Access first slideSharedPtr<ISlide> slide = pres->get_Slides()->idx_get(0);// Add chart with default dataSharedPtr<IChart> chart = slide->get_Shapes()->AddChart(Aspose::Slides::Charts::ChartType::ScatterWithSmoothLines, 0, 0, 500, 500);// Delete default generated serieschart->get_ChartData()->get_Series()->Clear();// Setting the index of chart data sheetint defaultWorksheetIndex = 0;// Getting the chart data workbookSharedPtr<IChartDataWorkbook> fact = chart->get_ChartData()->get_ChartDataWorkbook();// Add serieschart->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 seriesSharedPtr<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 seriesseries->set_Type(ChartType::ScatterWithStraightLinesAndMarkers);// Changing the chart series markerseries->get_Marker()->set_Size(10);series->get_Marker()->set_Symbol(MarkerStyleType::Star);// Take second chart seriesseries = 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 markerseries->get_Marker()->set_Size(10);series->get_Marker()->set_Symbol(MarkerStyleType::Circle);// Save PPTX filepres->Save(outputFilePath, Aspose::Slides::Export::SaveFormat::Pptx);
在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 fileSharedPtr<Presentation> pres = MakeObject<Presentation>();// Access first slideSharedPtr<ISlide> slide = pres->get_Slides()->idx_get(0);// Add chart with default dataSystem::SharedPtr<IChart> chart = slide->get_Shapes()->AddChart(Aspose::Slides::Charts::ChartType::Histogram, 50, 50, 500, 400);// Delete default generated series and categorieschart->get_ChartData()->get_Categories()->Clear();chart->get_ChartData()->get_Series()->Clear();// Getting the chart data workbookSystem::SharedPtr<IChartDataWorkbook> wb = chart->get_ChartData()->get_ChartDataWorkbook();wb->Clear(0);// Add seriesSystem::SharedPtr<IChartSeries> series = chart->get_ChartData()->get_Series()->Add(Aspose::Slides::Charts::ChartType::Histogram);// Populate series dataseries->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 typechart->get_Axes()->get_HorizontalAxis()->set_AggregationType(Aspose::Slides::Charts::AxisAggregationType::Automatic);// Save PPTX filepres->Save(outputFilePath, Aspose::Slides::Export::SaveFormat::Pptx);
如果你想试用Aspose的全部完整功能,可联系在线客服获取30天临时授权体验。
还想要更多吗可以点击阅读【2020 · Aspose最新资源整合】,查找需要的教程资源。如果您有任何疑问或需求,请随时加入Aspose技术交流群(),我们很高兴为您提供查询和咨询。
标签:
声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!