创建和管理图表
创建图表
简单地创建一个图表
使用以下示例代码创建带有 Aspose.Cells 的图表很简单:
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(); | |
// Obtaining the reference of the first worksheet | |
Worksheet worksheet = workbook.Worksheets[0]; | |
// Adding sample values to cells | |
worksheet.Cells["A2"].PutValue("Category1"); | |
worksheet.Cells["A3"].PutValue("Category2"); | |
worksheet.Cells["A4"].PutValue("Category3"); | |
worksheet.Cells["B1"].PutValue("Column1"); | |
worksheet.Cells["B2"].PutValue(4); | |
worksheet.Cells["B3"].PutValue(20); | |
worksheet.Cells["B4"].PutValue(50); | |
worksheet.Cells["C1"].PutValue("Column2"); | |
worksheet.Cells["C2"].PutValue(50); | |
worksheet.Cells["C3"].PutValue(100); | |
worksheet.Cells["C4"].PutValue(150); | |
// Adding a chart to the worksheet | |
int chartIndex = worksheet.Charts.Add(Aspose.Cells.Charts.ChartType.Column, 5, 0, 15, 5); | |
// Accessing the instance of the newly added chart | |
Aspose.Cells.Charts.Chart chart = worksheet.Charts[chartIndex]; | |
// Setting chart data source as the range "A1:C4" | |
chart.SetChartDataRange("A1:C4", true); | |
// Saving the Excel file | |
workbook.Save(dataDir + "output.xls"); | |
创建图表的注意事项
在创建图表之前,了解一些在使用 Aspose.Cells 创建图表时有用的基本概念非常重要。
图表对象
Aspose.Cells 提供了一组特殊的类Aspose.Cells.Charts用于创建 Aspose.Cells 支持的图表的命名空间。这些类用于创建图表对象,它们充当图表构建块。图表对象如下所列:
- 系列,图表中的单个数据系列。
- 轴,图表的轴。
- 图表,单个 Excel 图表。
- ChartArea,工作表中的图表区域。
- ChartDataTable,图表数据表。
- ChartFrame,图表中的框架对象。
- ChartPoint,图表系列中的单个点。
- ChartPointCollection,一个包含一个系列中所有点的集合。
- 图表,图表对象的集合。
- DataLabels,指定系列的所有 DataLabel 对象的集合。
- FillFormat,形状的填充格式。
- 地板,3D 图表的地板。
- 图例,图表图例。
- 线,图表线。
- SeriesCollection,Series 对象的集合。
- TickLabels,与图表轴上的刻度线关联的刻度线标签。
- 标题,图表或轴的标题。
- 趋势线,图表中的趋势线。
- TrendlineCollection,指定数据系列的所有趋势线对象的集合。
- 墙壁,3D 图表的墙壁。
使用图表对象
如上所述,所有图表对象都是其各自类的实例,并提供特定的属性和方法来执行特定的任务。使用图表对象创建图表。
使用图表收藏。中的每一项图表集合代表一个图表目的。一种图表对象封装了自定义图表外观所需的所有其他图表对象。下一节将展示如何使用一些基本的图表对象来创建一个简单的图表。
使用 Aspose.Cells 创建图表
脚步:
- 将一些数据添加到工作表单元格中Cell对象的认沽价值方法。 这将用作图表的数据源。
- 通过调用将图表添加到工作表图表收藏的添加方法,封装在工作表目的。
- 指定图表类型图表类型枚举。 例如,下面的示例使用ChartType.金字塔值作为图表类型。
- 访问新的图表对象来自图表通过传递其索引进行收集。
- 使用封装在图表对象来管理图表。 下面的示例使用系列合集图表对象指定图表的数据源。
向图表添加源数据时,数据源可以是单元格区域(如“A1:C3”),也可以是不连续的单元格序列(如“A1,A3,A5”),也可以是值(例如“1,2,3”)。
这些一般步骤允许您创建任何类型的图表。使用不同的图表对象来创建不同的图表。
使用 Aspose.Cells 可以创建许多不同类型的图表。Aspose.Cells 支持的所有标准图表都预定义在一个名为Aspose.Cells.Charts.ChartType.
预定义的图表类型是:
图表类型 | 描述 |
---|---|
柱子 | 表示簇状柱形图 |
列堆叠 | 表示堆积柱形图 |
列 100% 堆叠 | 表示 100% 堆积柱形图 |
Column3D簇状 | 表示 3D 簇状柱形图 |
列 3D 堆叠 | 表示 3D 堆积柱形图 |
Column3D100PercentStacked | 表示 3D 100% 堆积柱形图 |
立柱三维 | 表示 3D 柱形图 |
酒吧 | 表示簇状条形图 |
酒吧堆叠 | 表示堆积条形图 |
Bar100Percent 堆叠 | 表示 100% 堆积条形图 |
Bar3DClustered | 表示 3D 簇状条形图 |
Bar3D堆叠 | 表示 3D 堆积条形图 |
Bar3D100PercentStacked | 表示 3D 100% 堆积条形图 |
线 | 代表折线图 |
线堆叠 | 表示堆叠折线图 |
Line100Percent 堆叠 | 表示 100% 堆叠折线图 |
带数据标记的线 | 表示带有数据标记的折线图 |
LineStackedWithDataMarkers | 表示带有数据标记的堆积折线图 |
Line100PercentStackedWithDataMarkers | 表示带有数据标记的 100% 堆叠折线图 |
直线三维 | 表示 3D 折线图 |
馅饼 | 代表饼图 |
Pie3D | 表示 3D 饼图 |
派派 | 表示饼图的饼图 |
馅饼爆炸 | 代表分解饼图 |
饼图3D分解 | 表示 3D 爆炸饼图 |
馅饼吧 | 代表饼图条 |
分散 | 代表散点图 |
ScatterConnectedByCurvesWithDataMarker | 表示由曲线连接的散点图,带有数据标记 |
ScatterConnectedByCurvesWithoutDataMarker | 表示由曲线连接的散点图,没有数据标记 |
ScatterConnectedByLinesWithDataMarker | 表示由线连接的散点图,带有数据标记 |
ScatterConnectedByLinesWithoutDataMarker | 表示由线连接的散点图,没有数据标记 |
区域 | 代表面积图 |
面积堆叠 | 表示堆积面积图 |
面积 100% 堆叠 | 表示 100% 堆积面积图 |
三维区域 | 表示 3D 面积图 |
Area3D堆叠 | 表示 3D 堆积面积图 |
Area3D100PercentStacked | 表示 3D 100% 堆积面积图 |
油炸圈饼 | 代表甜甜圈图 |
甜甜圈爆炸 | 代表爆炸甜甜圈图 |
雷达 | 代表雷达图 |
带数据标记的雷达 | 用数据标记表示雷达图 |
雷达填充 | 表示填充雷达图 |
Surface3D | 表示 3D 曲面图 |
表面线框3D | 表示线框 3D 曲面图 |
表面轮廓 | 代表等高线图 |
表面轮廓线框 | 表示线框等高线图 |
气泡 | 代表气泡图 |
泡泡3D | 代表3D气泡图 |
圆柱 | 表示柱面图 |
圆柱叠层 | 表示堆积柱面图 |
圆柱100%堆叠 | 表示 100% 堆积柱面图 |
圆柱棒 | 表示圆柱形条形图。 |
圆柱形条堆叠 | 表示堆积柱形条形图 |
CylindericalBar100PercentStacked | 表示 100% 堆积柱形条形图 |
圆柱柱3D | 表示 3D 圆柱柱形图 |
锥体 | 代表圆锥图 |
圆锥堆叠 | 表示堆叠圆锥图 |
Cone100Percent堆叠 | 表示 100% 堆积圆锥图 |
锥形棒 | 代表锥形条形图 |
锥形条堆叠 | 表示堆叠圆锥形条形图 |
ConicalBar100PercentStacked | 表示 100% 堆积锥形条形图 |
锥形柱3D | 表示 3D 锥形柱形图 |
金字塔 | 代表金字塔图 |
金字塔堆叠 | 表示堆积金字塔图 |
金字塔100%堆积 | 表示 100% 堆积金字塔图 |
金字塔酒吧 | 代表金字塔条形图 |
金字塔酒吧堆叠 | 表示堆积金字塔条形图 |
PyramidBar100PercentStacked | 表示 100% 堆积金字塔条形图 |
金字塔柱3D | 表示 3D 金字塔柱形图 |
当您指定一个单元格范围作为数据源时,您只能设置从左上角到右下角的范围。例如,“A1:C3”有效,“C3:A1”无效。
|
金字塔图
执行示例代码时,金字塔图将添加到工作表中。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(); | |
// Adding a new worksheet to the Excel object | |
int sheetIndex = workbook.Worksheets.Add(); | |
// Obtaining the reference of the newly added worksheet by passing its sheet index | |
Worksheet worksheet = workbook.Worksheets[sheetIndex]; | |
// Adding sample values to cells | |
worksheet.Cells["A1"].PutValue(50); | |
worksheet.Cells["A2"].PutValue(100); | |
worksheet.Cells["A3"].PutValue(150); | |
worksheet.Cells["B1"].PutValue(4); | |
worksheet.Cells["B2"].PutValue(20); | |
worksheet.Cells["B3"].PutValue(50); | |
// Adding a chart to the worksheet | |
int chartIndex = worksheet.Charts.Add(Aspose.Cells.Charts.ChartType.Pyramid, 5, 0, 15, 5); | |
// Accessing the instance of the newly added chart | |
Aspose.Cells.Charts.Chart chart = worksheet.Charts[chartIndex]; | |
// Adding SeriesCollection (chart data source) to the chart ranging from "A1" cell to "B3" | |
chart.NSeries.Add("A1:B3", true); | |
// Saving the Excel file | |
workbook.Save(dataDir + "output.xls"); | |
折线图
在上面的示例中,只需更改图表类型到线创建折线图。下面提供了完整的源代码。执行代码时,工作表中会添加一个折线图。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(); | |
// Adding a new worksheet to the Excel object | |
int sheetIndex = workbook.Worksheets.Add(); | |
// Obtaining the reference of the newly added worksheet by passing its sheet index | |
Worksheet worksheet = workbook.Worksheets[sheetIndex]; | |
// Adding sample values to cells | |
worksheet.Cells["A1"].PutValue(50); | |
worksheet.Cells["A2"].PutValue(100); | |
worksheet.Cells["A3"].PutValue(150); | |
worksheet.Cells["B1"].PutValue(4); | |
worksheet.Cells["B2"].PutValue(20); | |
worksheet.Cells["B3"].PutValue(50); | |
// Adding a chart to the worksheet | |
int chartIndex = worksheet.Charts.Add(Aspose.Cells.Charts.ChartType.Line, 5, 0, 15, 5); | |
// Accessing the instance of the newly added chart | |
Aspose.Cells.Charts.Chart chart = worksheet.Charts[chartIndex]; | |
// Adding SeriesCollection (chart data source) to the chart ranging from "A1" cell to "B3" | |
chart.NSeries.Add("A1:B3", true); | |
// Saving the Excel file | |
workbook.Save( dataDir + "output.xls"); | |
气泡图
为了创建气泡图,图表类型必须设置为ChartType.气泡并且需要相应地设置一些额外的属性,例如 BubbleSizes、Values 和 XValues。执行以下代码后,气泡图将添加到工作表中。
符合数据标记图表
为了用数据标记图表创建一条线,图表类型必须设置为ChartType.LineWithDataMarkers并且需要相应地设置一些额外的属性,例如背景区域、系列标记、值和 X 值。执行以下代码后,带有数据标记图表的一行将添加到工作表中。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// Instantiate a workbook | |
Workbook workbook = new Workbook(); | |
// Access first worksheet | |
Worksheet worksheet = workbook.Worksheets[0]; | |
// Set columns title | |
worksheet.Cells[0, 0].Value = "X"; | |
worksheet.Cells[0, 1].Value = "Y"; | |
// Random data shall be used for generating the chart | |
Random R = new Random(); | |
// Create random data and save in the cells | |
for (int i = 1; i < 21; i++) | |
{ | |
worksheet.Cells[i, 0].Value = i; | |
worksheet.Cells[i, 1].Value = 0.8; | |
} | |
for (int i = 21; i < 41; i++) | |
{ | |
worksheet.Cells[i, 0].Value = i - 20; | |
worksheet.Cells[i, 1].Value = 0.9; | |
} | |
// Add a chart to the worksheet | |
int idx = worksheet.Charts.Add(ChartType.LineWithDataMarkers, 1, 3, 20, 20); | |
// Access the newly created chart | |
Chart chart = worksheet.Charts[idx]; | |
// Set chart style | |
chart.Style = 3; | |
// Set autoscaling value to true | |
chart.AutoScaling = true; | |
// Set foreground color white | |
chart.PlotArea.Area.ForegroundColor = Color.White; | |
// Set Properties of chart title | |
chart.Title.Text = "Sample Chart"; | |
// Set chart type | |
chart.Type = ChartType.LineWithDataMarkers; | |
// Set Properties of categoryaxis title | |
chart.CategoryAxis.Title.Text = "Units"; | |
//Set Properties of nseries | |
int s2_idx = chart.NSeries.Add("A2: A2", true); | |
int s3_idx = chart.NSeries.Add("A22: A22", true); | |
// Set IsColorVaried to true for varied points color | |
chart.NSeries.IsColorVaried = true; | |
// Set properties of background area and series markers | |
chart.NSeries[s2_idx].Area.Formatting = FormattingType.Custom; | |
chart.NSeries[s2_idx].Marker.Area.ForegroundColor = Color.Yellow; | |
chart.NSeries[s2_idx].Marker.Border.IsVisible = false; | |
// Set X and Y values of series chart | |
chart.NSeries[s2_idx].XValues = "A2: A21"; | |
chart.NSeries[s2_idx].Values = "B2: B21"; | |
// Set properties of background area and series markers | |
chart.NSeries[s3_idx].Area.Formatting = FormattingType.Custom; | |
chart.NSeries[s3_idx].Marker.Area.ForegroundColor = Color.Green; | |
chart.NSeries[s3_idx].Marker.Border.IsVisible = false; | |
// Set X and Y values of series chart | |
chart.NSeries[s3_idx].XValues = "A22: A41"; | |
chart.NSeries[s3_idx].Values = "B22: B41"; | |
// Save the workbook | |
workbook.Save(outputDir + @"LineWithDataMarkerChart.xlsx", Aspose.Cells.SaveFormat.Xlsx); |