如何创建动态滚动图表

可能的使用场景

动态滚动图表是指显示数据点不断变化和更新的图形表示。这是一种图表类型,会不断更新自己,展示最近数据点的滚动窗口,同时丢弃旧的数据点,因为新的数据点出现。

动态滚动图表通常用于可视化实时或流数据中的趋势和模式。在临时方面和随时间的变化至关重要的场景中特别有用,如股票市场分析、天气监测或实时性能跟踪。

这些图表通常采用动画或自动滚动机制,以确保始终呈现最新的信息。滚动窗口的长度可以自定义,以显示特定的时间段,如最近一小时、一天或一周。

总之,动态滚动图表是不断更新的图形表示,显示最新数据点,丢弃旧数据点,使用户能够观察实时趋势和模式。

使用Aspose Cells创建动态滚动图表

在接下来的段落中,我们将向您展示如何使用Aspose.Cells创建动态滚动图表。我们将向您展示示例的代码以及使用该代码创建的Excel文件。

示例代码

以下示例代码将生成动态滚动图表文件

//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创建了动态滚动图表。