翻译|使用教程|编辑:李显亮|2021-04-08 10:41:25.563|阅读 172 次
概述:在演示诸如公司的增长趋势或产品采用率之类的数据时,向演示文稿中添加图表可能会有所帮助。为此,本文将教您如何 使用C ++在PowerPoint演示文稿中创建图表。
# 慧都年终大促·界面/图表报表/文档/IDE等千款热门软控件火热促销中 >>
图表是简洁显示数据的绝佳工具。此外,它们通过直观地表示,使使用大量数据变得更加容易。在演示诸如公司的增长趋势或产品采用率之类的数据时,向演示文稿中添加图表可能会有所帮助。为此,本文将教您如何 使用C ++在PowerPoint演示文稿中创建图表。
Aspose.Slides for C ++ 是本机C ++库,支持创建,读取和操作PowerPoint文件。该API还支持在PowerPoint演示文稿中创建图表。您可以点击下方按钮下载体验。
以下是在PowerPoint演示文稿中创建柱形图的步骤。
以下是使用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);
下面是示例代码生成的柱形图的图像。
以下是将饼图添加到PowerPoint幻灯片的步骤。
以下是使用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);
以下是将分散的图表添加到PowerPoint幻灯片的步骤。
以下是使用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);
以下是在PowerPoint演示文稿中创建直方图的步骤。
以下是使用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);
如果你想试用Aspose的全部完整功能,可联系在线客服获取30天临时授权体验。
本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@pclwef.cn