翻譯|使用教程|編輯:李顯亮|2021-04-08 10:41:25.563|閱讀 233 次
概述:在演示諸如公司的增長趨勢或產品采用率之類的數據時,向演示文稿中添加圖表可能會有所幫助。為此,本文將教您如何 使用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@fc6vip.cn