C++でプレゼンテーションのチャート データ系列を管理する

概要

チャートは、プロットされた値をワークブックのセルに保存するか、数値リテラルとして直接保存できます。 IChartSeries は関連する値のセットを表し、各 IChartDataPoint はそのシリーズ内のポイントを表します。 IChartCategory オブジェクトは、シリーズが共有するラベルまたはグループ化値を提供します。デフォルトデータで作成されたチャートでは、シリーズ名、カテゴリ、ポイント値はチャートワークブックの IChartDataCell オブジェクトを使用します。

典型的なカテゴリチャートでは、デフォルトのワークブックは行0を系列名、列0をカテゴリ名に使用し、残りのセルに系列値を配置します。 IChartDataWorkbook::GetCell に渡されるワークシート、行、列のインデックスはゼロベースです。このレイアウトはデフォルトデータでチャートを作成する際に便利ですが、既存のすべてのチャートがこれを使用しているとは限りません。ロードされたプレゼンテーションでは、ワークブックの値を変更する前に、系列、カテゴリ、データポイントが参照しているセルを確認してください。

チャート設定には 3 つの異なるスコープがあります:

  • 系列レベルの設定は、IChartSeries::get_Format のように、1 つの系列内のすべてのポイントのデフォルトの外観を提供します。
  • データポイントの設定は、IChartDataPoint::get_Format のように、1 ポイントの系列外観を上書きします。
  • グループ設定は、同じ IChartSeriesGroup に属する互換性のある系列に適用されます。重なりやギャップ幅などのオプションを設定する必要がある場合は、IChartSeries::get_ParentSeriesGroup を介してグループにアクセスしてください。

明示的なポイントまたは系列の塗りつぶしが設定されていない場合、チャートのスタイルとテーマが自動的な外観を決定します。系列とポイントの書式設定の両方が存在する場合、ポイントの書式設定がそのポイントに対して優先されます。

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 とは別物で、チャートの内部または外部のワークブック構成を特定します。数値リテラルを使用しても、埋め込まれたワークブックが自動的に削除されるわけではありません。この例では、カテゴリラベル、系列名、その他の系列のためにワークブックは引き続き利用可能です。ワークブックの管理については Chart Workbook を参照してください。

散布図およびバブルチャートの場合は、set_DataSourceTypeForXValues と set_DataSourceTypeForYValues を呼び出して、get_XValue と get_YValue のソースを選択します。バブルチャートでは、set_DataSourceTypeForBubbleSizes を使用して get_BubbleSize のソースを指定します。該当する各ソースを DoubleLiterals に設定し、set_AsLiteralDouble メソッドで数値を割り当ててください。

チャート系列の重なりを設定する

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 を使用して名前範囲のすべてのセルを含めています。

以下の例は、1 系列と 2 データポイントを持つプレゼンテーションを作成します。セル 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);

// これらの2つのセルは系列名を提供します。
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 で、2 つのセル値の間にスペースが挿入されます。凡例は両方の列を 1 つのエントリとして表示します。以下の画像で結果を示します。

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 で割り当てます。負の数値はワークブック内で変更されず、表示色だけが変わります。

以下の例は、デフォルトのチャートデータを 1 系列に置き換えます。ワークシートの行 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

1 ポイントだけ反転を有効にするには、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 で取得できます。データポイントは同じカテゴリ位置に残りますが、チャートはブランク値設定に従ってその値を空として扱います。

以下の例は、最初の系列の 2 番目のポイントだけをクリアします。

#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 を呼び出さないでください。Clear はその系列のすべてのデータポイントを削除します。

空セルの表示を制御する

値を含む非表示セルは空セルとは別のケースです。非表示のワークシート行や列のデータを含めるか除外するには、Include Data from Hidden Rows and Columns を参照してください。

空のワークブックセルは欠損データを表し、0 を含むセルは既知の数値を表します。セルを空にするには、nullptr を渡して IChartDataCell::set_Value を呼び出します。数値ゼロはブランクセル設定に関係なくゼロのままです。

IChart::set_DisplayBlanksAs を使用して、チャートが空セルをどのように表示するかを選択します。この設定はチャート全体に適用され、ブランクをプロットする方法を変更しますが、空のワークブックセルにゼロや補間値を埋め込むことはありません。

以下の自包含例は、1 系列の折れ線グラフを作成し、Day 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);
}

// Leave Day 3 genuinely empty, while retaining its category and data point.
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。1 つのバージョンだけを保存したい場合は、目的のモードを割り当ててプレゼンテーションを一度だけ保存してください。

比較画像は 3 つのファイルすべてで同じデータを示しています。Day 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.

可視効果はチャートタイプに依存します。折れ線グラフは 3 つのモードを比較しやすいですが、棒や列のチャートは欠損カテゴリをつなぐラインがなく、Span は上記のような接続セグメントを生成できません。欠損列とゼロ高さの列は見た目が似ることがあります。同様に、マーカーだけの散布図は接続ラインがありません。すべてのチャートタイプで 3 つの明確な結果が得られるとは限らないので、使用するタイプで出力を確認してください。

系列のギャップ幅を設定する

ギャップ幅は隣接する棒または列クラスター間のスペースで、棒または列幅のパーセンテージで表されます。重なりと同様に、これは個々の系列ではなく親系列グループに属します。グループに対して一度だけ 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

系列ラインの表示/非表示を切り替える

系列ラインはチャートの関連部分を視覚的に接続します。サポートされている積み上げ棒および OfPie チャートで、IChartSeriesGroup::set_HasSeriesLines を使用して表示または非表示にできます。2 次元の積み上げ棒および列チャートでは、カテゴリ間で対応するスタック境界を接続します。Pie of Pie と Bar of Pie チャートでは、メインプロットとセカンダリプロットを接続します。この設定は他のチャートタイプ向けの汎用コネクタではありません。

IChartSeries::get_ParentSeriesGroup を介してグループにアクセスします。この設定はグループ内のすべての系列に適用されます。IChartSeries::get_HasSeriesLines は値を読み取り、親グループの設定を反映するだけです。更新するにはグループのセッターを呼び出してください。

以下の例は、デフォルトデータで作成した ChartType::PieOfPie チャートを 1 つ作成し、系列ラインを有効にしたバージョンと無効にしたバージョンの 2 つのプレゼンテーションを保存します。

#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
2 本のラインがメインパイをセカンダリパイに接続します。 2 本のパイは表示されたままですが、接続ラインは非表示です。

系列ラインは、ラベルをデータポイントに接続するデータラベルリーダーライン、軸に沿って間隔を示すグリッドライン、データ値を表す折れ線チャートのラインとは異なります。この設定はそれらの要素を制御しません。

FAQ

どのチャートタイプがデータ系列をサポートしていますか?

ChartType 列挙体で表されるすべてのチャートタイプはチャートデータを使用しますが、系列の値構造や設定はタイプごとに異なります。たとえば、カテゴリチャートはカテゴリと値を使用し、散布図は X と Y の値を使用し、バブルチャートはバブルのサイズを追加します。系列タイプに合ったデータポイント作成メソッドを使用してください。重なりやギャップ幅などのオプションは、互換性のある棒または列のグループにのみ適用されます。

チャート系列グループとは何ですか?

IChartSeriesGroup は、グループレベルのプロット設定を共有する互換性のある系列を含みます。組み合わせチャートは複数のグループを含むことができるため、ある系列を介して変更したグループが必ずしもチャート内のすべての系列に影響するわけではありません。

新しく作成したチャートはデフォルトデータを含みますか?

はい。既定では、IShapeCollection::AddChart はサンプルの系列、カテゴリ、値を作成します。これらのセルを編集するか、完全にカスタムデータセットを追加する前に系列とカテゴリのコレクションをクリアできます。オーバーロードを使用してデフォルトデータなしでチャートを作成することも可能です。

チャートオブジェクトはワークブックセルとどのように接続されていますか?

系列名、カテゴリラベル、データポイントの値は IChartDataWorkbook のセルを参照しています。参照セルを変更すると対応するチャート要素が更新されます。カスタムデータを構築する際は、カテゴリ行と系列値行が整合するように配置し、各ポイントが意図したカテゴリの下にプロットされるようにしてください。

系列全体ではなく 1 ポイントだけをクリアするには?

そのポイントの値セルを nullptr に設定して、カテゴリ位置は保持したまま空のポイントにします。ポイントコレクション全体を削除したい場合以外は、IChartDataPointCollection::Clear を呼び出さないでください。このメソッドはその系列のすべてのデータポイントを削除します。

空のポイントはどのように表示されますか?

結果はチャートタイプと IChart::get_DisplayBlanksAs に依存します。サポートされているチャートは、空白をギャップ、ゼロ値、または隣接ポイントを接続する形で表示できます。欠損データの意味に合った設定を選択してください。完全な例とビジュアル比較については Control the Display of Empty Cells を参照してください。

負の値はどのように書式設定されますか?

サポートされている棒、列、バブル系列では、IChartSeries::set_InvertIfNegative を呼び出し、IChartSeries::get_InvertedSolidFillColor で色を設定します。個々のポイントに対しては IChartDataPoint::set_InvertIfNegative で動作を上書きできます。これらのメソッドは書式設定に影響し、保存された数値自体は変更しません。

系列とポイントの両方が書式設定されている場合、どちらが優先されますか?

明示的なデータポイントの書式設定がそのポイントに対して優先されます。他のポイントは明示的な系列書式設定、または系列書式が未定義の場合は自動的なチャートスタイルとテーマを使用します。重なりやギャップ幅といったグループ設定はレイアウトを制御し、ポイントレベルの書式設定の上書きにはなりません。

系列の数に上限はありますか?

Aspose.Slides には固定された系列数の上限はありません。実際の制限はプレゼンテーションファイルのサイズ、利用可能なメモリ、レンダリング時間、およびチャートの可読性によって決まります。

列が互いに近すぎる、または遠すぎる場合は何を変更すべきですか?

適切な親系列グループに対して IChartSeriesGroup::set_GapWidth を呼び出してください。値を大きくするとクラスター間のスペースが広がり、値を小さくするとクラスターが密になります。