C++ を使用したプレゼンテーションのチャート軸をカスタマイズする

概要

この記事では、Aspose.Slides for C++ を使用してチャート軸をカスタマイズする方法を説明します。計算された軸値、チャートの行と列の入れ替え、軸の可視性、カテゴリ ラベルと目盛り間隔、日付カテゴリと書式設定、タイトルの回転、軸の位置指定、表示単位、対数スケーリングについて取り上げます。

チャートの縦軸で最大値を取得する

Presentation を作成し、デフォルト データのエリアチャートを追加します。計算された軸値を読み取る前に ValidateChartLayout を呼び出し、チャートのレイアウトを最新の状態にします。

軸の上限と下限は get_ActualMaxValue と get_ActualMinValue で取得し、目盛り間隔は get_ActualMajorUnit と get_ActualMinorUnit で取得します。get_ActualMajorUnitScale と get_ActualMinorUnitScale は時間単位のスケールを提供し、日付軸に関連します。例ではこれらの値をローカル変数に格納し、チャートを保存します。

#include <DOM/Presentation.h>
#include <DOM/ISlide.h>
#include <DOM/IShapeCollection.h>
#include <DOM/IChart.h>
#include <DOM/Chart/ChartType.h>
#include <DOM/Chart/IAxesManager.h>
#include <DOM/Chart/IAxis.h>
#include <Export/SaveFormat.h>

using namespace Aspose::Slides;
using namespace Aspose::Slides::Charts;
using namespace Aspose::Slides::Export;

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

auto chart = slide->get_Shapes()->AddChart(ChartType::Area, 100, 100, 500, 350);
chart->ValidateChartLayout();

auto maxValue = chart->get_Axes()->get_VerticalAxis()->get_ActualMaxValue();
auto minValue = chart->get_Axes()->get_VerticalAxis()->get_ActualMinValue();

auto majorUnit = chart->get_Axes()->get_VerticalAxis()->get_ActualMajorUnit();
auto minorUnit = chart->get_Axes()->get_VerticalAxis()->get_ActualMinorUnit();

auto majorUnitScale = chart->get_Axes()->get_VerticalAxis()->get_ActualMajorUnitScale();
auto minorUnitScale = chart->get_Axes()->get_VerticalAxis()->get_ActualMinorUnitScale();

presentation->Save(u"AxisValues_out.pptx", SaveFormat::Pptx);

軸間でデータを入れ替える

SwitchRowColumn を使用して、チャート データで系列とカテゴリの役割を入れ替えます。元のカテゴリが系列になり、元の系列がカテゴリになります。これはデータのグループ化方法を変更しますが、水平軸と垂直軸を入れ替えるわけではありません。例では SetRange を使用してデフォルト データを Sheet1!A1:D5(ヘッダー行とカテゴリ列を含む)にバインドし、行と列を入れ替えた後にチャートを保存します。結果は 4 系列・3 カテゴリのチャートです。

#include <DOM/Presentation.h>
#include <DOM/ISlide.h>
#include <DOM/IShapeCollection.h>
#include <DOM/IChart.h>
#include <DOM/Chart/ChartType.h>
#include <DOM/Chart/IAxesManager.h>
#include <DOM/Chart/IAxis.h>
#include <Export/SaveFormat.h>
#include <DOM/Chart/IChartData.h>

using namespace Aspose::Slides;
using namespace Aspose::Slides::Charts;
using namespace Aspose::Slides::Export;

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

auto chart = slide->get_Shapes()->AddChart(ChartType::ClusteredColumn, 100, 100, 400, 300);

chart->get_ChartData()->SetRange(u"Sheet1!A1:D5");
chart->get_ChartData()->SwitchRowColumn();

presentation->Save(u"SwitchChartRowColumns_out.pptx", SaveFormat::Pptx);

軸のプロット順序を逆にする

set_IsPlotOrderReversed を使用して、カテゴリ軸または値軸の順序を逆にします。たとえば、水平棒グラフで「計画」「開発」「テスト」の順に上から下へ表示できます。軸を逆にするとプロット方向が変わりますが、ワークブック データの並び替えや再配置は行われません。比較として、SwitchRowColumn は系列とカテゴリの役割を入れ替えます。

以下の例は、3 つのステージと 1 系列の水平棒グラフを作成します。プレゼンテーションには 3 スライドが保存されます:通常順、カテゴリ逆、値逆。各コピーは同じデータセットを使用し、各コピーで 1 つの軸だけが逆になります。

#include <DOM/Presentation.h>
#include <DOM/ISlide.h>
#include <DOM/ISlideCollection.h>
#include <DOM/IShapeCollection.h>
#include <DOM/IChart.h>
#include <DOM/Chart/ChartType.h>
#include <DOM/Chart/IAxesManager.h>
#include <DOM/Chart/IAxis.h>
#include <DOM/Chart/CrossesType.h>
#include <DOM/Chart/IChartData.h>
#include <DOM/Chart/IChartDataWorkbook.h>
#include <DOM/Chart/IChartDataCell.h>
#include <DOM/Chart/IChartCategoryCollection.h>
#include <DOM/Chart/IChartSeriesCollection.h>
#include <DOM/Chart/IChartSeries.h>
#include <DOM/Chart/IChartDataPointCollection.h>
#include <Export/SaveFormat.h>
#include <system/object_ext.h>
#include <array>

using namespace Aspose::Slides;
using namespace Aspose::Slides::Charts;
using namespace Aspose::Slides::Export;

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

auto chart = slide->get_Shapes()->AddChart(ChartType::ClusteredBar, 50, 40, 620, 320);
chart->set_HasLegend(false);

chart->get_ChartData()->get_Categories()->Clear();
chart->get_ChartData()->get_Series()->Clear();

auto workbook = chart->get_ChartData()->get_ChartDataWorkbook();
workbook->Clear(0);

auto series = chart->get_ChartData()->get_Series()->Add(ChartType::ClusteredBar);
auto stages = std::array<System::String, 3>{u"Planning", u"Development", u"Testing"};
auto values = std::array<int, 3>{10, 30, 20};
for (auto i = 0; i < stages.size(); i++)
{
    auto categoryCell = workbook->GetCell(0, i + 1, 0, System::ObjectExt::Box(stages[i]));
    chart->get_ChartData()->get_Categories()->Add(categoryCell);
    auto valueCell = workbook->GetCell(0, i + 1, 1, System::ObjectExt::Box(values[i]));
    series->get_DataPoints()->AddDataPointForBarSeries(valueCell);
}

// スライド 2: 最初のカテゴリを上部に表示します。
auto categorySlide = presentation->get_Slides()->AddClone(slide);
auto categoryChart = System::ExplicitCast<IChart>(categorySlide->get_Shape(0));
categoryChart->get_Axes()->get_VerticalAxis()->set_IsPlotOrderReversed(true);
// 水平値軸を下部に保ちます。
categoryChart->get_Axes()->get_VerticalAxis()->set_CrossType(CrossesType::Maximum);

// スライド 3: 水平数値スケールのみ逆にします。
auto valueSlide = presentation->get_Slides()->AddClone(slide);
auto valueChart = System::ExplicitCast<IChart>(valueSlide->get_Shape(0));
valueChart->get_Axes()->get_HorizontalAxis()->set_IsPlotOrderReversed(true);

presentation->Save(u"ReversedAxes.pptx", SaveFormat::Pptx);

軸を逆にすると、垂直軸をプロットの反対側に移動させることもできます。set_CrossType で他の軸が交差する位置を制御します。スライド 2 では、逆にしたカテゴリ軸で Maximum を設定すると水平値軸が下部に残ります。特定の交差点を指定する場合は set_CrossAt を使用します。方向を変更する際は交差設定を確認してください。特にカスタム交差点が設定されている場合は注意が必要です。set_IsPlotOrderReversed を false に設定すると通常のプロット方向に戻りますが、交差設定は別に管理されます。

通常順(スライド 1) カテゴリ逆(スライド 2) 値逆(スライド 3)
通常のカテゴリ軸と値軸順序の水平棒グラフ カテゴリが上から下へ表示される水平棒グラフ 値軸が逆になり、ゼロが右側にある水平棒グラフ
カテゴリは下から上へ、値は左から右へ増加します。 計画、開発、テストが上から下へ表示されます。値とワークブックの行順は変更されません。 カテゴリは通常順のまま、値は右から左へ増加します。カテゴリ軸は右側のゼロで交差します。

折れ線グラフの縦軸を非表示にする

垂直軸に対して false を渡して set_IsVisible を呼び出すと、縦軸が非表示になります。例ではデフォルト データの折れ線グラフを作成し、縦軸を非表示にして保存します。

#include <DOM/Presentation.h>
#include <DOM/ISlide.h>
#include <DOM/IShapeCollection.h>
#include <DOM/IChart.h>
#include <DOM/Chart/ChartType.h>
#include <DOM/Chart/IAxesManager.h>
#include <DOM/Chart/IAxis.h>
#include <Export/SaveFormat.h>

using namespace Aspose::Slides;
using namespace Aspose::Slides::Charts;
using namespace Aspose::Slides::Export;

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

auto chart = slide->get_Shapes()->AddChart(ChartType::Line, 100, 100, 400, 300);
chart->get_Axes()->get_VerticalAxis()->set_IsVisible(false);

presentation->Save(u"HiddenVerticalAxis.pptx", SaveFormat::Pptx);

折れ線グラフの横軸を非表示にする

水平軸に対して false を渡して set_IsVisible を呼び出すと、横軸が非表示になります。例ではデフォルト データの折れ線グラフを作成し、横軸を非表示にして保存します。

#include <DOM/Presentation.h>
#include <DOM/ISlide.h>
#include <DOM/IShapeCollection.h>
#include <DOM/IChart.h>
#include <DOM/Chart/ChartType.h>
#include <DOM/Chart/IAxesManager.h>
#include <DOM/Chart/IAxis.h>
#include <Export/SaveFormat.h>

using namespace Aspose::Slides;
using namespace Aspose::Slides::Charts;
using namespace Aspose::Slides::Export;

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

auto chart = slide->get_Shapes()->AddChart(ChartType::Line, 100, 100, 400, 300);
chart->get_Axes()->get_HorizontalAxis()->set_IsVisible(false);

presentation->Save(u"HiddenHorizontalAxis.pptx", SaveFormat::Pptx);

カテゴリ軸を変更する

set_CategoryAxisType を使用して、日付軸またはテキスト軸を選択します。この例では ExistingChart.pptx が必要で、最初のスライドの最初のシェイプがチャート、カテゴリセルには数値の Excel 日付が格納されています。水平軸を日付軸に変更します。set_IsAutomaticMajorUnit に false、set_MajorUnit に 1、set_MajorUnitScale に月を指定すると、主要目盛りが 1 か月間隔で配置されます。

#include <DOM/Presentation.h>
#include <DOM/ISlide.h>
#include <DOM/IShapeCollection.h>
#include <DOM/IChart.h>
#include <DOM/Chart/ChartType.h>
#include <DOM/Chart/IAxesManager.h>
#include <DOM/Chart/IAxis.h>
#include <Export/SaveFormat.h>
#include <DOM/Chart/CategoryAxisType.h>
#include <DOM/Chart/TimeUnitType.h>
#include <system/object_ext.h>

using namespace Aspose::Slides;
using namespace Aspose::Slides::Charts;
using namespace Aspose::Slides::Export;

auto presentation = System::MakeObject<Presentation>(u"ExistingChart.pptx");
auto slide = presentation->get_Slide(0);

auto chart = System::ExplicitCast<IChart>(slide->get_Shape(0));
chart->get_Axes()->get_HorizontalAxis()->set_CategoryAxisType(CategoryAxisType::Date);
chart->get_Axes()->get_HorizontalAxis()->set_IsAutomaticMajorUnit(false);
chart->get_Axes()->get_HorizontalAxis()->set_MajorUnit(1);
chart->get_Axes()->get_HorizontalAxis()->set_MajorUnitScale(TimeUnitType::Months);

presentation->Save(u"ChangeChartCategoryAxis_out.pptx", SaveFormat::Pptx);

カテゴリ軸ラベル間隔を制御する

カテゴリが多数ある場合、カテゴリやデータポイントを削除せずに表示ラベル数を減らすことができます。set_IsAutomaticTickLabelSpacing に false を設定し、続いて set_TickLabelSpacing で希望するカテゴリ間隔を指定します。通常順のテキストカテゴリの場合、カウントは最初のカテゴリから始まります。

間隔 例で表示されるラベル
1 Category 1, Category 2, Category 3, … Category 24
2 Category 1, Category 3, Category 5, … Category 23
3 Category 1, Category 4, Category 7, … Category 22

間隔 3 は 3 番目ごとのラベルだけを表示し、表示ラベル間に 2 つのラベルが非表示になります。対応する列は削除されません。自動間隔は利用可能なスペースに基づいて決定され、必ずしもすべてのラベルを表示するわけではありません。

目盛りには別個の制御があります。set_IsAutomaticTickMarksSpacing に false を設定し、set_TickMarksSpacing で目盛りの間隔を指定します。例として、1 に設定すればカテゴリ間隔ごとに目盛りが残り、ラベルは 3 番目ごとに表示されます。set_MajorTickMark で可視スタイルを設定すると結果が確認できます。自動間隔プロパティを true に戻すと、チャートが再び自動で間隔を選択します。

以下の自己完結型サンプルは 24 カテゴリと 1 系列を作成し、CategoryAxisIntervals.pptx に 3 スライドを保存します:自動間隔、ラベル間隔手動(目盛りは独立)、自動間隔に復元。2 つのコピーは元のチャート データを保持します。入力プレゼンテーションは不要です。水平ラベルのテキストが密度の違いを分かりやすく示します。

#include <DOM/Presentation.h>
#include <DOM/ISlide.h>
#include <DOM/IShapeCollection.h>
#include <DOM/IChart.h>
#include <DOM/Chart/ChartType.h>
#include <DOM/Chart/IAxesManager.h>
#include <DOM/Chart/IAxis.h>
#include <Export/SaveFormat.h>
#include <DOM/Chart/IChartData.h>
#include <DOM/Chart/CategoryAxisType.h>
#include <DOM/Chart/TickMarkType.h>
#include <DOM/Chart/IChartTextFormat.h>
#include <DOM/Chart/IChartTextBlockFormat.h>
#include <DOM/Chart/IChartPortionFormat.h>
#include <DOM/Chart/IChartDataWorkbook.h>
#include <DOM/Chart/IChartDataCell.h>
#include <DOM/Chart/IChartCategoryCollection.h>
#include <DOM/Chart/IChartSeriesCollection.h>
#include <DOM/Chart/IChartSeries.h>
#include <DOM/Chart/IChartDataPointCollection.h>
#include <system/object_ext.h>
#include <DOM/ISlideCollection.h>

using namespace Aspose::Slides;
using namespace Aspose::Slides::Charts;
using namespace Aspose::Slides::Export;

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

auto chart = slide->get_Shapes()->AddChart(ChartType::ClusteredColumn, 30, 40, 660, 320);

chart->set_HasLegend(false);
chart->get_ChartData()->get_Categories()->Clear();
chart->get_ChartData()->get_Series()->Clear();

auto workbook = chart->get_ChartData()->get_ChartDataWorkbook();
workbook->Clear(0);

auto series = chart->get_ChartData()->get_Series()->Add(ChartType::ClusteredColumn);
for (auto i = 0; i < 24; i++)
{
    auto categoryName = System::String::Format(u"Category {0}", i + 1);
    auto categoryCell = workbook->GetCell(0, i + 1, 0, System::ObjectExt::Box(categoryName));
    chart->get_ChartData()->get_Categories()->Add(categoryCell);
    auto valueCell = workbook->GetCell(0, i + 1, 1, System::ObjectExt::Box(10 + i % 6 * 5));
    series->get_DataPoints()->AddDataPointForBarSeries(valueCell);
}

auto axis = chart->get_Axes()->get_HorizontalAxis();
axis->set_CategoryAxisType(CategoryAxisType::Text);
axis->get_TextFormat()->get_TextBlockFormat()->set_RotationAngle(0);
axis->get_TextFormat()->get_PortionFormat()->set_FontHeight(12);
axis->set_MajorTickMark(TickMarkType::Outside);
axis->set_IsAutomaticTickLabelSpacing(true);
axis->set_IsAutomaticTickMarksSpacing(true);

// Slide 2: show every third label, but keep a tick mark for every category.
auto manualSlide = presentation->get_Slides()->AddClone(slide);
auto manualChart = System::ExplicitCast<IChart>(manualSlide->get_Shape(0));
auto manualAxis = manualChart->get_Axes()->get_HorizontalAxis();
manualAxis->set_IsAutomaticTickLabelSpacing(false);
manualAxis->set_TickLabelSpacing(3);
manualAxis->set_IsAutomaticTickMarksSpacing(false);
manualAxis->set_TickMarksSpacing(1);

// Slide 3: let the chart choose both intervals again.
auto restoredSlide = presentation->get_Slides()->AddClone(manualSlide);
auto restoredChart = System::ExplicitCast<IChart>(restoredSlide->get_Shape(0));
restoredChart->get_Axes()->get_HorizontalAxis()->set_IsAutomaticTickLabelSpacing(true);
restoredChart->get_Axes()->get_HorizontalAxis()->set_IsAutomaticTickMarksSpacing(true);

presentation->Save(u"CategoryAxisIntervals.pptx", SaveFormat::Pptx);

**自動間隔(スライド 1):**このレンダリングでは 2 番目ごとのカテゴリラベルが表示され、2 行に折り返されます。自動結果はチャートサイズ、フォント、レンダラによって変わります。

すべての 24 列が表示された自動カテゴリラベル間隔

**手動間隔(スライド 2):**3 番目ごとのラベルが 1 行に表示され、目盛りはすべてのカテゴリ間隔に残ります。ラベルがない列も含めて 24 列すべてが同じ値で表示されます。スライド 3 で自動表示に戻ります。

すべての 24 列が表示された手動間隔(間隔 3)

正しい軸と間隔を選択する

テキストカテゴリ軸(列、折れ線、エリア、棒グラフのカテゴリ軸)に対してこのカテゴリカウント間隔を使用します。列グラフでは水平軸、水平棒グラフではカテゴリ軸が垂直になるため、get_VerticalAxis に適用します。目盛り間隔は、系列軸を持つチャートでも同様に適用できます。

カテゴリラベル間隔を数値軸のスケール設定に使用しないでください。値軸では set_MajorUnit が値の差を指定します(例: 10 なら 0, 10, 20,…)。カテゴリラベル間隔 3 はデータ値に関係なくカテゴリ位置を数えます。散布図やバブルチャートはテキストカテゴリ軸ではなく値軸を使用します。日付軸の場合は、Change a Category Axis に記載の時間ベースの主要単位とスケールを使用してください。

カテゴリ軸値の日付書式を設定する

この例ではデフォルト データを 4 つの年次値に置き換えます。日付は最初のワークシート(インデックス 0)に OLE Automation のシリアル番号として格納されています。set_CategoryAxisType で日付軸を選択し、set_IsNumberFormatLinkedToSource でソース書式リンクを無効にし、set_NumberFormat に yyyy を指定して、セル書式に関係なくカテゴリラベルが 4 桁年表示になるようにします。

#include <DOM/Presentation.h>
#include <DOM/ISlide.h>
#include <DOM/IShapeCollection.h>
#include <DOM/IChart.h>
#include <DOM/Chart/ChartType.h>
#include <DOM/Chart/IAxesManager.h>
#include <DOM/Chart/IAxis.h>
#include <Export/SaveFormat.h>
#include <DOM/Chart/IChartData.h>
#include <DOM/Chart/CategoryAxisType.h>
#include <DOM/Chart/IChartDataWorkbook.h>
#include <DOM/Chart/IChartDataCell.h>
#include <DOM/Chart/IChartCategoryCollection.h>
#include <DOM/Chart/IChartSeriesCollection.h>
#include <DOM/Chart/IChartSeries.h>
#include <DOM/Chart/IChartDataPointCollection.h>
#include <system/object_ext.h>
#include <system/date_time.h>

using namespace Aspose::Slides;
using namespace Aspose::Slides::Charts;
using namespace Aspose::Slides::Export;

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

auto chart = slide->get_Shapes()->AddChart(ChartType::Line, 50, 50, 450, 300);

chart->get_ChartData()->get_Categories()->Clear();
chart->get_ChartData()->get_Series()->Clear();

auto workbook = chart->get_ChartData()->get_ChartDataWorkbook();
workbook->Clear(0);

auto series = chart->get_ChartData()->get_Series()->Add(ChartType::Line);
for (auto i = 0; i < 4; i++)
{
    auto date = System::DateTime(2015 + i, 1, 1);
    auto categoryCell = workbook->GetCell(0, i + 1, 0, System::ObjectExt::Box(date.ToOADate()));
    chart->get_ChartData()->get_Categories()->Add(categoryCell);

    auto valueCell = workbook->GetCell(0, i + 1, 1, System::ObjectExt::Box(i + 1));
    series->get_DataPoints()->AddDataPointForLineSeries(valueCell);
}

chart->get_Axes()->get_HorizontalAxis()->set_CategoryAxisType(CategoryAxisType::Date);
chart->get_Axes()->get_HorizontalAxis()->set_IsNumberFormatLinkedToSource(false);
chart->get_Axes()->get_HorizontalAxis()->set_NumberFormat(u"yyyy");

presentation->Save(u"DateAxisFormat.pptx", SaveFormat::Pptx);

チャート軸タイトルの回転角度を設定する

垂直軸のタイトルを有効にするには set_HasTitle を使用し、タイトルテキストを設定した上で set_RotationAngle で回転させます。角度は度数で指定します。この例では列グラフの値軸タイトルを 90 度回転させて保存します。

#include <DOM/Presentation.h>
#include <DOM/ISlide.h>
#include <DOM/IShapeCollection.h>
#include <DOM/IChart.h>
#include <DOM/Chart/ChartType.h>
#include <DOM/Chart/IAxesManager.h>
#include <DOM/Chart/IAxis.h>
#include <Export/SaveFormat.h>
#include <DOM/Chart/IChartTextFormat.h>
#include <DOM/Chart/IChartTextBlockFormat.h>
#include <DOM/Chart/IChartTitle.h>

using namespace Aspose::Slides;
using namespace Aspose::Slides::Charts;
using namespace Aspose::Slides::Export;

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

auto chart = slide->get_Shapes()->AddChart(ChartType::ClusteredColumn, 50, 50, 450, 300);
chart->get_Axes()->get_VerticalAxis()->set_HasTitle(true);
chart->get_Axes()->get_VerticalAxis()->get_Title()->AddTextFrameForOverriding(u"Value");
chart->get_Axes()->get_VerticalAxis()->get_Title()->get_TextFormat()->get_TextBlockFormat()->set_RotationAngle(90);

presentation->Save(u"RotatedAxisTitle.pptx", SaveFormat::Pptx);

カテゴリ軸または値軸の軸位置を設定する

set_AxisBetweenCategories を使用して、値軸がカテゴリ軸の間の目盛り位置で交差するか、カテゴリ目盛り位置で交差するかを制御します。このプロパティはカテゴリ軸に適用されます。例では列グラフの水平カテゴリ軸に true を設定し、結果を保存します。

#include <DOM/Presentation.h>
#include <DOM/ISlide.h>
#include <DOM/IShapeCollection.h>
#include <DOM/IChart.h>
#include <DOM/Chart/ChartType.h>
#include <DOM/Chart/IAxesManager.h>
#include <DOM/Chart/IAxis.h>
#include <Export/SaveFormat.h>

using namespace Aspose::Slides;
using namespace Aspose::Slides::Charts;
using namespace Aspose::Slides::Export;

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

auto chart = slide->get_Shapes()->AddChart(ChartType::ClusteredColumn, 50, 50, 450, 300);
chart->get_Axes()->get_HorizontalAxis()->set_AxisBetweenCategories(true);

presentation->Save(u"AxisBetweenCategories.pptx", SaveFormat::Pptx);

チャート値軸の表示単位を設定する

set_DisplayUnit を使用すると、データ自体は変更せずに値軸ラベルのスケールを変更できます。DisplayUnitType を Millions に設定すれば、60,000,000 が 60 と表示されます。例では列グラフを作成し、垂直軸にミリオン表示単位を適用します。

#include <DOM/Presentation.h>
#include <DOM/ISlide.h>
#include <DOM/IShapeCollection.h>
#include <DOM/IChart.h>
#include <DOM/Chart/ChartType.h>
#include <DOM/Chart/IAxesManager.h>
#include <DOM/Chart/IAxis.h>
#include <Export/SaveFormat.h>
#include <DOM/Chart/DisplayUnitType.h>

using namespace Aspose::Slides;
using namespace Aspose::Slides::Charts;
using namespace Aspose::Slides::Export;

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

auto chart = slide->get_Shapes()->AddChart(ChartType::ClusteredColumn, 50, 50, 450, 300);
chart->get_Axes()->get_VerticalAxis()->set_DisplayUnit(DisplayUnitType::Millions);

presentation->Save(u"Result.pptx", SaveFormat::Pptx);

対数値軸を構成する

対数値軸は、正の値が数桁にわたって変化する場合に有用です。たとえば、あるサービスが 5 期間で 10、100、1,000、10,000、100,000 のリクエスト数を受けたとします。線形スケールでは小さい値がゼロ付近に密集して見えますが、対数スケールでは等間隔が等比率を表すため、相対的な増加が比較しやすくなります。

値軸に対して true を渡して set_IsLogarithmic を呼び出し、set_LogBase で基数を指定します。これにより既存の値の表示方法が変わりますが、データや対数トレンドラインは変更されません。トレンドラインは別機能です。

値軸と基数の選択

折れ線または列グラフの場合、値軸は get_VerticalAxis です。水平棒グラフの場合は get_HorizontalAxis です。散布図やバブルチャートは 2 つの値軸を持つため、X 値には水平軸、Y 値には垂直軸、あるいは両方を使用します。カテゴリ軸には対数スケーリングを適用しないでください。

既定の対数基数は 10 です。PPTX チャートでは 2 から 1000(整数)までの基数が使用でき、2.5 のような小数基数もサポートされています。基数 10 の場合、10、100、1,000、10,000 といったべき乗が等間隔になります。

線形スケーリングと対数スケーリングの比較

以下の自己完結型サンプルは、最初のスライドに 5 つのリクエスト数を持つ折れ線グラフを作成し、2 枚目のスライドを複製して 10 進対数値軸を有効にします。両方のスライドは同じデータを保持します。

#include <DOM/Presentation.h>
#include <DOM/ISlide.h>
#include <DOM/ISlideCollection.h>
#include <DOM/IShapeCollection.h>
#include <DOM/IChart.h>
#include <DOM/Chart/ChartType.h>
#include <DOM/Chart/IAxesManager.h>
#include <DOM/Chart/IAxis.h>
#include <DOM/Chart/IChartData.h>
#include <DOM/Chart/IChartDataWorkbook.h>
#include <DOM/Chart/IChartDataCell.h>
#include <DOM/Chart/IChartCategoryCollection.h>
#include <DOM/Chart/IChartSeriesCollection.h>
#include <DOM/Chart/IChartSeries.h>
#include <DOM/Chart/IChartDataPointCollection.h>
#include <Export/SaveFormat.h>
#include <system/object_ext.h>
#include <array>

using namespace Aspose::Slides;
using namespace Aspose::Slides::Charts;
using namespace Aspose::Slides::Export;

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

auto chart = slide->get_Shapes()->AddChart(ChartType::Line, 30, 40, 660, 200);

chart->set_HasLegend(false);

chart->get_ChartData()->get_Categories()->Clear();
chart->get_ChartData()->get_Series()->Clear();

auto workbook = chart->get_ChartData()->get_ChartDataWorkbook();
workbook->Clear(0);

auto series = chart->get_ChartData()->get_Series()->Add(ChartType::Line);
auto values = std::array<double, 5>{10, 100, 1000, 10000, 100000};

for (auto i = 0; i < values.size(); i++)
{
    auto categoryName = System::String::Format(u"Period {0}", i + 1);
    auto categoryCell = workbook->GetCell(0, i + 1, 0, System::ObjectExt::Box(categoryName));
    chart->get_ChartData()->get_Categories()->Add(categoryCell);

    auto valueCell = workbook->GetCell(0, i + 1, 1, System::ObjectExt::Box(values[i]));
    series->get_DataPoints()->AddDataPointForLineSeries(valueCell);
}

// 最初のスライドは線形のままにし、コピーの値軸スケールだけを変更します。
auto logarithmicSlide = presentation->get_Slides()->AddClone(slide);
auto logarithmicChart = System::ExplicitCast<IChart>(logarithmicSlide->get_Shape(0));
auto valueAxis = logarithmicChart->get_Axes()->get_VerticalAxis();
valueAxis->set_IsLogarithmic(true);
valueAxis->set_LogBase(10);

presentation->Save(u"LogarithmicAxis.pptx", SaveFormat::Pptx);

線形スケール(スライド 1): 等しい垂直距離はリクエスト数の等しい差を表します。最大値がチャートを支配し、他の値が見えにくくなります。

線形値軸で 5 つのリクエスト数を示す PowerPoint の折れ線グラフ

対数スケール(スライド 2): 等しい垂直距離は等しい比率を表します。各期間のリクエスト数は前の期間の 10 倍になるため、基数 10 のスケール上では直線的に上昇します。軸ラベルは元のリクエスト数をそのまま表示します。

PowerPoint の同じ 5 つのリクエスト数を基数 10 の対数値軸で示す

ゼロおよび負の値の取り扱い

対数スケールは正の値のみを表示できます。使用前にデータを確認してください。ゼロや負の値が意味を持つ場合は線形スケールを維持するか、別途表示してください。分析上それらの値を除外する場合は除外理由を明示してください。小さな正の数で置き換えるとデータの意味が変わります。対数軸の手動最小値・最大値も正の数でなければなりません。

FAQ

軸が交差する位置(軸交差点)を設定するにはどうすればよいですか?

set_CrossType を使用して交差動作を選択します。数値の交差点を指定する場合は set_CrossAt を使用します。これらの設定により、軸交差点を適切な基準線に移動できます。

目盛りラベルを軸に対してどのように配置できますか?

set_TickLabelPosition に TickLabelPositionType のいずれかの値(Low、High、NextTo、None)を渡します。目盛り自体を制御するには、set_MajorTickMark または set_MinorTickMark を使用します。これらはラベル配置とは別の設定です。