C++を使ったチャートのカスタマイズ
カスタムチャートの作成
これまでにチャートについて話す際、標準的な書式設定を持つ標準チャートを見てきました。データソースを定義し、一部のプロパティを設定するだけで、チャートはデフォルトの書式設定で作成されます。しかし、Aspose.Cells APIは、開発者が独自の書式設定を持つチャートを作成できるカスタムチャートの作成もサポートしています。
開発者は、Aspose.Cellsを使用して実行時にカスタムチャートを作成できます。
チャートはデータ系列で構成されます。Aspose.Cellsの各データ系列はSeriesオブジェクトで表され、 SeriesCollectionオブジェクトはSeriesオブジェクトのコレクションとして機能します。カスタムチャートを作成する際、開発者は異なる種類のチャートを異なるデータ系列に使用する自由があります(SeriesCollectionオブジェクトで収集されたデータ系列)。
以下の例コードは、カスタムチャートの作成方法を示しています。この例では、最初のデータ系列には列チャートを使用し、2番目のデータ系列には折れ線グラフを使用しています。その結果、ワークシートには列チャートと折れ線グラフが組み合わされたチャートが追加されます。
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
using namespace Aspose::Cells::Charts;
int main()
{
Aspose::Cells::Startup();
<span class="c1">// Source directory path
U16String srcDir(u"..\Data\01_SourceDirectory\");
<span class="c1">// Output directory path
U16String outDir(u"..\Data\02_OutputDirectory\");
<span class="c1">// Create a new workbook
Workbook workbook;
<span class="c1">// Add a new worksheet to the workbook
int sheetIndex = workbook.GetWorksheets().Add();
<span class="c1">// Obtain the reference of the newly added worksheet by passing its sheet index
Worksheet worksheet = workbook.GetWorksheets().Get(sheetIndex);
<span class="c1">// Add sample values to cells
worksheet.GetCells().Get(u"A1").PutValue(50);
worksheet.GetCells().Get(u"A2").PutValue(100);
worksheet.GetCells().Get(u"A3").PutValue(150);
worksheet.GetCells().Get(u"A4").PutValue(110);
worksheet.GetCells().Get(u"B1").PutValue(260);
worksheet.GetCells().Get(u"B2").PutValue(12);
worksheet.GetCells().Get(u"B3").PutValue(50);
worksheet.GetCells().Get(u"B4").PutValue(100);
<span class="c1">// Add a chart to the worksheet
int chartIndex = worksheet.GetCharts().Add(ChartType::Column, 5, 0, 15, 5);
<span class="c1">// Access the instance of the newly added chart
Chart chart = worksheet.GetCharts().Get(chartIndex);
<span class="c1">// Add NSeries (chart data source) to the chart ranging from "A1" cell to "B4"
chart.GetNSeries().Add(u"A1:B4", true);
<span class="c1">// Set the chart type of 2nd NSeries to display as line chart
chart.GetNSeries().Get(1).SetType(ChartType::Line);
<span class="c1">// Save the Excel file
workbook.Save(outDir + u"output.xls");
<span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o"><<</span> <span class="s">"Chart created successfully!"</span> <span class="o"><<</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>
<span class="n">Aspose</span><span class="o">::</span><span class="n">Cells</span><span class="o">::</span><span class="n">Cleanup</span><span class="p">();</span>
}
現在、Aspose.Cellsはパイチャート、ラインチャート、カラムチャート、およびカラム積み上げチャートを組み合わせたカスタムチャートのみをサポートしていますが、今後のリリースでさらに多くのチャートがサポートされる予定です。