PowerPointプレゼンテーションでグラフを作成する

グラフは、データを簡潔に表示するための優れたツールです。また、データを視覚的に表現することで、大量のデータを消費しやすくします。プレゼンテーションにグラフを追加すると、会社の成長傾向や製品の採用率などのデータを提示するときに役立つ場合があります。そのために、この記事では、C++を使用してPowerPointプレゼンテーションでグラフを作成する方法を説明します。

PowerPointプレゼンテーションでグラフを作成するためのC++API

Aspose.Slides for C++は、PowerPointファイルの作成、読み取り、操作をサポートするネイティブC++ライブラリです。 APIは、PowerPointプレゼンテーションでのグラフの作成もサポートしています。 APIは、NuGetからインストールするか、ダウンロードセクションから直接ダウンロードできます。

PM> Install-Package Aspose.Slides.Cpp

C++を使用してPowerPointプレゼンテーションで縦棒グラフを作成する

以下は、PowerPointプレゼンテーションで縦棒グラフを作成する手順です。

以下は、C++を使用してPowerPointプレゼンテーションに縦棒グラフを追加するためのサンプルコードです。

// 出力ファイルパス。
const String outputFilePath = u"OutputDirectory\\column_chart.pptx";

// PPTXファイルを表すPresentationクラスをインスタンス化します
SharedPtr<Presentation> pres = MakeObject<Presentation>();

// 最初のスライドにアクセスする
SharedPtr<ISlide> slide = pres->get_Slides()->idx_get(0);

// デフォルトデータでチャートを追加
SharedPtr<IChart> chart = slide->get_Shapes()->AddChart(Aspose::Slides::Charts::ChartType::ClusteredColumn, 0, 0, 500, 500);

// チャートデータシートのインデックスを設定する
int defaultWorksheetIndex = 0;

// チャートデータワークブックの取得
SharedPtr<IChartDataWorkbook> fact = chart->get_ChartData()->get_ChartDataWorkbook();

// チャートタイトルの設定
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);

// デフォルトで生成されたシリーズとカテゴリを削除する
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();

// シリーズを追加
chart->get_ChartData()->get_Series()->Add(fact->GetCell(defaultWorksheetIndex, 0, 1, ObjectExt::Box<System::String>(u"Series 1")), chart->get_Type());
chart->get_ChartData()->get_Series()->Add(fact->GetCell(defaultWorksheetIndex, 0, 2, ObjectExt::Box<System::String>(u"Series 2")), chart->get_Type());

// カテゴリを追加
chart->get_ChartData()->get_Categories()->Add(fact->GetCell(defaultWorksheetIndex, 1, 0, ObjectExt::Box<System::String>(u"Category 1")));
chart->get_ChartData()->get_Categories()->Add(fact->GetCell(defaultWorksheetIndex, 2, 0, ObjectExt::Box<System::String>(u"Category 2")));
chart->get_ChartData()->get_Categories()->Add(fact->GetCell(defaultWorksheetIndex, 3, 0, ObjectExt::Box<System::String>(u"Category 3")));

// 最初のチャートシリーズを取る
SharedPtr<IChartSeries> series = chart->get_ChartData()->get_Series()->idx_get(0);

// シリーズデータを入力する
series->get_DataPoints()->AddDataPointForBarSeries(fact->GetCell(defaultWorksheetIndex, 1, 1, ObjectExt::Box<double>(20)));
series->get_DataPoints()->AddDataPointForBarSeries(fact->GetCell(defaultWorksheetIndex, 2, 1, ObjectExt::Box<double>(50)));
series->get_DataPoints()->AddDataPointForBarSeries(fact->GetCell(defaultWorksheetIndex, 3, 1, ObjectExt::Box<double>(30)));

// シリーズの塗りつぶし色の設定
series->get_Format()->get_Fill()->set_FillType(FillType::Solid);
series->get_Format()->get_Fill()->get_SolidFillColor()->set_Color(System::Drawing::Color::get_Blue());

// 2番目のチャートシリーズを取る
series = chart->get_ChartData()->get_Series()->idx_get(1);

// シリーズデータを入力する
series->get_DataPoints()->AddDataPointForBarSeries(fact->GetCell(defaultWorksheetIndex, 1, 2, ObjectExt::Box<double>(30)));
series->get_DataPoints()->AddDataPointForBarSeries(fact->GetCell(defaultWorksheetIndex, 2, 2, ObjectExt::Box<double>(10)));
series->get_DataPoints()->AddDataPointForBarSeries(fact->GetCell(defaultWorksheetIndex, 3, 2, ObjectExt::Box<double>(60)));

// シリーズの塗りつぶし色の設定
series->get_Format()->get_Fill()->set_FillType(FillType::Solid);
series->get_Format()->get_Fill()->get_SolidFillColor()->set_Color(System::Drawing::Color::get_Orange());

// 最初のラベルにはカテゴリ名が表示されます
SharedPtr<IDataLabel> lbl = 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);

// 3番目のラベルの値を表示
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"/");

// PPTXファイルを保存する
pres->Save(outputFilePath, Aspose::Slides::Export::SaveFormat::Pptx);

以下は、サンプルコードによって生成された縦棒グラフの画像です。

PowerPointプレゼンテーションで縦棒グラフを作成する

C++を使用してPowerPointプレゼンテーションで円グラフを作成する

以下は、PowerPointスライドに円グラフを追加する手順です。

以下は、C++を使用してPowerPointスライドに円グラフを追加するためのサンプルコードです。

// 出力ファイルパス。
const String outputFilePath = u"OutputDirectory\\pie_chart.pptx";

// PPTXファイルを表すPresentationクラスをインスタンス化します
SharedPtr<Presentation> pres = MakeObject<Presentation>();

// 最初のスライドにアクセスする
SharedPtr<ISlide> slide = pres->get_Slides()->idx_get(0);

// デフォルトデータでチャートを追加
SharedPtr<IChart> chart = slide->get_Shapes()->AddChart(Aspose::Slides::Charts::ChartType::Pie, 0, 0, 500, 500);

// チャートタイトルの設定
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);

// デフォルトで生成されたシリーズとカテゴリを削除する
chart->get_ChartData()->get_Series()->Clear();
chart->get_ChartData()->get_Categories()->Clear();

// チャートデータシートのインデックスを設定する
int defaultWorksheetIndex = 0;

// チャートデータワークブックの取得
SharedPtr<IChartDataWorkbook> fact = chart->get_ChartData()->get_ChartDataWorkbook();

// カテゴリを追加
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")));

// シリーズを追加
chart->get_ChartData()->get_Series()->Add(fact->GetCell(defaultWorksheetIndex, 0, 1, ObjectExt::Box<System::String>(u"Series 1")), chart->get_Type());

// 最初のチャートシリーズを取る
SharedPtr<IChartSeries> series = chart->get_ChartData()->get_Series()->idx_get(0);

// シリーズデータを入力する
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());

// セクター境界の設定
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());

// セクター境界の設定
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());

// セクター境界の設定
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);

// シリーズのカテゴリごとにカスタムラベルを作成する
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);

// チャートの引出線の表示
series->get_Labels()->get_DefaultDataLabelFormat()->set_ShowLeaderLines(true);

// 円グラフセクターの回転角の設定
chart->get_ChartData()->get_SeriesGroups()->idx_get(0)->set_FirstSliceAngle(180);

// PPTXファイルを保存する
pres->Save(outputFilePath, Aspose::Slides::Export::SaveFormat::Pptx);

以下は、サンプルコードによって生成された円グラフの画像です。

PowerPointプレゼンテーションで円グラフを作成する

C++を使用してPowerPointプレゼンテーションで散布図を作成する

以下は、PowerPointスライドに散在するグラフを追加する手順です。

以下は、C++を使用してPowerPointスライドに分散グラフを追加するためのサンプルコードです。

// 出力ファイルパス。
const String outputFilePath = u"OutputDirectory\\scattered_chart.pptx";

// PPTXファイルを表すPresentationクラスをインスタンス化します
SharedPtr<Presentation> pres = MakeObject<Presentation>();

// 最初のスライドにアクセスする
SharedPtr<ISlide> slide = pres->get_Slides()->idx_get(0);

// デフォルトデータでチャートを追加
SharedPtr<IChart> chart = slide->get_Shapes()->AddChart(Aspose::Slides::Charts::ChartType::ScatterWithSmoothLines, 0, 0, 500, 500);

// デフォルトで生成されたシリーズを削除する 
chart->get_ChartData()->get_Series()->Clear();

// チャートデータシートのインデックスを設定する
int defaultWorksheetIndex = 0;

// チャートデータワークブックの取得
SharedPtr<IChartDataWorkbook> fact = chart->get_ChartData()->get_ChartDataWorkbook();

// シリーズを追加
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());

// 最初のチャートシリーズを取る
SharedPtr<IChartSeries> series = chart->get_ChartData()->get_Series()->idx_get(0);

// そこに新しいポイント(1:3)を追加します。
series->get_DataPoints()->AddDataPointForScatterSeries(fact->GetCell(defaultWorksheetIndex, 2, 1, ObjectExt::Box<double>(1)), fact->GetCell(defaultWorksheetIndex, 2, 2, ObjectExt::Box<double>(3)));

// 新しいポイントを追加(2:10)
series->get_DataPoints()->AddDataPointForScatterSeries(fact->GetCell(defaultWorksheetIndex, 3, 1, ObjectExt::Box<double>(2)), fact->GetCell(defaultWorksheetIndex, 3, 2, ObjectExt::Box<double>(10)));

// シリーズの種類を編集する
series->set_Type(ChartType::ScatterWithStraightLinesAndMarkers);

// チャートシリーズマーカーの変更
series->get_Marker()->set_Size(10);
series->get_Marker()->set_Symbol(MarkerStyleType::Star);

// 2番目のチャートシリーズを取る
series = chart->get_ChartData()->get_Series()->idx_get(1);

// そこに新しいポイント(5:2)を追加します。
series->get_DataPoints()->AddDataPointForScatterSeries(fact->GetCell(defaultWorksheetIndex, 2, 3, ObjectExt::Box<double>(5)), fact->GetCell(defaultWorksheetIndex, 2, 4, ObjectExt::Box<double>(2)));

// 新しいポイントを追加(3:1)
series->get_DataPoints()->AddDataPointForScatterSeries(fact->GetCell(defaultWorksheetIndex, 3, 3, ObjectExt::Box<double>(3)), fact->GetCell(defaultWorksheetIndex, 3, 4, ObjectExt::Box<double>(1)));

// 新しいポイントを追加(2:2)
series->get_DataPoints()->AddDataPointForScatterSeries(fact->GetCell(defaultWorksheetIndex, 4, 3, ObjectExt::Box<double>(2)), fact->GetCell(defaultWorksheetIndex, 4, 4, ObjectExt::Box<double>(2)));

// 新しいポイントを追加(5:1)
series->get_DataPoints()->AddDataPointForScatterSeries(fact->GetCell(defaultWorksheetIndex, 5, 3, ObjectExt::Box<double>(5)), fact->GetCell(defaultWorksheetIndex, 5, 4, ObjectExt::Box<double>(1)));

// チャートシリーズマーカーの変更
series->get_Marker()->set_Size(10);
series->get_Marker()->set_Symbol(MarkerStyleType::Circle);

// PPTXファイルを保存する
pres->Save(outputFilePath, Aspose::Slides::Export::SaveFormat::Pptx);

以下は、サンプルコードによって生成された分散チャートの画像です。

PowerPointプレゼンテーションで散在するグラフを作成する

PowerPointプレゼンテーションでヒストグラムチャートを作成する

以下は、PowerPointプレゼンテーションでヒストグラムチャートを作成する手順です。

以下は、C++を使用してPowerPointプレゼンテーションでヒストグラムチャートを作成するためのサンプルコードです。

// 出力ファイルパス。
const String outputFilePath = u"OutputDirectory\\histogram_chart.pptx";

// PPTXファイルを表すPresentationクラスをインスタンス化します
SharedPtr<Presentation> pres = MakeObject<Presentation>();

// 最初のスライドにアクセスする
SharedPtr<ISlide> slide = pres->get_Slides()->idx_get(0);

// デフォルトデータでチャートを追加
System::SharedPtr<IChart> chart = slide->get_Shapes()->AddChart(Aspose::Slides::Charts::ChartType::Histogram, 50, 50, 500, 400);

// デフォルトで生成されたシリーズとカテゴリを削除する
chart->get_ChartData()->get_Categories()->Clear();
chart->get_ChartData()->get_Series()->Clear();

// チャートデータワークブックの取得
System::SharedPtr<IChartDataWorkbook> wb = chart->get_ChartData()->get_ChartDataWorkbook();

wb->Clear(0);

// シリーズを追加
System::SharedPtr<IChartSeries> series = chart->get_ChartData()->get_Series()->Add(Aspose::Slides::Charts::ChartType::Histogram);

// シリーズデータを入力する
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)));

// 軸集計タイプを設定する
chart->get_Axes()->get_HorizontalAxis()->set_AggregationType(Aspose::Slides::Charts::AxisAggregationType::Automatic);

// PPTXファイルを保存する
pres->Save(outputFilePath, Aspose::Slides::Export::SaveFormat::Pptx);

以下は、サンプルコードによって生成されたヒストグラムチャートの画像です。

PowerPointプレゼンテーションでヒストグラムチャートを作成する

追加のサポートされているチャート

上記のグラフ以外に、Aspose.Slides forC++はさらに多くの種類のグラフをサポートしています。 thisのドキュメント記事を読むと、サンプルコードでサポートされているチャートタイプの完全なリストを表示できます。

無料ライセンスを取得する

無料の一時ライセンスをリクエストして、評価の制限なしにAPIを試すことができます。

結論

この記事では、C++を使用してPowerPointスライドにグラフを追加する方法を学習しました。具体的には、PowerPointプレゼンテーションに列、散布図、円グラフ、およびヒストグラムのグラフを追加する方法を学習しました。さらに、Aspose.Slides for C++ APIは、PowerPointプレゼンテーション内で使用できるより多くのグラフタイプを提供することを確認しました。チャートに加えて、APIはPowerPointプレゼンテーションを強化するための一連の機能を証明します。 公式ドキュメントを使用して、APIを詳細に調べることができます。ご不明な点がございましたら、無料サポートフォーラムまでお気軽にお問い合わせください。

関連項目