使用Aspose Cells电子数据表格控件创建图表,Aspose Cells是一款功能强大的,在没安装Excel的情况下可对Excel文件进行读取,修改,导出的NET和Java控件产品,控件提供了Aspose.Cells.Charts类用于创建图表。
支持的图表类型如下:
- Column
- ColumnStacked
- Column100PercentStacked
- Column3DClustered
- Column3DStacked
- Column3D100PercentStacked
- Column3D
- Bar
- BarStacked
- Bar100PercentStacked
- Bar3DClustered
- Bar3DStacked
- Bar3D100PercentStacked
- Line
- LineStacked
- Line100PercentStacked
- LineWithDataMarkers
- LineStackedWithDataMarkers
- Line100PercentStackedWithDataMarkers
- Line3D
- Pie
- Pie3D
- PiePie
- PieExploded
- Pie3DExploded
- PieBar
- Scatter
- ScatterConnectedByCurvesWithDataMarker
- ScatterConnectedByCurvesWithoutDataMarker
- ScatterConnectedByLinesWithDataMarker
- ScatterConnectedByLinesWithoutDataMarker
- Area
- AreaStacked
- Area100PercentStacked
- Area3D
- Area3DStacked
- Area3D100PercentStacked
- Doughnut Represents Doughnut Chart
- DoughnutExploded
- Radar
- RadarWithDataMarkers
- RadarFilled
- Surface3D
- SurfaceWireframe3D
- SurfaceContour
- SurfaceContourWireframe
- Bubble
- Bubble3D
- Cylinder
- CylinderStacked
- Cylinder100PercentStacked
- CylindericalBar
- CylindericalBarStacked
- CylindericalBar100PercentStacked
- CylindericalColumn3D
- Cone
- ConeStacked
- Cone100PercentStacked
- ConicalBar
- ConicalBarStacked
- ConicalBar100PercentStacked
- ConicalColumn3D
- Pyramid
- PyramidStacked
- Pyramid100PercentStacked
- PyramidBar
- PyramidBarStacked
- PyramidBar100PercentStacked
- PyramidColumn3D
大致步奏如下:
1. 首先使用Cell对象的PutValue为单元格添加值
2. 通过Charts对象的Add方法添加图表
3. 指定图表的类型,通过 ChartType枚举。
4. 通过索引在Charts集合中存取Chart对象。
5. 使用chart对象的属性或者方法进行图表的相关设置,如 SeriesCollection对象用于指定数据源
当添加数据源时,可以指定单元格的范围如:"A1:C3"或者指定一些非临近的单元格,如:"A1, A3, A5",或者直接指定一序列值,如:"1,2,3"。
注意当指定数据源是单元格范围时,要遵循左上到右下的原则,如"A1:C3" 是有效的,"C3:A1"是无效的。
下面我们举例说明:
[C#]
//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 a sample value to "A1" cell
worksheet.Cells["A1"].PutValue(50);
//Adding a sample value to "A2" cell
worksheet.Cells["A2"].PutValue(100);
//Adding a sample value to "A3" cell
worksheet.Cells["A3"].PutValue(150);
//Adding a sample value to "B1" cell
worksheet.Cells["B1"].PutValue(4);
//Adding a sample value to "B2" cell
worksheet.Cells["B2"].PutValue(20);
//Adding a sample value to "B3" cell
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("C:\\book1.xls");
[VB.NET]
'Instantiating a Workbook object
Dim workbook As Workbook = New Workbook()
'Adding a new worksheet to the Excel object
Dim sheetIndex As Integer = workbook.Worksheets.Add()
'Obtaining the reference of the newly added worksheet by passing its sheet index
Dim worksheet As Worksheet = workbook.Worksheets(sheetIndex)
'Adding a sample value to "A1" cell
worksheet.Cells("A1").PutValue(50)
'Adding a sample value to "A2" cell
worksheet.Cells("A2").PutValue(100)
'Adding a sample value to "A3" cell
worksheet.Cells("A3").PutValue(150)
'Adding a sample value to "B1" cell
worksheet.Cells("B1").PutValue(4)
'Adding a sample value to "B2" cell
worksheet.Cells("B2").PutValue(20)
'Adding a sample value to "B3" cell
worksheet.Cells("B3").PutValue(50)
'Adding a chart to the worksheet
Dim chartIndex As Integer = worksheet.Charts.Add(Aspose.Cells.Charts.ChartType.Pyramid, 5, 0, 15, 5)
'Accessing the instance of the newly added chart
Dim chart As Aspose.Cells.Charts.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("C:\book1.xls")
下面的事例,我们简单的修改图表类型用于创建线性图:
[C#]
//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 a sample value to "A1" cell
worksheet.Cells["A1"].PutValue(50);
//Adding a sample value to "A2" cell
worksheet.Cells["A2"].PutValue(100);
//Adding a sample value to "A3" cell
worksheet.Cells["A3"].PutValue(150);
//Adding a sample value to "B1" cell
worksheet.Cells["B1"].PutValue(4);
//Adding a sample value to "B2" cell
worksheet.Cells["B2"].PutValue(20);
//Adding a sample value to "B3" cell
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("C:\\book1.xls");
VB.NET
'Instantiating a Workbook object
Dim workbook As Workbook = New Workbook()
'Adding a new worksheet to the Excel object
Dim sheetIndex As Integer = workbook.Worksheets.Add()
'Obtaining the reference of the newly added worksheet by passing its sheet index
Dim worksheet As Worksheet = workbook.Worksheets(sheetIndex)
'Adding a sample value to "A1" cell
worksheet.Cells("A1").PutValue(50)
'Adding a sample value to "A2" cell
worksheet.Cells("A2").PutValue(100)
'Adding a sample value to "A3" cell
worksheet.Cells("A3").PutValue(150)
'Adding a sample value to "B1" cell
worksheet.Cells("B1").PutValue(4)
'Adding a sample value to "B2" cell
worksheet.Cells("B2").PutValue(20)
'Adding a sample value to "B3" cell
worksheet.Cells("B3").PutValue(50)
'Adding a chart to the worksheet
Dim chartIndex As Integer = worksheet.Charts.Add(Aspose.Cells.Charts.ChartType.Line, 5, 0, 15, 5)
'Accessing the instance of the newly added chart
Dim chart As Aspose.Cells.Charts.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("C:\book1.xls")