使用 C++ 在簡報中自訂圖表座標軸

概述

本文說明如何使用 Aspose.Slides for C++ 自訂圖表座標軸。它涵蓋計算出的座標軸值、切換圖表列與欄、座標軸可見性、類別標籤與刻度間隔、日期類別與格式、標題旋轉、座標軸定位、顯示單位以及對數比例等。

Get the Max Values on the Vertical Axis on Charts

建立一個 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);

Swap the Data between Axes

使用 SwitchRowColumn 交換圖表資料中系列與類別的角色。先前的每個類別會變成系列,先前的每個系列會變成類別。此操作會改變資料的分組方式,並不會交換水平與垂直座標軸。範例使用 SetRange 將預設資料繫結至 Sheet1!A1:D5(包括標題列與類別欄),然後再切換列與欄。最終會儲存一個包含四個系列與三個類別的圖表。

#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);

Reverse the Plotting Order of an Axis

使用 set_IsPlotOrderReversed 來反轉類別或值座標軸。例如,水平長條圖可以依工作表的順序(規劃、開發、測試)從上到下顯示專案階段。反轉座標軸會改變繪製方向,並不會對工作表資料進行排序或重新排列。相較之下,SwitchRowColumn 會交換系列與類別的角色。

以下範例建立一個包含三個階段與一個系列的水平長條圖,並儲存三張投影片:正常順序、類別反轉、值反轉。每個副本使用相同的資料集,且每張投影片僅反轉一條座標軸。

#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);
}

// Slide 2: display the first category at the top.
 // 第 2 張投影片:將第一個類別顯示在頂部。
 // Keep the horizontal value axis at the bottom.
 // 保持水平值座標軸在底部。
auto categorySlide = presentation->get_Slides()->AddClone(slide);
auto categoryChart = System::ExplicitCast<IChart>(categorySlide->get_Shape(0));
categoryChart->get_Axes()->get_VerticalAxis()->set_IsPlotOrderReversed(true);
// Keep the horizontal value axis at the bottom.
 // 保持水平值座標軸在底部。
categoryChart->get_Axes()->get_VerticalAxis()->set_CrossType(CrossesType::Maximum);

// Slide 3: reverse only the horizontal numeric scale.
 // 第 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)
水平長條圖(類別與值座標軸順序正常) 水平長條圖(類別自上而下顯示) 水平長條圖(值座標軸反轉且零位於右側)
類別從下往上排列;值從左至右遞增。 規劃、開發、測試從上而下排列。值與工作表列順序保持不變。 類別保持正常順序;值從右至左遞增。類別座標軸在右側零點交叉。

Disable the Vertical Axis for Line Charts

使用 set_IsVisible 並傳入 false 以隱藏垂直座標軸。範例建立一個預設資料的折線圖,並將垂直座標軸隱藏後儲存。

#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);

Disable the Horizontal Axis for Line Charts

使用 set_IsVisible 並傳入 false 以隱藏水平座標軸。範例建立一個預設資料的折線圖,並將水平座標軸隱藏後儲存。

#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);

Change a Category Axis

使用 set_CategoryAxisType 可選擇日期或文字類別座標軸。此範例需要 ExistingChart.pptx,其中第一張投影片的第一個圖形為圖表,且類別儲存格內含 Excel 數值日期。範例將水平座標軸變更為日期座標軸。接著呼叫 set_IsAutomaticMajorUnit 並傳入 false、set_MajorUnit 傳入 1、以及 set_MajorUnitScale 設為月份,以在每月間隔放置主要刻度。

#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);

Control Category Axis Label Intervals

當圖表包含許多類別時,可在不移除類別或資料點的前提下減少可見座標軸標籤的數量。先使用 set_IsAutomaticTickLabelSpacing 並傳入 false,再以 set_TickLabelSpacing 設定所需的類別間隔。對於以正常順序排列的文字類別,計數從第一個類別開始:

間隔 範例中顯示的標籤
1 類別 1、類別 2、類別 3、… 類別 24
2 類別 1、類別 3、類別 5、… 類別 23
3 類別 1、類別 4、類別 7、… 類別 22

間隔為 3 時會顯示每第三個標籤,兩個標籤之間會被隱藏,且不會移除相對應的欄位。自動間隔會根據可用空間自動選擇間隔,未必會顯示全部標籤。

刻度線有獨立的控制方式。使用 set_IsAutomaticTickMarksSpacing 並傳入 false,再以 set_TickMarksSpacing 設定刻度間隔。例如,1 會在每個類別間隔放置刻度線,而標籤僅每第三個類別顯示一次。使用 set_MajorTickMark 設定可見樣式,即可看到結果。將任一自動間隔屬性重新設為 true,圖表便會重新自行決定間隔。

以下範例一次建立 24 個類別與一個系列,然後在 CategoryAxisIntervals.pptx 中儲存三張投影片:自動間隔、手動標籤間隔(刻度獨立)、以及恢復自動間隔。兩個副本保留原始圖表資料,無需提供輸入簡報。水平標籤文字的差異讓密度變化一目了然。

#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);

// 第 2 張投影片:顯示每三個標籤,但對每個類別保留刻度線。
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);

// 第 3 張投影片:讓圖表再次自行選擇兩種間隔。
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);

Automatic spacing (slide 1): 在此呈現中,每第二個類別標籤會顯示,且會換行成兩行。自動結果會因圖表大小、字型與渲染器而異。

Automatic category label spacing with all 24 columns visible

Manual spacing (slide 2): 每第三個標籤顯示在同一行,而刻度線仍保持在每個類別間隔。所有 24 個欄位(即使沒有標籤)仍保持可見且數值相同。第 3 張投影片會恢復如上所示的自動外觀。

Manual category label interval of three with all 24 columns visible

Choose the Correct Axis and Interval

對於文字類別座標軸(例如柱狀圖、折線圖、區域圖或長條圖的類別座標軸),請使用此類別計數間隔。在柱狀圖中,它是水平座標軸;在水平長條圖中,類別座標軸是垂直的,請對 get_VerticalAxis 套用這些設定。刻度間隔同樣適用於具有系列座標軸的圖表。

切勿使用類別標籤間隔來設定值座標軸的數字刻度。於值座標軸上, set_MajorUnit 會指定數值差異,例如 10 的主要單位會在 0、10、20… 等位置產生刻度。類別標籤間隔 3 則是以類別位置計算,與實際資料值無關。散佈圖與氣泡圖使用值座標軸而非文字類別座標軸。若為日期座標軸,請參照「Change a Category Axis」中的時間單位與比例設定。

Set the Date Format for Category Axis Values

此範例以四筆年度數值取代預設圖表資料。日期以 OLE Automation 序號儲存在第一個工作表(索引 0)中。使用 set_CategoryAxisType 選取日期座標軸,並以 set_IsNumberFormatLinkedToSource 停用來源格式連結,最後使用 set_NumberFormat 指定 yyyy,使類別標籤顯示四位數年份且不受儲存格格式影響。

#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 a Rotation Angle for a Chart Axis Title

使用 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 the Axis Position on a Category or Value Axis

使用 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 the Display Unit on a Chart Value Axis

使用 set_DisplayUnit 可在不變更底層資料的前提下,調整值座標軸標籤的比例。將 DisplayUnitType 設為 Millions 後,60000000 會顯示為 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);

Configure a Logarithmic Value Axis

對數值座標軸在正值跨越多個数量级時非常有用。例如,某服務在五個時期內分別收到 10、100、1,000、10,000、100,000 個請求。在線性比例下,較小的值會聚集在接近零的位置。對數比例則使相對增長更易比較:相等的距離代表相等的比例而非相等的差異。

在值座標軸上呼叫 set_IsLogarithmic 並傳入 true,再以 set_LogBase 設定基底。此操作會改變現有值的顯示方式,並不會改變資料或新增對數趨勢線。趨勢線是針對資料的擬合關係,屬於另一項功能。

Choose the Value Axis and Base

對於折線圖或柱狀圖,值座標軸為 get_VerticalAxis。對於水平長條圖,則為 get_HorizontalAxis。散佈圖與氣泡圖有兩條值座標軸,請為 X 值選擇水平座標軸,為 Y 值選擇垂直座標軸,或同時設定。切勿將對數比例套用於類別座標軸。

預設的對數基底為 10。對於 PPTX 圖表,基底可設定為 2 至 1000(含)之間的任意整數,亦支援小數基底(例如 2.5)。以基底 10 為例,10、100、1,000、10,000 等次方會等距排列。

Compare Linear and Logarithmic Scaling

以下範例建立一個折線圖,在第一張投影片上顯示五筆請求量,然後複製該投影片並在第二張投影片上啟用基底為 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);

Linear scale (slide 1): 等距的垂直距離代表請求量的相等差異。最大的值主導圖表,使較小的值難以辨識。

PowerPoint 中的折線圖,顯示線性值座標軸上的五個請求次數

Logarithmic scale (slide 2): 等距的垂直距離代表相等的比例。每個時期的請求量是前一期的十倍,因此在基底為 10 的比例上點會形成一條直線。座標軸標籤仍顯示原始的請求次數。

PowerPoint 中的相同五個請求次數,使用 10 為底的對數值座標軸

Handle Zero and Negative Values

對數比例只能顯示嚴格正數。啟用前請先檢查資料。如果零或負值具有實際意義,請保留線性比例或另行顯示。若將這些值排除於分析之外,請說明排除的原因。切勿悶聲將其替換為小的正數,因為這會改變資料的意義。任何手動設定的最小值或最大值亦必須為正數。

FAQ

How do I set the value at which one axis crosses the other (axis crossing)?

使用 set_CrossType 以選擇交叉行為。若要指定數值交叉點,請使用 set_CrossAt。這些設定可讓您將座標軸交叉點移至適當的基線。

How can I position tick labels relative to the axis?

使用 set_TickLabelPosition 搭配 TickLabelPositionType 的值:Low、High、NextTo 或 None。若要控制刻度線本身,請使用 set_MajorTickMark 或 set_MinorTickMark,這與標籤定位是分開的設定。