饼图

饼图和条形图的第二绘图选项

Aspose.Slides for C++ 现在支持饼图和条形图的第二绘图选项。在本主题中,我们将通过示例查看如何使用 Aspose.Slides 指定这些选项。为了指定属性,请按照以下步骤操作:

  1. 实例化 Presentation 类对象。
  2. 在幻灯片上添加图表。
  3. 指定图表的第二绘图选项。
  4. 将演示文稿写入磁盘。

在下面给出的示例中,我们设置了饼图的不同属性。

For complete examples and data files, please go to https://github.com/aspose-slides/Aspose.Slides-for-C
// The path to the documents directory.
const String outPath = u"../out/SecondPlotOptionsforCharts_out.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);
// Take first chart series
SharedPtr<IChartSeries> series = chart->get_ChartData()->get_Series()->idx_get(0);
// Set different properties
series->get_Labels()->get_DefaultDataLabelFormat()->set_ShowValue(true);
series->get_ParentSeriesGroup()->set_SecondPieSize ( 149);
series->get_ParentSeriesGroup()->set_PieSplitBy ( Aspose::Slides::Charts::PieSplitType::ByPercentage);
series->get_ParentSeriesGroup()->set_PieSplitPosition ( 53);
// Write the presentation file to disk
pres->Save(outPath, Aspose::Slides::Export::SaveFormat::Pptx);

设置自动饼图切片颜色

Aspose.Slides for C++ 提供了一个简单的 API 用于设置自动饼图切片颜色。示例代码应用了上述属性设置。

  1. 创建 Presentation 类的实例。
  2. 访问第一张幻灯片。
  3. 添加默认数据的图表。
  4. 设置图表标题。
  5. 设置第一系列显示值。
  6. 设置图表数据表的索引。
  7. 获取图表数据工作表。
  8. 删除默认生成的系列和类别。
  9. 添加新类别。
  10. 添加新系列。

将修改后的演示文稿写入 PPTX 文件。

For complete examples and data files, please go to https://github.com/aspose-slides/Aspose.Slides-for-C
// The path to the documents directory.
const String outPath = u"../out/AutomicPieChartSliceColors_out.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 worksheet
SharedPtr<IChartDataWorkbook> fact = chart->get_ChartData()->get_ChartDataWorkbook();
// Add Catrgories
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"3ed Qtr")));
// Now, Adding a new 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);
// Now populating 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);
// Write the presentation file to disk
pres->Save(outPath, Aspose::Slides::Export::SaveFormat::Pptx);