動的ローリングチャートの作成方法

可能な使用シナリオ

動的ローリングチャートとは、データポイントが常にシフトして更新されるグラフィカルな表現を指します。このタイプのチャートは継続的に自動更新され、新しいデータポイントが追加されると古いデータポイントは破棄されます。

動的ローリングチャートは、リアルタイムやストリーミングデータのトレンドやパターンを可視化するために一般的に使用されます。株式市場の分析、気象モニタリング、またはライブパフォーマンスの追跡など、時間的な側面や時間の経過に伴う変化が重要なシナリオで特に有用です。

これらのチャートは通常、アニメーションや自動スクロールのメカニズムを使用して、常に最新の情報が表示されるようにしています。ローリングウィンドウの長さは、過去の1時間、1日、または1週間など、特定の時間期間を表示するようにカスタマイズできます。

要約すると、動的なローリングチャートは、古いデータを破棄しながら最新のデータポイントを表示する連続的に更新されるグラフ表現であり、ユーザーがリアルタイムのトレンドやパターンを観察することができます。

Aspose Cellsを使用して動的なローリングチャートを作成する

次の段落では、Aspose.Cellsを使用して動的なローリングチャートを作成する方法を示します。例のコードと、このコードで作成されたExcelファイルも表示します。

サンプルコード

次のサンプルコードは[DynamicRollingChart.xlsx]を生成します。

//How to Create a Dynamic Rolling Chart
//Your local test path
string LocalPath = @"";
//Create a new workbook and access the first worksheet.
Workbook workbook = new Workbook();
WorksheetCollection sheets = workbook.Worksheets;
Worksheet sheet = sheets[0];
//Populate the data for the chart. Add values to cells and set series names.
sheet.Cells["A1"].PutValue("Month");
sheet.Cells["A2"].PutValue(1);
sheet.Cells["A3"].PutValue(2);
sheet.Cells["A4"].PutValue(3);
sheet.Cells["A5"].PutValue(4);
sheet.Cells["A6"].PutValue(5);
sheet.Cells["A7"].PutValue(6);
sheet.Cells["A8"].PutValue(7);
sheet.Cells["B1"].PutValue("Sales");
sheet.Cells["B2"].PutValue(50);
sheet.Cells["B3"].PutValue(45);
sheet.Cells["B4"].PutValue(55);
sheet.Cells["B5"].PutValue(60);
sheet.Cells["B6"].PutValue(55);
sheet.Cells["B7"].PutValue(65);
sheet.Cells["B8"].PutValue(70);
//Set the dynamic range for the chart's data source.
int index = sheets.Names.Add("Sheet1!ChtData");
sheets.Names[index].RefersTo = "=OFFSET(Sheet1!$B$1,COUNT(Sheet1!$B:$B),0,-5,1)";
//Set the dynamic range for the chart's data labels.
index = sheets.Names.Add("Sheet1!ChtLabels");
sheets.Names[index].RefersTo = "=OFFSET(Sheet1!$A$1,COUNT(Sheet1!$A:$A),0,-5,1)";
//Create a chart object and set its data source.
int chartIndex = sheet.Charts.Add(ChartType.Line, 10, 3, 25, 10);
Chart chart = sheet.Charts[chartIndex];
chart.NSeries.Add("Sales", true);
chart.NSeries[0].Values = "Sheet1!ChtData";
chart.NSeries[0].XValues = "Sheet1!ChtLabels";
//Save the workbook as an Excel file.
workbook.Save(LocalPath + "DynamicRollingChart.xlsx");

メモ

生成されたファイルでは、列Aと列Bにデータを追加できます。これに対し、チャートは最新の5つのデータセットを動的にカウントします。これは、例のコードの「OFFSET」式を使用して行います。

"=OFFSET(Sheet1!$A$1,COUNT(Sheet1!$A:$A),0,-5,1)"

数式の中の数値「-5」を「-10」に変更してみると、動的チャートは最新の10つのデータセットをカウントします。これでAspose.Cellsを使用して動的なローリングチャートを作成しました。