チャートの作成とカスタマイズ

可能な使用シナリオ

チャートは情報を視覚的に表示するものです。Aspose.Cellsを使用すると、Microsoft Excelと同様にチャートを使用して情報を視覚化することができます。情報をチャートで表現することは、意思決定者が迅速かつタイムリーな決定を下すのに役立ちます。チャートを使用すると、生の数字よりも比較やパターン、データの傾向を素早く把握することができます。スプレッドシートのデータに基づいてランタイムでチャートを作成することは、Aspose.Cellsの有用な機能の1つです。Aspose.Cellsは、標準チャートとカスタマイズされたチャートの両方を作成することをサポートしています。以下では、Aspose.Cells APIを使用して一般的なMS-Excelのチャートの作成方法についていくつかの例を示します。

ピラミッドチャート

例のコードを実行すると、ワークシートにピラミッドチャートが追加されます。次のサンプルコードの出力Excelファイル(66519068.xlsx)を参照してください。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C
Aspose::Cells::Startup();
// Output directory path
U16String outDir(u"..\\Data\\02_OutputDirectory\\");
// Path of output excel file
U16String outputChartTypePyramid = outDir + u"outputChartTypePyramid.xlsx";
// Create a new workbook
Workbook workbook;
// Get first worksheet which is created by default
Worksheet worksheet = workbook.GetWorksheets().Get(0);
// Adding 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"B1").PutValue(4);
worksheet.GetCells().Get(u"B2").PutValue(20);
worksheet.GetCells().Get(u"B3").PutValue(50);
// Adding a chart to the worksheet
int chartIndex = worksheet.GetCharts().Add(Aspose::Cells::Charts::ChartType::Pyramid, 5, 0, 20, 8);
// Accessing the instance of the newly added chart
Chart chart = worksheet.GetCharts().Get(chartIndex);
// Adding SeriesCollection (chart data source) to the chart ranging from "A1" cell to "B3"
chart.GetNSeries().Add(u"A1:B3", true);
// Saving the Excel file
workbook.Save(outputChartTypePyramid);
Aspose::Cells::Cleanup();

折れ線グラフ

上記の例では、ChartTypeを**ChartType::Line**に変更すると折れ線グラフが作成されます。完全なソースは以下に示します。コードを実行すると、折れ線グラフがワークシートに追加されます。次のサンプルコードの出力Excelファイル(66519069.xlsx)を参照してください。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C
Aspose::Cells::Startup();
// Output directory path
U16String outDir(u"..\\Data\\02_OutputDirectory\\");
// Path of output excel file
U16String outputChartTypeLine = testPath + u"outputChartTypeLine.xlsx";
// Create a new workbook
Workbook workbook;
// Get first worksheet which is created by default
Worksheet worksheet = workbook.GetWorksheets().Get(0);
// Adding 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"B1").PutValue(4);
worksheet.GetCells().Get(u"B2").PutValue(20);
worksheet.GetCells().Get(u"B3").PutValue(50);
// Adding a chart to the worksheet
int chartIndex = worksheet.GetCharts().Add(Aspose::Cells::Charts::ChartType::Line, 5, 0, 20, 8);
// Accessing the instance of the newly added chart
Chart chart = worksheet.GetCharts().Get(chartIndex);
// Adding SeriesCollection (chart data source) to the chart ranging from "A1" cell to "B3"
chart.GetNSeries().Add(u"A1:B3", true);
// Saving the Excel file
workbook.Save(outputChartTypeLine);
Aspose::Cells::Cleanup();

バブルチャート

バブルチャートを作成するには、ChartTypeを**ChartType_Bubble**に設定し、SetBubbleSizesSetXValuesなどの追加のプロパティを適切に設定する必要があります。以下のコードを実行すると、バブルチャートがワークシートに追加されます。次のサンプルコードの出力Excelファイル(66519070.xlsx)を参照してください。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C
Aspose::Cells::Startup();
// Output directory path
U16String outDir(u"..\\Data\\02_OutputDirectory\\");
// Path of output excel file
U16String outputChartTypeBubble = outDir + u"outputChartTypeBubble.xlsx";
// Create a new workbook
Workbook workbook;
// Get first worksheet which is created by default
Worksheet worksheet = workbook.GetWorksheets().Get(0);
// Fill in data for chart's series
// Y Values
worksheet.GetCells().Get(0, 0).PutValue(u"Y Values");
worksheet.GetCells().Get(0, 1).PutValue(2);
worksheet.GetCells().Get(0, 2).PutValue(4);
worksheet.GetCells().Get(0, 3).PutValue(6);
// Bubble Size
worksheet.GetCells().Get(1, 0).PutValue(u"Bubble Size");
worksheet.GetCells().Get(1, 1).PutValue(2);
worksheet.GetCells().Get(1, 2).PutValue(3);
worksheet.GetCells().Get(1, 3).PutValue(1);
// X Values
worksheet.GetCells().Get(2, 0).PutValue(u"X Values");
worksheet.GetCells().Get(2, 1).PutValue(1);
worksheet.GetCells().Get(2, 2).PutValue(2);
worksheet.GetCells().Get(2, 3).PutValue(3);
// Set first column width
worksheet.GetCells().SetColumnWidth(0, 12);
// Adding a chart to the worksheet
int chartIndex = worksheet.GetCharts().Add(Aspose::Cells::Charts::ChartType::Bubble, 5, 0, 20, 8);
// Accessing the instance of the newly added chart
Chart chart = worksheet.GetCharts().Get(chartIndex);
// Adding SeriesCollection (chart data source) to the chart ranging from B1 to D1
chart.GetNSeries().Add(u"B1:D1", true);
// Set bubble sizes
chart.GetNSeries().Get(0).SetBubbleSizes(u"B2:D2");
// Set X axis values
chart.GetNSeries().Get(0).SetXValues(u"B3:D3");
// Set Y axis values
chart.GetNSeries().Get(0).SetValues(u"B1:D1");
// Saving the Excel file
workbook.Save(outputChartTypeBubble);
Aspose::Cells::Cleanup();

カスタムチャートの作成

これまでに標準チャートについて見てきましたが、標準のフォーマット設定があるチャートです。データソースを定義し、いくつかのプロパティを設定するだけで、チャートはデフォルトのフォーマット設定で作成されます。しかし、Aspose.Cells APIは、開発者が独自のフォーマット設定でチャートを作成できるカスタムチャートもサポートしています。Aspose.Cellsを使用して、開発者は独自のフォーマット設定でランタイムでカスタムチャートを作成することができます。

チャートはデータシリーズから構成されています。カスタムチャートを作成するとき、開発者は異なる種類のチャートを異なるデータシリーズに使用する自由があります。

以下の例のコードは、カスタムチャートの作成方法を示しています。この例では、最初のデータシリーズには列チャート、2番目のシリーズには折れ線グラフを使用します。その結果、ワークシートには列チャートと折れ線グラフが組み合わされたチャートが追加されます。以下のサンプルコードの出力Excelファイル(66519071.xlsx)を参照してください。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C
Aspose::Cells::Startup();
// Output directory path
U16String outDir(u"..\\Data\\02_OutputDirectory\\");
// Path of output excel file
U16String outputChartTypeCustom = outDir + u"outputChartTypeCustom.xlsx";
// Create a new workbook
Workbook workbook;
// Get first worksheet which is created by default
Worksheet worksheet = workbook.GetWorksheets().Get(0);
// Adding 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);
// Adding a chart to the worksheet
int chartIndex = worksheet.GetCharts().Add(Aspose::Cells::Charts::ChartType::Column, 5, 0, 20, 8);
// Accessing the instance of the newly added chart
Chart chart = worksheet.GetCharts().Get(chartIndex);
// Adding SeriesCollection (chart data source) to the chart ranging from A1 to B4
chart.GetNSeries().Add(u"A1:B4", true);
// Setting the chart type of 2nd NSeries to display as line chart
chart.GetNSeries().Get(1).SetType(Aspose::Cells::Charts::ChartType::Line);
// Saving the Excel file
workbook.Save(outputChartTypeCustom);
Aspose::Cells::Cleanup();