Tạo biểu đồ trong bản trình bày PPTX bằng C++

Tạo biểu đồ trong bản trình bày PPTX bằng API phía máy chủ của Aspose.Slides for C++ mà không cần Microsoft PowerPoint.

Tạo biểu đồ PPTX bằng C++

Dùng Aspose.Slides for C++ để tạo biểu đồ trong bản trình bày PPTX. Cài đặt gói từ NuGet hoặc dùng lệnh sau trong Package Manager Console.

Lệnh

PM> Install-Package Aspose.Slides.Cpp

Tạo biểu đồ trong PPTX – C++

auto presentation = MakeObject<Presentation>();
auto slide = presentation->get_Slide(0);

// Add a chart with default data.
auto chart = slide->get_Shapes()->AddChart(ChartType::ClusteredColumn, 0, 0, 500, 500);

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

// Get the chart data workbook.
auto chartDataWorkbook = chart->get_ChartData()->get_ChartDataWorkbook();

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

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

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

// Take the first chart series.
auto series = chart->get_ChartData()->get_Series()->idx_get(0);

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

// Set the fill color for the series.
series->get_Format()->get_Fill()->set_FillType(FillType::Solid);
series->get_Format()->get_Fill()->get_SolidFillColor()->set_Color(Color::get_Blue());

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

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

// Set the fill color for the series.
series->get_Format()->get_Fill()->set_FillType(FillType::Solid);
series->get_Format()->get_Fill()->get_SolidFillColor()->set_Color(Color::get_Orange());

// Show the category name for the first label.
auto dataLabel = series->get_DataPoints()->idx_get(0)->get_Label();
dataLabel->get_DataLabelFormat()->set_ShowCategoryName(true);

dataLabel = series->get_DataPoints()->idx_get(1)->get_Label();
dataLabel->get_DataLabelFormat()->set_ShowSeriesName(true);

// Show the value for the third label.
dataLabel = series->get_DataPoints()->idx_get(2)->get_Label();
dataLabel->get_DataLabelFormat()->set_ShowValue(true);
dataLabel->get_DataLabelFormat()->set_ShowSeriesName(true);
dataLabel->get_DataLabelFormat()->set_Separator(u"/");

// Save the PPTX file.
presentation->Save(u"column_chart.pptx", SaveFormat::Pptx);
presentation->Dispose();

Cách tạo biểu đồ trong tệp PPTX bằng C++

Tạo biểu đồ trong bản trình bày PPTX với Aspose.Slides for C++ chỉ bằng vài dòng mã.

  1. Tạo một đối tượng Presentation.
  2. Truy cập trang chiếu đầu tiên.
  3. Thêm biểu đồ với dữ liệu mặc định.
  4. Đặt chỉ mục của trang dữ liệu biểu đồ.
  5. Lấy sổ làm việc dữ liệu biểu đồ.
  6. Đặt tiêu đề biểu đồ.
  7. Xóa các chuỗi dữ liệu và danh mục được tạo mặc định.
  8. Thêm chuỗi dữ liệu và danh mục.
  9. Lấy chuỗi đầu tiên của biểu đồ và điền dữ liệu cho chuỗi.
  10. Đặt màu tô cho chuỗi.
  11. Hiển thị tên danh mục, tên chuỗi và giá trị trong nhãn dữ liệu của chuỗi thứ hai.
  12. Lưu tệp PPTX.

Yêu cầu hệ thống

Aspose.Slides for C++ hỗ trợ các nền tảng và hệ điều hành chính. Hãy đảm bảo môi trường của bạn đáp ứng các điều kiện tiên quyết sau.

  • Microsoft Windows, Linux, macOS hoặc hệ điều hành tương thích khác có môi trường chạy C++ được hỗ trợ.
  • Thư viện Aspose.Slides for C++ được tham chiếu trong dự án của bạn.

Được xây dựng bằng Aspose.Slides

Tạo biểu đồ PPTX trực tuyến

Ứng dụng Aspose.Slides Chart miễn phí được xây dựng bằng Aspose.Slides for C++, cùng API với đoạn mã ở trên. Hãy mở ứng dụng và xem API có thể làm gì với các tệp PPTX của riêng bạn.

  • Chạy trong trình duyệt, không cần cài đặt gì.
  • Xem cách Aspose.Slides tạo biểu đồ trước khi bạn viết mã.
  • Tạo biểu đồ từ dữ liệu Excel hoặc bảng.
  • Tải xuống dưới dạng tệp PPTX hoặc hình ảnh.

Những gì là PPTX Tập Tin Định Dạng

Tệp PPTX là tệp bản trình bày được tạo bằng Microsoft PowerPoint và các ứng dụng trình bày khác. Khác với định dạng nhị phân PPT cũ hơn, PPTX dựa trên Office Open XML. Bản trình bày gồm các trang chiếu có thể chứa văn bản, hình ảnh, định dạng, hiệu ứng động và phương tiện khác. Các trang chiếu được trình chiếu cho khán giả với thiết lập trình bày tùy chỉnh.

Đọc Thêm

Các định dạng khác được hỗ trợ cho biểu đồ

Với C++, bạn cũng có thể tạo biểu đồ ở các định dạng khác.