在 C++ 簡報中管理圖表資料系列

概觀

圖表可以將其繪製的值儲存在工作簿儲存格中或直接作為數值文字常量。IChartSeries 代表一組相關的值,每個 IChartDataPoint 代表該系列中的一個點。IChartCategory 物件提供系列共用的標籤或分組值。 在使用預設資料建立的圖表中,系列名稱、類別和點的值使用來自圖表工作簿的 IChartDataCell 物件。

對於典型的類別圖表,預設工作簿使用第 0 列儲存系列名稱,第 0 行儲存類別名稱,其餘儲存格則存放系列值。傳遞給 IChartDataWorkbook::GetCell 的工作表、列和欄索引採用從零開始的計算方式。此佈局在建立預設資料圖表時很有用,但不要假設每個現有圖表都使用它。對於已載入的簡報,在變更工作簿值之前,請檢查系列、類別和資料點所參照的儲存格。

圖表設定具有三種不同的範圍:

當未設定明確的點或系列填色時,圖表樣式和主題會決定自動外觀。當同時存在系列與點的格式設定時,點的格式優先於該點。

chart-series-powerpoint

對於群集圓柱體圖表,請參閱變更 3D 系列類型或條形外觀如何影響第一個系列。

使用數值文字常量設定系列值

當系列值來自應用程式資料或計算且不需要工作表儲存格參照時,請使用數值文字常量。在將每個點的 get_Value 透過 set_AsLiteralDouble 指派之前,先呼叫 set_DataSourceTypeForValues 並傳入 DataSourceType::DoubleLiterals。

此集合設定會控制該系列中所有點值的來源類型。每個點的 IBaseChartValue::get_DataSourceType 會回報已選取的類型;請透過集合設定變更它。使用 Worksheet 時,請使用 get_AsCell 取得參照的儲存格。使用 DoubleLiterals 時,請使用 get_AsLiteralDouble 或 set_AsLiteralDouble 直接讀寫數字。

以下範例建立一個具有預設系列與類別的直條圖,然後將計算出的 10、20 等值指派給其第一個系列的現有點。接著儲存簡報、重新開啟,並列印儲存的值。

#include <DOM/Chart/ChartType.h>
#include <DOM/Chart/DataSourceType.h>
#include <DOM/Chart/IChartData.h>
#include <DOM/Chart/IChartDataPoint.h>
#include <DOM/Chart/IChartDataPointCollection.h>
#include <DOM/Chart/IChartSeries.h>
#include <DOM/Chart/IChartSeriesCollection.h>
#include <DOM/Chart/IDoubleChartValue.h>
#include <DOM/IChart.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/Presentation.h>
#include <Export/SaveFormat.h>
#include <system/console.h>
#include <system/object_ext.h>
#include <system/shared_ptr.h>

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

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

auto chart = slide->get_Shapes()->AddChart(ChartType::ClusteredColumn, 20, 20, 500, 300);
auto series = chart->get_ChartData()->get_Series()->idx_get(0);

series->get_DataPoints()->set_DataSourceTypeForValues(DataSourceType::DoubleLiterals);
for (auto i = 0; i < series->get_DataPoints()->get_Count(); i++)
{
    series->get_DataPoints()->idx_get(i)->get_Value()->set_AsLiteralDouble((i + 1) * 10);
}

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

auto reopenedPresentation = MakeObject<Presentation>(u"numeric_literals.pptx");
auto reopenedSlide = reopenedPresentation->get_Slide(0);
auto reopenedChart = ExplicitCast<IChart>(reopenedSlide->get_Shape(0));
auto reopenedSeries = reopenedChart->get_ChartData()->get_Series()->idx_get(0);
for (auto i = 0; i < reopenedSeries->get_DataPoints()->get_Count(); i++)
{
    auto point = reopenedSeries->get_DataPoints()->idx_get(i);
    Console::WriteLine(point->get_Value()->get_AsLiteralDouble());
}
reopenedPresentation->Dispose();

點值的來源類型與 IChartData::get_DataSourceType 分開,後者識別圖表的內部或外部工作簿設定。使用數值文字常量本身不會移除內嵌工作簿。在此範例中,工作簿仍保留用於類別標籤、系列名稱以及其他系列。請參閱圖表工作簿以了解工作簿管理。

對於散點圖和氣泡圖,請呼叫 set_DataSourceTypeForXValues 與 set_DataSourceTypeForYValues 以選擇 get_XValue 與 get_YValue 的來源。氣泡圖亦使用 set_DataSourceTypeForBubbleSizes 來設定 get_BubbleSize。在透過該值的 set_AsLiteralDouble 方法指派數字之前,請將每個相關來源設定為 DoubleLiterals。

設定圖表系列重疊

IChartSeries::get_Overlap 會回報 2D 圖表中條形或柱形的重疊程度,範圍為 -100 到 100 百分比。它是父系列群組設定的唯讀投影。呼叫 IChartSeriesGroup::set_Overlap 可更新該群組中所有相容系列。此選項適用於顯示分組條形或柱形的圖表類型;不會影響組合圖表中不相關的系列群組。

以下範例為包含第一個系列的群組設定重疊值:

#include <cstdint>
#include <DOM/Chart/ChartType.h>
#include <DOM/Chart/IChartData.h>
#include <DOM/Chart/IChartSeries.h>
#include <DOM/Chart/IChartSeriesCollection.h>
#include <DOM/Chart/IChartSeriesGroup.h>
#include <DOM/IChart.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/Presentation.h>
#include <Export/SaveFormat.h>
#include <system/shared_ptr.h>

using Aspose::Slides::Charts::ChartType;
using Aspose::Slides::Export::SaveFormat;
using Aspose::Slides::Presentation;

const int firstSlideIndex = 0;
const int firstSeriesIndex = 0;
const int8_t overlapPercent = 30;

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

// 新增的圖表包含示範系列、類別和數值。
auto chart = slide->get_Shapes()->AddChart(ChartType::ClusteredColumn, 20.0f, 20.0f, 500.0f, 200.0f);

auto seriesCollection = chart->get_ChartData()->get_Series();
auto series = seriesCollection->idx_get(firstSeriesIndex);
series->get_ParentSeriesGroup()->set_Overlap(overlapPercent);

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

結果:

The series overlap

變更系列填色

使用 IChartSeries::get_Format 為整個系列設定預設填色。如果某個點已明確設定填色,則其 IChartDataPoint::get_Format 會覆寫該系列的填色。

以下範例將第一個系列的填色設定為實心藍色:

#include <DOM/Chart/ChartType.h>
#include <DOM/Chart/IChartData.h>
#include <DOM/Chart/IChartSeries.h>
#include <DOM/Chart/IChartSeriesCollection.h>
#include <DOM/Chart/IFormat.h>
#include <DOM/FillType.h>
#include <DOM/IChart.h>
#include <DOM/IColorFormat.h>
#include <DOM/IFillFormat.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/Presentation.h>
#include <Export/SaveFormat.h>
#include <drawing/color.h>
#include <system/shared_ptr.h>

using Aspose::Slides::Charts::ChartType;
using Aspose::Slides::Export::SaveFormat;
using Aspose::Slides::FillType;
using Aspose::Slides::Presentation;
using System::Drawing::Color;

const int firstSlideIndex = 0;
const int firstSeriesIndex = 0;

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

auto chart = slide->get_Shapes()->AddChart(ChartType::ClusteredColumn, 20.0f, 20.0f, 500.0f, 200.0f);

auto seriesCollection = chart->get_ChartData()->get_Series();
auto series = seriesCollection->idx_get(firstSeriesIndex);
auto seriesColor = Color::get_Blue();
series->get_Format()->get_Fill()->set_FillType(FillType::Solid);
series->get_Format()->get_Fill()->get_SolidFillColor()->set_Color(seriesColor);

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

結果:

The color of the series

變更系列名稱

系列名稱儲存在圖表資料工作簿中,通常顯示在圖例中。對於為群集直條圖所建立的預設工作簿,儲存格 B1 位於第 0 列第 1 欄,內含第一個系列的名稱。以下範例中的具名常數使此結構顯式化:

#include <DOM/Chart/ChartType.h>
#include <DOM/Chart/IChartData.h>
#include <DOM/Chart/IChartDataCell.h>
#include <DOM/Chart/IChartDataWorkbook.h>
#include <DOM/IChart.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/Presentation.h>
#include <Export/SaveFormat.h>
#include <system/object_ext.h>
#include <system/shared_ptr.h>
#include <system/string.h>

using Aspose::Slides::Charts::ChartType;
using Aspose::Slides::Export::SaveFormat;
using Aspose::Slides::Presentation;
using System::ObjectExt;
using System::String;

const int firstSlideIndex = 0;
const int worksheetIndex = 0;
const int seriesNameRowIndex = 0;
const int firstSeriesColumnIndex = 1;

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

auto chart = slide->get_Shapes()->AddChart(ChartType::ClusteredColumn, 20.0f, 20.0f, 500.0f, 200.0f);

auto workbook = chart->get_ChartData()->get_ChartDataWorkbook();
auto seriesNameCell = workbook->GetCell(worksheetIndex, seriesNameRowIndex, firstSeriesColumnIndex);
auto seriesName = ObjectExt::Box<String>(u"Revenue");
seriesNameCell->set_Value(seriesName);

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

您也可以直接更新由 IChartSeries::get_Name 參照的儲存格。此做法避免假設現有圖表中的特定列與欄:

#include <DOM/Chart/ChartType.h>
#include <DOM/Chart/IChartCellCollection.h>
#include <DOM/Chart/IChartData.h>
#include <DOM/Chart/IChartDataCell.h>
#include <DOM/Chart/IChartSeries.h>
#include <DOM/Chart/IChartSeriesCollection.h>
#include <DOM/Chart/IStringChartValue.h>
#include <DOM/IChart.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/Presentation.h>
#include <Export/SaveFormat.h>
#include <system/object_ext.h>
#include <system/shared_ptr.h>
#include <system/string.h>

using Aspose::Slides::Charts::ChartType;
using Aspose::Slides::Export::SaveFormat;
using Aspose::Slides::Presentation;
using System::ObjectExt;
using System::String;

const int firstSlideIndex = 0;
const int firstSeriesIndex = 0;
const int firstNameCellIndex = 0;

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

auto chart = slide->get_Shapes()->AddChart(ChartType::ClusteredColumn, 20.0f, 20.0f, 500.0f, 200.0f);

auto seriesCollection = chart->get_ChartData()->get_Series();
auto series = seriesCollection->idx_get(firstSeriesIndex);
auto seriesNameCells = series->get_Name()->get_AsCells();
auto seriesNameCell = seriesNameCells->idx_get(firstNameCellIndex);
auto seriesName = ObjectExt::Box<String>(u"Revenue");
seriesNameCell->set_Value(seriesName);

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

結果:

The series name

從多個儲存格建立具有名稱的系列

當產品名稱與報告期間分別儲存在不同工作簿儲存格時,合成系列名稱非常實用。例如,您可以將 B1 中的 Product A 與 C1 中的 2026 合併為單一系列名稱,同時保持兩個部份皆連結至其來源儲存格。

使用 IChartDataWorkbook::GetCellCollection 取得名稱範圍,然後將該集合傳遞給 IChartSeriesCollection::Add。skipHiddenCells 參數控制是否排除隱藏儲存格:true 會排除,false 會包含。本範例使用 false 以包含名稱範圍內的每個儲存格。

以下範例建立一個簡報,包含一個系列與兩個資料點。儲存格 B1:C1 只提供系列名稱;A2:A3 提供類別標籤;B2:B3 提供數值。

#include <DOM/Chart/ChartType.h>
#include <DOM/Chart/IChartCategoryCollection.h>
#include <DOM/Chart/IChartCellCollection.h>
#include <DOM/Chart/IChartData.h>
#include <DOM/Chart/IChartDataCell.h>
#include <DOM/Chart/IChartDataPointCollection.h>
#include <DOM/Chart/IChartDataWorkbook.h>
#include <DOM/Chart/IChartSeries.h>
#include <DOM/Chart/IChartSeriesCollection.h>
#include <DOM/IChart.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/Presentation.h>
#include <Export/SaveFormat.h>
#include <system/object_ext.h>
#include <system/shared_ptr.h>
#include <system/string.h>

using namespace Aspose::Slides;
using namespace Aspose::Slides::Charts;
using namespace Aspose::Slides::Export;
using System::ObjectExt;
using System::String;

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

auto chart = slide->get_Shapes()->AddChart(ChartType::ClusteredColumn, 50.0f, 50.0f, 620.0f, 180.0f);
auto chartData = chart->get_ChartData();

chartData->get_Series()->Clear();
chartData->get_Categories()->Clear();
chart->set_HasLegend(true);

auto workbook = chartData->get_ChartDataWorkbook();
workbook->Clear(0);

// 這兩個儲存格提供系列名稱。
auto productName = ObjectExt::Box<String>(u"Product A");
auto reportingPeriod = ObjectExt::Box<String>(u"2026");
workbook->GetCell(0, 0, 1, productName);
workbook->GetCell(0, 0, 2, reportingPeriod);
auto nameCells = workbook->GetCellCollection(u"Sheet1!$B$1:$C$1", false);
auto series = chartData->get_Series()->Add(nameCells, ChartType::ClusteredColumn);

// 分別的儲存格提供類別和數值資料點。
auto northLabel = ObjectExt::Box<String>(u"North");
auto southLabel = ObjectExt::Box<String>(u"South");
auto northCategory = workbook->GetCell(0, 1, 0, northLabel);
auto southCategory = workbook->GetCell(0, 2, 0, southLabel);
chartData->get_Categories()->Add(northCategory);
chartData->get_Categories()->Add(southCategory);
auto northAmount = ObjectExt::Box<int>(120);
auto southAmount = ObjectExt::Box<int>(150);
auto northValue = workbook->GetCell(0, 1, 1, northAmount);
auto southValue = workbook->GetCell(0, 2, 1, southAmount);
series->get_DataPoints()->AddDataPointForBarSeries(northValue);
series->get_DataPoints()->AddDataPointForBarSeries(southValue);

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

最終的系列名稱為 Product A 2026,兩個儲存格值之間有一個空格。圖例會將其顯示為單一條目。下圖說明結果:

Column chart with North and South values and the composite series name Product A 2026 in the legend

取得自動系列填色

IChartSeries::GetAutomaticSeriesColor 會回傳根據系列索引與圖表樣式計算出的顏色。這是當系列填色未明確定義時所使用的顏色。呼叫此方法只會讀取計算出的顏色,不會指派新的填色。

以下範例列印每個預設系列的自動顏色:

#include <DOM/Chart/ChartType.h>
#include <DOM/Chart/IChartData.h>
#include <DOM/Chart/IChartSeries.h>
#include <DOM/Chart/IChartSeriesCollection.h>
#include <DOM/IChart.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/Presentation.h>
#include <drawing/color.h>
#include <system/console.h>
#include <system/shared_ptr.h>
#include <system/string.h>

using Aspose::Slides::Charts::ChartType;
using Aspose::Slides::Presentation;
using System::Console;
using System::String;

const int firstSlideIndex = 0;

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

auto chart = slide->get_Shapes()->AddChart(ChartType::ClusteredColumn, 20.0f, 20.0f, 500.0f, 200.0f);

auto seriesCollection = chart->get_ChartData()->get_Series();
const int seriesCount = seriesCollection->get_Count();
for (int seriesIndex = 0; seriesIndex < seriesCount; seriesIndex++)
{
    auto series = seriesCollection->idx_get(seriesIndex);
    auto automaticColor = series->GetAutomaticSeriesColor();
    auto colorName = automaticColor.get_Name();
    auto outputLine = String::Format(u"Series {0}: {1}", seriesIndex, colorName);
    Console::WriteLine(outputLine);
}

presentation->Dispose();

預設圖表樣式的範例輸出:

Series 0: ff4f81bd
Series 1: ffc0504d
Series 2: ff9bbb59

實際顏色取決於圖表樣式與主題。

為圖表系列設定反轉填色

對於長條、柱形與氣泡系列,IChartSeries::set_InvertIfNegative 可在負值時使用不同的填色。將常規系列填色設定為實心,啟用反轉,並透過 IChartSeries::get_InvertedSolidFillColor 指派負值顏色。負數在工作簿中保持不變;僅變更其顯示顏色。

以下範例以單一系列取代預設圖表資料。工作表第 0 列儲存系列名稱,第 0 欄儲存類別名稱,第 1 欄儲存數值:

#include <DOM/Chart/ChartType.h>
#include <DOM/Chart/IChartCategoryCollection.h>
#include <DOM/Chart/IChartData.h>
#include <DOM/Chart/IChartDataCell.h>
#include <DOM/Chart/IChartDataPointCollection.h>
#include <DOM/Chart/IChartDataWorkbook.h>
#include <DOM/Chart/IChartSeries.h>
#include <DOM/Chart/IChartSeriesCollection.h>
#include <DOM/Chart/IFormat.h>
#include <DOM/FillType.h>
#include <DOM/IChart.h>
#include <DOM/IColorFormat.h>
#include <DOM/IFillFormat.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/Presentation.h>
#include <Export/SaveFormat.h>
#include <drawing/color.h>
#include <system/object_ext.h>
#include <system/shared_ptr.h>
#include <system/string.h>

using Aspose::Slides::Charts::ChartType;
using Aspose::Slides::Export::SaveFormat;
using Aspose::Slides::FillType;
using Aspose::Slides::Presentation;
using System::Drawing::Color;
using System::ObjectExt;
using System::String;

const int firstSlideIndex = 0;
const int worksheetIndex = 0;
const int headerRowIndex = 0;
const int categoryColumnIndex = 0;
const int firstSeriesColumnIndex = 1;
const int firstDataRowIndex = 1;
const int categoryCount = 3;

const String categoryNames[] = {u"Category 1", u"Category 2", u"Category 3"};
const int seriesValues[] = {-20, 50, -30};

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

auto chart = slide->get_Shapes()->AddChart(ChartType::ClusteredColumn, 20.0f, 20.0f, 500.0f, 200.0f);
auto chartData = chart->get_ChartData();
auto workbook = chartData->get_ChartDataWorkbook();

auto seriesCollection = chartData->get_Series();
seriesCollection->Clear();
chartData->get_Categories()->Clear();

auto seriesName = ObjectExt::Box<String>(u"Series 1");
auto seriesNameCell = workbook->GetCell(worksheetIndex, headerRowIndex, firstSeriesColumnIndex, seriesName);
auto chartType = chart->get_Type();
auto series = seriesCollection->Add(seriesNameCell, chartType);

for (int categoryIndex = 0; categoryIndex < categoryCount; categoryIndex++)
{
    const int dataRowIndex = firstDataRowIndex + categoryIndex;
    auto categoryName = categoryNames[categoryIndex];
    const int seriesValue = seriesValues[categoryIndex];

    auto boxedCategoryName = ObjectExt::Box<String>(categoryName);
    auto categoryCell = workbook->GetCell(worksheetIndex, dataRowIndex, categoryColumnIndex, boxedCategoryName);
    chartData->get_Categories()->Add(categoryCell);

    auto boxedSeriesValue = ObjectExt::Box<int>(seriesValue);
    auto valueCell = workbook->GetCell(worksheetIndex, dataRowIndex, firstSeriesColumnIndex, boxedSeriesValue);
    series->get_DataPoints()->AddDataPointForBarSeries(valueCell);
}

auto automaticSeriesColor = series->GetAutomaticSeriesColor();
auto invertedSeriesColor = Color::get_Red();
series->get_Format()->get_Fill()->set_FillType(FillType::Solid);
series->get_Format()->get_Fill()->get_SolidFillColor()->set_Color(automaticSeriesColor);
series->set_InvertIfNegative(true);
series->get_InvertedSolidFillColor()->set_Color(invertedSeriesColor);

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

結果:

The inverted solid fill color

您也可以透過 IChartDataPoint::set_InvertIfNegative 為單一點啟用反轉。以下範例在系列上停用反轉,僅為選取的點啟用,且為該點指派負值,以便顯示效果:

#include <DOM/Chart/ChartType.h>
#include <DOM/Chart/IChartData.h>
#include <DOM/Chart/IChartDataCell.h>
#include <DOM/Chart/IChartDataPoint.h>
#include <DOM/Chart/IChartSeries.h>
#include <DOM/Chart/IChartSeriesCollection.h>
#include <DOM/Chart/IDoubleChartValue.h>
#include <DOM/Chart/IFormat.h>
#include <DOM/FillType.h>
#include <DOM/IChart.h>
#include <DOM/IColorFormat.h>
#include <DOM/IFillFormat.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/Presentation.h>
#include <Export/SaveFormat.h>
#include <drawing/color.h>
#include <system/object_ext.h>
#include <system/shared_ptr.h>

using Aspose::Slides::Charts::ChartType;
using Aspose::Slides::Export::SaveFormat;
using Aspose::Slides::FillType;
using Aspose::Slides::Presentation;
using System::Drawing::Color;
using System::ObjectExt;

const int firstSlideIndex = 0;
const int firstSeriesIndex = 0;
const int targetDataPointIndex = 2;
const int negativeValue = -30;

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

auto chart = slide->get_Shapes()->AddChart(ChartType::ClusteredColumn, 20.0f, 20.0f, 500.0f, 200.0f);

auto seriesCollection = chart->get_ChartData()->get_Series();
auto series = seriesCollection->idx_get(firstSeriesIndex);
auto automaticSeriesColor = series->GetAutomaticSeriesColor();
auto invertedSeriesColor = Color::get_Red();
series->get_Format()->get_Fill()->set_FillType(FillType::Solid);
series->get_Format()->get_Fill()->get_SolidFillColor()->set_Color(automaticSeriesColor);
series->get_InvertedSolidFillColor()->set_Color(invertedSeriesColor);
series->set_InvertIfNegative(false);

auto dataPoint = series->get_DataPoint(targetDataPointIndex);
auto boxedNegativeValue = ObjectExt::Box<int>(negativeValue);
dataPoint->get_YValue()->get_AsCell()->set_Value(boxedNegativeValue);
dataPoint->set_InvertIfNegative(true);

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

清除特定資料點的值

若要使單一點變為空白且不移除其他點,請將其支援的工作簿儲存格設定為 nullptr。對於直條圖而言,繪製的值可透過 IChartDataPoint::get_YValue 取得。資料點仍保留在相同的類別位置,但圖表會依照圖表的空白值設定將其視為空白。

以下範例僅清除第一個系列的第二個點:

#include <DOM/Chart/ChartType.h>
#include <DOM/Chart/IChartData.h>
#include <DOM/Chart/IChartDataCell.h>
#include <DOM/Chart/IChartDataPoint.h>
#include <DOM/Chart/IChartSeries.h>
#include <DOM/Chart/IChartSeriesCollection.h>
#include <DOM/Chart/IDoubleChartValue.h>
#include <DOM/IChart.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/Presentation.h>
#include <Export/SaveFormat.h>
#include <system/shared_ptr.h>

using Aspose::Slides::Charts::ChartType;
using Aspose::Slides::Export::SaveFormat;
using Aspose::Slides::Presentation;

const int firstSlideIndex = 0;
const int firstSeriesIndex = 0;
const int targetDataPointIndex = 1;

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

auto chart = slide->get_Shapes()->AddChart(ChartType::ClusteredColumn, 20.0f, 20.0f, 500.0f, 200.0f);

auto seriesCollection = chart->get_ChartData()->get_Series();
auto series = seriesCollection->idx_get(firstSeriesIndex);
auto dataPoint = series->get_DataPoint(targetDataPointIndex);
dataPoint->get_YValue()->get_AsCell()->set_Value(nullptr);

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

散點圖使用分別的 X 與 Y 儲存格,氣泡圖亦使用大小儲存格。僅清除您想移除的值所對應的儲存格。若只想保留其他點,請不要呼叫 IChartDataPointCollection::Clear,因為該方法會移除該系列的全部資料點。

控制空儲存格的顯示方式

包含值的隱藏儲存格屬於與空儲存格不同的情況。若要包含或排除隱藏工作表列與欄的資料,請參閱從隱藏列與欄中匯入資料。

空的工作簿儲存格代表遺失的資料;儲存格內含 0 則代表已知的數值。呼叫 IChartDataCell::set_Value 並傳入 nullptr 即可使儲存格變為空白。數值零即使在空白儲存格設定下仍保持為零。

使用 IChart::set_DisplayBlanksAs 可選擇圖表如何顯示空儲存格。此設定套用於整個圖表,會改變空白的繪製方式,但不會將空白工作簿儲存格填入零或插值。

以下獨立範例建立一個包含單一系列的折線圖,清除第 3 天的值,並以每種模式儲存相同圖表。此範例不需要輸入檔案。IChartDataWorkbook 使用第 0 個工作表,第 0 欄作為類別標籤,第 1 欄作為數值;第 0 列保留系列名稱。最終資料為 10, 20, empty, 30, 40。

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

using namespace Aspose::Slides;
using namespace Aspose::Slides::Charts;
using namespace Aspose::Slides::Export;
using System::ObjectExt;
using System::String;

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

auto chart = slide->get_Shapes()->AddChart(ChartType::LineWithMarkers, 40.0f, 40.0f, 640.0f, 400.0f);
auto chartData = chart->get_ChartData();
auto workbook = chartData->get_ChartDataWorkbook();

chartData->get_Series()->Clear();
chartData->get_Categories()->Clear();

auto seriesName = ObjectExt::Box<String>(u"Measurements");
auto seriesNameCell = workbook->GetCell(0, 0, 1, seriesName);
auto series = chartData->get_Series()->Add(seriesNameCell, chart->get_Type());
auto values = std::array<int, 5>{10, 20, 25, 30, 40};

for (auto i = 0; i < values.size(); i++)
{
    auto categoryName = String::Format(u"Day {0}", i + 1);
    auto boxedCategoryName = ObjectExt::Box<String>(categoryName);
    auto categoryCell = workbook->GetCell(0, i + 1, 0, boxedCategoryName);
    chartData->get_Categories()->Add(categoryCell);
    auto boxedValue = ObjectExt::Box<int>(values[i]);
    auto valueCell = workbook->GetCell(0, i + 1, 1, boxedValue);
    series->get_DataPoints()->AddDataPointForLineSeries(valueCell);
}

// 將第 3 天真正留空,同時保留其類別與資料點。
workbook->GetCell(0, 3, 1)->set_Value(nullptr);

auto modes = std::array<DisplayBlanksAsType, 3>{DisplayBlanksAsType::Gap, DisplayBlanksAsType::Zero, DisplayBlanksAsType::Span};
for (auto mode : modes)
{
    chart->set_DisplayBlanksAs(mode);
    auto outputPath = String::Format(u"empty_cells_{0}.pptx", mode);
    presentation->Save(outputPath, SaveFormat::Pptx);
}

presentation->Dispose();

每個輸出檔案皆在儲存前指定模式:empty_cells_Gap.pptx、empty_cells_Zero.pptx、empty_cells_Span.pptx。若只想儲存單一版本,請先指定所需模式,然後只儲存一次簡報,而非遍歷所有模式。

下表比較三個檔案中的相同資料。第 3 天在工作簿中皆為空白:

Line charts with identical data: Gap breaks the line at Day 3, Zero drops the line to zero, and Span connects Day 2 to Day 4.

可見效果取決於圖表類型。折線圖可輕易比較所有三種模式。長條圖與柱形圖因缺少連接線而無法呈現 Span 所示的連接段落;缺少的柱形與零高柱形外觀也可能相似。類似地,僅有標記的散點圖也沒有連接線。不要期望每種圖表類型都會產生三種明顯不同的結果;請檢查您使用的圖表類型之輸出。

設定系列間隙寬度

間隙寬度是相鄰長條或柱形叢之間的空間,表示為長條或柱形寬度的百分比。與重疊相同,它屬於父系列群組,而非單一系列。對該群組呼叫一次 IChartSeriesGroup::set_GapWidth。較大的數值會在叢之間產生更大的間距,較小的數值則使其更為密集。

以下範例變更間隙寬度,僅儲存最終簡報:

#include <cstdint>
#include <DOM/Chart/ChartType.h>
#include <DOM/Chart/IChartData.h>
#include <DOM/Chart/IChartSeries.h>
#include <DOM/Chart/IChartSeriesCollection.h>
#include <DOM/Chart/IChartSeriesGroup.h>
#include <DOM/IChart.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/Presentation.h>
#include <Export/SaveFormat.h>
#include <system/shared_ptr.h>

using Aspose::Slides::Charts::ChartType;
using Aspose::Slides::Export::SaveFormat;
using Aspose::Slides::Presentation;

const int firstSlideIndex = 0;
const int firstSeriesIndex = 0;
const uint16_t gapWidthPercent = 30;

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

auto chart = slide->get_Shapes()->AddChart(ChartType::StackedColumn, 20.0f, 20.0f, 500.0f, 200.0f);

auto seriesCollection = chart->get_ChartData()->get_Series();
auto series = seriesCollection->idx_get(firstSeriesIndex);
series->get_ParentSeriesGroup()->set_GapWidth(gapWidthPercent);

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

結果:

The gap width

顯示或隱藏系列線條

系列線條在圖表中視覺上連接相關部分。使用 IChartSeriesGroup::set_HasSeriesLines 可在支援的堆疊長條與堆疊圓餅圖中顯示或隱藏它們。在二維堆疊長條與柱形圖中,它們連接跨類別的相應堆疊邊界。在「圓餅之圓餅」與「長條之圓餅」圖表中,它們連接主圖與次圖。此設定並非一般目的的連接線,無法用於其他圖表類型。

透過 IChartSeries::get_ParentSeriesGroup 取得群組。設定會套用至該群組內的每個系列。IChartSeries::get_HasSeriesLines 只會讀取值並反映父群組的設定;請呼叫群組的 setter 以更新它。

以下範例建立一個使用預設資料的 ChartType::PieOfPie 圖表,分別儲存啟用與關閉系列線條的兩個簡報,以供比較。

#include <DOM/Chart/ChartType.h>
#include <DOM/Chart/IChartData.h>
#include <DOM/Chart/IChartSeries.h>
#include <DOM/Chart/IChartSeriesCollection.h>
#include <DOM/Chart/IChartSeriesGroup.h>
#include <DOM/IChart.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/Presentation.h>
#include <Export/SaveFormat.h>
#include <system/shared_ptr.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::PieOfPie, 50, 50, 600, 400);
auto series = chart->get_ChartData()->get_Series()->idx_get(0);
auto seriesGroup = series->get_ParentSeriesGroup();

seriesGroup->set_HasSeriesLines(true);
presentation->Save(u"series_lines_enabled.pptx", SaveFormat::Pptx);

seriesGroup->set_HasSeriesLines(false);
presentation->Save(u"series_lines_disabled.pptx", SaveFormat::Pptx);
presentation->Dispose();
系列線條已啟用 系列線條已關閉
Pie of Pie chart with series lines enabled Pie of Pie chart with series lines disabled
兩條線將主圓餅連接至次圓餅。 兩個圓餅仍然可見,但連接線已隱藏。

系列線條與資料標籤引導線、網格線或折線圖中的資料線條不同。此設定不會控制那些元素。

常見問題集

哪些圖表類型支援資料系列?

所有由 ChartType 列舉的圖表類型皆使用圖表資料,但其系列並非全部擁有相同的值結構或設定。例如,類別圖使用類別與值,散點圖使用 X 與 Y 值,氣泡圖則額外使用氣泡大小。請使用與系列類型相符的資料點建立方法。重疊與間隙寬度等選項僅適用於相容的長條或柱形群組。

什麼是圖表系列群組?

IChartSeriesGroup 包含共享群組層級繪圖設定的相容系列。組合圖表可以包含多個群組,因此透過單一系列取得的群組設定不一定會改變圖表中所有系列。

新建立的圖表會包含預設資料嗎?

會。預設情況下,IShapeCollection::AddChart 會建立示範系列、類別與值。您可以編輯這些儲存格,或在加入完全自訂的資料集之前先清除系列與類別集合。也有重載可以建立不含預設資料的圖表。

圖表物件如何與工作簿儲存格連結?

系列名稱、類別標籤與資料點值會參照 IChartDataWorkbook 中的儲存格。變更參照的儲存格會更新對應的圖表元素。建立自訂資料時,請保持類別列與系列值列對齊,使每個點皆在預期的類別下繪製。

如何只清除單一點而不是整個系列?

將相關的值儲存格設定為 nullptr,即可保留該點的類別位置作為空白點。僅在您確定要移除該系列所有點時才呼叫 IChartDataPointCollection::Clear。如果同時移除類別,請更新每個系列,使其值仍與類別集合保持對齊。

空白點會如何顯示?

結果取決於圖表類型與 IChart::get_DisplayBlanksAs。受支援的圖表可以將空白顯示為間隙、零值或連接相鄰點。選擇與簡報中遺失資料意義相符的設定。請參閱控制空儲存格的顯示方式以取得完整範例與視覺比較。

負值會如何格式化?

對於受支援的長條、柱形與氣泡系列,呼叫 IChartSeries::set_InvertIfNegative 並透過 IChartSeries::get_InvertedSolidFillColor 設定顏色。您亦可使用 IChartDataPoint::set_InvertIfNegative 為單一點覆寫此行為。這些方法影響格式化,而非儲存的數值。

當系列與點同時設定格式時,哪個會生效?

明確的資料點格式會在該點上優先於系列格式。其他點則繼續使用明確的系列格式,或在系列格式未定義時使用自動圖表樣式與主題。群組設定如重疊與間隙寬度控制版面配置,並非點層級的格式覆寫。

圖表可以包含的系列數量有上限嗎?

Aspose.Slides 本身未設置固定的系列數量上限。實務上,簡報檔案的限制、可用記憶體、渲染時間以及圖表可讀性會決定實際可用的上限。

當欄位過於接近或過於分散時,應該調整什麼?

對適當的父系列群組呼叫 IChartSeriesGroup::set_GapWidth。增大數值會擴大叢之間的間距,減少則使叢更緊密。