C++ で PPT プレゼンテーションにグラフを作成

Microsoft PowerPoint なしで、サーバー側の Aspose.Slides for C++ API を使用して PPT プレゼンテーションにグラフを作成します。

C++ で PPT グラフを作成

Aspose.Slides for C++ を使用して、PPT プレゼンテーションにグラフを作成します。 NuGet からパッケージをインストールするか、パッケージ マネージャー コンソールで次のコマンドを使用してください。

コマンド

PM> Install-Package Aspose.Slides.Cpp

PPT にグラフを作成 – 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 PPT file.
presentation->Save(u"column_chart.ppt", SaveFormat::Ppt);
presentation->Dispose();

C++ で PPT ファイルにグラフを作成する方法

Aspose.Slides for C++ を使用すると、数行のコードで PPT プレゼンテーションにグラフを作成できます。

  1. Presentation オブジェクトを作成します。
  2. 最初のスライドにアクセスします。
  3. 既定のデータを持つグラフを追加します。
  4. グラフのデータ シートのインデックスを設定します。
  5. グラフのデータ ブックを取得します。
  6. グラフのタイトルを設定します。
  7. 既定で生成された系列とカテゴリを削除します。
  8. 系列とカテゴリを追加します。
  9. グラフの最初の系列を取得し、データを入力します。
  10. 系列の塗りつぶしの色を設定します。
  11. 2 番目の系列のデータ ラベルに、カテゴリ名、系列名、値を表示します。
  12. PPT ファイルを保存します。

システム要件

Aspose.Slides for C++ は主要なプラットフォームとオペレーティング システムをサポートしています。環境が次の前提条件を満たしていることを確認してください。

  • サポート対象の C++ ランタイムを備えた Microsoft Windows、Linux、macOS、またはその他の互換 OS。
  • プロジェクトで参照されている Aspose.Slides for C++ ライブラリ。

Aspose.Slides で構築

PPT グラフをオンラインで作成

無料の Aspose.Slides Chart アプリは、上記のコードと同じ API である Aspose.Slides for C++ で構築されています。アプリを開いて、ご自身の PPT ファイルで API に何ができるかをお確かめください。

  • ブラウザーで動作し、インストールは不要です。
  • コードを書く前に、Aspose.Slides がグラフを作成するしくみを確認できます。
  • Excel またはテーブルのデータからグラフを作成します。
  • PPT ファイルまたは画像としてダウンロードします。

PPT ファイル形式とは

PPT ファイルには、スライド ショー用のスライドのコレクションが含まれます。Microsoft PowerPoint 97-2003 のバイナリ ファイル形式を使用します。PPT ファイルには、テキスト、箇条書き、画像、マルチメディア、その他の埋め込み OLE オブジェクトを含めることができます。Microsoft は PowerPoint 2007 で新しい PPTX 形式を導入しました。PPTX は Office Open XML に基づいており、従来のバイナリ形式とは異なります。OpenOffice Impress や Apple Keynote などの他のアプリケーションでも PPT ファイルを作成できます。

続きを読む

グラフでサポートされるその他の形式

C++ を使用すると、他の形式でもグラフを作成できます。