ドロップダウンリストを使用した動的チャートの作成方法
可能な使用シナリオ
Excelでのドロップダウンリストを使用した動的チャートは、選択したデータに基づいてダイナミックに更新されるインタラクティブなチャートをユーザーが作成できる強力なツールです。この機能は、複数のデータセットを分析したり、さまざまなシナリオを比較したりする必要がある状況で特に有用です。
ドロップダウンリストを使用した動的チャートの一般的な応用例は、財務分析です。たとえば、企業には異なる年や部門の複数の財務データがあるかもしれません。ドロップダウンリストを使用することで、ユーザーは分析したい特定のデータセットを選択し、チャートは自動的に対応する情報を表示します。これにより、簡単に比較し、トレンドやパターンを特定できます。
もう1つの応用例は、営業とマーケティングです。企業には異なる製品や地域の販売データがあるかもしれません。ドロップダウンリストを使用した動的チャートでは、ユーザーはドロップダウンリストから特定の製品や地域を選択し、チャートは選択したオプションの販売実績を動的に表示します。これにより、トップのパフォーマンスエリアや製品を特定し、データに基づいた意思決定が行えます。
要するに、Excelでのドロップダウンリストを使用した動的チャートは、データを柔軟にインタラクティブに可視化および分析する手段を提供します。複数のデータセットを比較したり、さまざまなシナリオを探ったりする必要がある状況で価値があり、財務分析、営業とマーケティングなど様々な応用に使える多目的なツールです。
Aspose Cellsを使用して動的チャートをドロップダウンリストで作成する
次の段落では、Aspose.Cellsを使用して動的チャートをドロップダウンリストで作成する方法を示します。この例のコードと、このコードで作成されたExcelファイルを紹介します。
サンプルコード
次のサンプルコードでは、ドロップダウンリストファイルを生成します。
//How to Create a Dynamic Chart with Dropdownlist | |
//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["A3"].PutValue("Tea"); | |
sheet.Cells["A4"].PutValue("Coffee"); | |
sheet.Cells["A5"].PutValue("Sugar"); | |
//In this example, we will add 12 months of data | |
sheet.Cells["B2"].PutValue("Jan"); | |
sheet.Cells["C2"].PutValue("Feb"); | |
sheet.Cells["D2"].PutValue("Mar"); | |
sheet.Cells["E2"].PutValue("Apr"); | |
sheet.Cells["F2"].PutValue("May"); | |
sheet.Cells["G2"].PutValue("Jun"); | |
sheet.Cells["H2"].PutValue("Jul"); | |
sheet.Cells["I2"].PutValue("Aug"); | |
sheet.Cells["J2"].PutValue("Sep"); | |
sheet.Cells["K2"].PutValue("Oct"); | |
sheet.Cells["L2"].PutValue("Nov"); | |
sheet.Cells["M2"].PutValue("Dec"); | |
int allMonths = 12; | |
int iCount = 3; | |
for (int i = 0; i < iCount; i++) | |
{ | |
for (int j = 0; j < allMonths; j++) | |
{ | |
int _row = i + 2; | |
int _column = j + 1; | |
sheet.Cells[_row, _column].PutValue(50 * (i % 2) + 20 * (j % 3) + 10 * (i / 3) + 10); | |
} | |
} | |
//This is the Dropdownlist for Dynamic Data | |
CellArea ca = new CellArea(); | |
ca.StartRow = 9; | |
ca.EndRow = 9; | |
ca.StartColumn = 0; | |
ca.EndColumn = 0; | |
int _index = sheet.Validations.Add(ca); | |
Validation _va = sheet.Validations[_index]; | |
_va.Type = Aspose.Cells.ValidationType.List; | |
_va.InCellDropDown = true; | |
_va.Formula1 = "=$B$2:$M$2"; | |
sheet.Cells["A9"].PutValue("Current Month"); | |
sheet.Cells["A10"].PutValue("Jan"); | |
Style _style = sheet.Cells["A10"].GetStyle(); | |
_style.Font.IsBold = true; | |
_style.Pattern = BackgroundType.Solid; | |
_style.ForegroundColor = Color.Yellow; | |
sheet.Cells["A10"].SetStyle(_style); | |
//Set the dynamic range for the chart's data source. | |
int index = sheets.Names.Add("Sheet1!ChtMonthData"); | |
sheets.Names[index].RefersTo = "=OFFSET(Sheet1!$A$3,0,MATCH($A$10, $B$2:$M$2, 0),3,1)"; | |
//Set the dynamic range for the chart's data labels. | |
index = sheets.Names.Add("Sheet1!ChtXLabels"); | |
sheets.Names[index].RefersTo = "=Sheet1!$A$3:$A$5"; | |
//Create a chart object and set its data source. | |
int chartIndex = sheet.Charts.Add(ChartType.Column, 8, 2, 20, 8); | |
Chart chart = sheet.Charts[chartIndex]; | |
chart.NSeries.Add("month", true); | |
chart.NSeries[0].Name = "=Sheet1!$A$10"; | |
chart.NSeries[0].Values = "Sheet1!ChtMonthData"; | |
chart.NSeries[0].XValues = "Sheet1!ChtXLabels"; | |
//Save the workbook as an Excel file. | |
workbook.Save(LocalPath + "DynamicChartWithDropdownlist.xlsx"); |
メモ
生成されたファイルでは、チャートは選択した月のデータを動的にカウントします。これはサンプルコードでの「OFFSET」式の使用によって行われます。
"=OFFSET(Sheet1!$A$3,0,MATCH($A$10, $B$2:$M$2, 0),3,1)"
セル「Sheet1!$A$10」のドロップダウンリストの値を変更してみると、チャートが動的に変化することがわかります。これで、Aspose.Cellsを使用して動的なチャートをドロップダウンリストで作成しました。