如何创建动态滚动图表
可能的使用场景
动态滚动图表是一种用于显示随时间变化的数据的图形表示类型。它旨在实时显示数据,使用户能够追踪连续的更新和趋势。随着新增数据的加入,图表将持续更新并自动滚动,以显示最新的信息。
动态滚动图表通常在各个行业中被广泛使用,如金融、股市分析、天气跟踪和社交媒体分析。它们使用户能够可视化和分析数据模式,并基于实时信息做出明智的决策。
这些图表通常是交互式的,允许用户放大或缩小、滚动历史数据和调整时间间隔。它们通常支持多个数据系列,提供不同指标及其相关性的综合视图。
总的来说,动态滚动图表是用于监控和分析时间序列数据的有价值的工具,有助于实时决策和增强数据可视化能力。
使用Aspose Cells创建动态滚动图表
在接下来的段落中,我们将向您展示如何使用Aspose.Cells创建动态滚动图表。我们会展示示例的代码以及用此代码创建的Excel文件。
示例代码
以下示例代码将生成动态滚动图表文件。
// How to Create a Dynamic Scrolling 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("Day"); | |
sheet.Cells["B1"].PutValue("Sales"); | |
//In this example, we will add 30 days of data | |
int allDays = 30; | |
int showDays = 10; | |
int currentDay = 1; | |
for (int i = 0; i < allDays; i++) | |
{ | |
string _cellA = String.Format("A{0}", i + 2); | |
string _cellB = String.Format("B{0}", i + 2); | |
sheet.Cells[_cellA].PutValue(i + 1); | |
sheet.Cells[_cellB].PutValue(50 * (i % 2) + 20 * (i % 3) + 10 * (i/3)); | |
} | |
//This is the Dynamic Scrolling Control Data | |
sheet.Cells["G19"].PutValue("Start Day"); | |
sheet.Cells["G20"].PutValue(currentDay); | |
sheet.Cells["H19"].PutValue("Show Days"); | |
sheet.Cells["H20"].PutValue(showDays); | |
//Set the dynamic range for the chart's data source. | |
int index = sheets.Names.Add("Sheet1!ChtScrollData"); | |
sheets.Names[index].RefersTo = "=OFFSET(Sheet1!$B$2,Sheet1!$G$20,0,Sheet1!$H$20,1)"; | |
//Set the dynamic range for the chart's data labels. | |
index = sheets.Names.Add("Sheet1!ChtScrollLabels"); | |
sheets.Names[index].RefersTo = "=OFFSET(Sheet1!$A$2,Sheet1!$G$20,0,Sheet1!$H$20,1)"; | |
//Add a ScrollBar for the Dynamic Scrolling Chart | |
ScrollBar bar = sheet.Shapes.AddScrollBar(2, 0, 3, 0, 200, 30); | |
bar.Min = 0; | |
bar.Max = allDays - showDays; | |
bar.CurrentValue = currentDay; | |
bar.LinkedCell = "$G$20"; | |
//Create a chart object and set its data source. | |
int chartIndex = sheet.Charts.Add(ChartType.Line, 2, 4, 15, 10); | |
Chart chart = sheet.Charts[chartIndex]; | |
chart.NSeries.Add("Sales", true); | |
chart.NSeries[0].Values = "Sheet1!ChtScrollData"; | |
chart.NSeries[0].XValues = "Sheet1!ChtScrollLabels"; | |
//Save the workbook as an Excel file. | |
workbook.Save(LocalPath + "DynamicScrollingChart.xlsx"); |
备注
在生成的文件中,您可以操作滚动条,而图表会动态计算最新的10组数据。这是在示例代码中使用“OFFSET”公式完成的:
"=OFFSET(Sheet1!$B$2,Sheet1!$G$20,0,Sheet1!$H$20,1)"
您可以尝试将单元格“Sheet1!$H$20”中的数字“10”更改为“15”,动态图表将计算最新的15组数据。现在我们成功地使用Aspose.Cells创建了动态滚动图表。