Spire.XLS 组件为用户提供了多种图表类型选择,其中涵盖了 Excel 自带的各种图表。这些图表类型被定义在枚举 Spire.Xls.ExcelChartType 中,共有以下 74 种:
图表类型 |
描述 |
1. ColumnClustered | Represents Clustered Column Chart |
2. ColumnStacked | Represents Stacked Column Chart |
3. Column100PercentStacked | Represents 100% Stacked Column Chart |
4. Column3DClustered | Represents 3D Clustered Column Chart |
5. Column3DStacked | Represents 3D Stacked Column Chart |
6. Column3D100PercentStacked | Represents 3D 100% Stacked Column Chart |
7. Column3D | Represents 3D Column Chart |
8. BarClustered | Represents Clustered Bar Chart |
9. BarStacked | Represents Stacked Bar Chart |
10. Bar100PercentStacked | Represents 100% Stacked Bar Chart |
11. Bar3DClustered | Represents 3D Clustered Bar Chart |
12. Bar3DStacked | Represents 3D Stacked Bar Chart |
13. Bar3D100PercentStacked | Represents 100% 3D Stacked Bar Chart |
14. Line | Represents Line Chart |
15. LineStacked | Represents Stacked Line Chart |
16. Line100PercentStacked | Represents 100% Stacked Line Chart |
17. LineMarkers | Represents Markers Line Chart |
18. LineMarkersStacked | Represents Stacked Markers Line Chart |
19. LineMarkers100PercentStacked | Represents 100% Stacked Markers Line Chart |
20. Line3D | Represents 3D Line Chart |
21. Pie | Represents Pie Chart |
22. Pie3D = 21 | Represents 3D Pie Chart |
23. PieOfPie | Represents Pie of Pie chart |
24. PieExploded | Represents Exploded Pie Chart |
25. Pie3DExploded | Represents 3D Exploded Pie Chart |
26. PieBar | Represents Bar Pie Chart |
27. ScatterMarkers | Represents Markers Scatter Chart |
28. ScatterSmoothedLineMarkers | Represents ScatterSmoothedLineMarkers Chart |
29. ScatterSmoothedLine | Represents ScatterSmoothedLine Chart |
30. ScatterLineMarkers | Represents ScatterLineMarkers Chart |
31. ScatterLine | Represents ScatterLine Chart |
32. Area | Represents Area Chart |
33. AreaStacked | Represents AreaStacked Chart |
34. Area100PercentStacked | Represents Area100PercentStacked Chart |
35. Area3D | Represents Area3D Chart |
36. Area3DStacked | Represents Area3DStacked Chart |
37. Area3D100PercentStacked | Represents Area3D100PercentStacked Chart |
38. Doughnut | Represents Doughnut Chart |
39. DoughnutExploded | Represents DoughnutExploded Chart |
40. Radar | Represents Radar Chart |
41. RadarMarkers | Represents RadarMarkers Chart |
42. RadarFilled | Represents RadarFilled Chart |
43. Surface3D | Represents Surface3D Chart |
44. Surface3DNoColor | Represents Surface3DNoColor Chart |
45. SurfaceContour | Represents SurfaceContour Chart |
46. SurfaceContourNoColor | Represents SurfaceContourNoColor Chart |
47. Bubble | Represents Bubble Chart |
48. Bubble3D | Represents Bubble3D Chart |
49. StockHighLowClose | Represents StockHighLowClose Chart |
50. StockOpenHighLowClose | Represents StockOpenHighLowClose Chart |
51. StockVolumeHighLowClose | Represents StockVolumeHighLowClose Chart |
52. StockVolumeOpenHighLowClose | Represents StockVolumeOpenHighLowClose Chart |
53. CylinderClustered | Represents CylinderClustered Chart |
54. CylinderStacked | Represents CylinderStacked Chart |
55. Cylinder100PercentStacked | Represents Cylinder100PercentStacked Chart |
56. CylinderBarClustered | Represents CylinderBarClustered Chart |
57. CylinderBarStacked | Represents CylinderBarStacked Chart |
58. CylinderBar100PercentStacked | Represents CylinderBar100PercentStacked Chart |
59. Cylinder3DClustered | Represents Cylinder3DClustered Chart |
60. ConeClustered | Represents ConeClustered Chart |
61. ConeStacked | Represents ConeStacked Chart |
62. Cone100PercentStacked | Represents Cone100PercentStacked Chart |
63. ConeBarClustered | Represents ConeBarClustered Chart |
64. ConeBarStacked | Represents ConeBarStacked Chart |
65. ConeBar100PercentStacked | Represents ConeBar100PercentStacked Chart |
66. Cone3DClustered | Represents Cone3DClustered Chart |
67. PyramidClustered | Represents PyramidClustered Chart |
68. PyramidStacked | Represents PyramidStacked Chart |
69. Pyramid100PercentStacked | Represents Pyramid100PercentStacked Chart |
70. PyramidBarClustered | Represents PyramidBarClustered Chart |
71. PyramidBarStacked | Represents PyramidBarStacked Chart |
72. PyramidBar100PercentStacked | Represents PyramidBar100PercentStacked Chart |
73. Pyramid3DClustered | Represents Pyramid3DClustered Chart |
74. CombinationChart | Represents Combination Chart |
该节中我们将从以上表格中选取三种常见图表来进行创建:Clustered Column Chart(柱状图),Pie Chart (饼状图)及Doughnut Chart(环形图)。
Clustered Column Chart 柱状图
C#
//加载Excel文档
Workbook workbook = new Workbook();
workbook.LoadFromFile("Sample.xlsx");
//获取工作表
Worksheet sheet = workbook.Worksheets[0];
//设置工作表的名称
sheet.Name = "柱状图";
sheet.GridLinesVisible = false;
//创建柱状图
Chart chart = sheet.Charts.Add(ExcelChartType.ColumnClustered);
//指定用于生成图表的数据区域
chart.DataRange = sheet.Range["A1:B5"];
chart.SeriesDataFromRange = false;
//指定图表所在的位置
chart.LeftColumn = 5;
chart.TopRow = 2;
chart.RightColumn = 15;
chart.BottomRow = 20;
//设置图表的名称以及x、y轴的名称
chart.ChartTitle = "部门信息";
chart.ChartTitleArea.IsBold = true;
chart.ChartTitleArea.Size = 12;
chart.PrimaryCategoryAxis.Title = "部门";
chart.PrimaryCategoryAxis.Font.IsBold = true;
chart.PrimaryCategoryAxis.TitleArea.IsBold = true;
chart.PrimaryValueAxis.Title = "人数";
chart.PrimaryValueAxis.HasMajorGridLines = false;
chart.PrimaryValueAxis.TitleArea.TextRotationAngle = 90;
chart.PrimaryValueAxis.MinValue = 0;
chart.PrimaryValueAxis.TitleArea.IsBold = true;
//5.设置图例的位置
chart.Legend.Position = LegendPositionType.Right;
//保存工作薄
workbook.SaveToFile("ColumnChart.xlsx", ExcelVersion.Version2013);
VB.NET
'加载Excel文档
Dim workbook As New Workbook()
workbook.LoadFromFile("Sample.xlsx")
'获取工作表
Dim sheet As Worksheet = workbook.Worksheets(0)
'设置工作表的名称
sheet.Name = "柱状图"
sheet.GridLinesVisible = False
'创建柱状图
Dim chart As Chart = sheet.Charts.Add(ExcelChartType.ColumnClustered)
'指定用于生成图表的数据区域
chart.DataRange = sheet.Range("A1:B5")
chart.SeriesDataFromRange = False
'指定图表所在的位置
chart.LeftColumn = 5
chart.TopRow = 2
chart.RightColumn = 15
chart.BottomRow = 20
'设置图表的名称以及x、y轴的名称
chart.ChartTitle = "部门信息"
chart.ChartTitleArea.IsBold = True
chart.ChartTitleArea.Size = 12
chart.PrimaryCategoryAxis.Title = "部门"
chart.PrimaryCategoryAxis.Font.IsBold = True
chart.PrimaryCategoryAxis.TitleArea.IsBold = True
chart.PrimaryValueAxis.Title = "人数"
chart.PrimaryValueAxis.HasMajorGridLines = False
chart.PrimaryValueAxis.TitleArea.TextRotationAngle = 90
chart.PrimaryValueAxis.MinValue = 0
chart.PrimaryValueAxis.TitleArea.IsBold = True
'5.设置图例的位置
chart.Legend.Position = LegendPositionType.Right
'保存工作薄
workbook.SaveToFile("ColumnChart.xlsx", ExcelVersion.Version2013)
Pie Chart 饼状图
C#
//创建工作簿
Workbook workbook = new Workbook();
//获取工作表
Worksheet sheet = workbook.Worksheets[0];
//设置工作表的名称
sheet.Name = "饼状图";
sheet.GridLinesVisible = false;
//向工作表添加数据
sheet.Range["A1"].Value = "年份";
sheet.Range["A2"].Value = "2002";
sheet.Range["A3"].Value = "2003";
sheet.Range["A4"].Value = "2004";
sheet.Range["A5"].Value = "2005";
sheet.Range["B1"].Value = "销售额";
sheet.Range["B2"].NumberValue = 4000;
sheet.Range["B3"].NumberValue = 6000;
sheet.Range["B4"].NumberValue = 7000;
sheet.Range["B5"].NumberValue = 8500;
sheet.Range["A1:B1"].Style.Font.IsBold = true;
sheet.Range["A2:B2"].Style.KnownColor = ExcelColors.LightYellow;
sheet.Range["A3:B3"].Style.KnownColor = ExcelColors.LightGreen1;
sheet.Range["A4:B4"].Style.KnownColor = ExcelColors.LightOrange;
sheet.Range["A5:B5"].Style.KnownColor = ExcelColors.LightTurquoise;
sheet.Range["A1:B5"].Style.Borders[BordersLineType.EdgeTop].Color = Color.FromArgb(0, 0, 128);
sheet.Range["A1:B5"].Style.Borders[BordersLineType.EdgeTop].LineStyle = LineStyleType.Thin;
sheet.Range["A1:B5"].Style.Borders[BordersLineType.EdgeBottom].Color = Color.FromArgb(0, 0, 128);
sheet.Range["A1:B5"].Style.Borders[BordersLineType.EdgeBottom].LineStyle = LineStyleType.Thin;
sheet.Range["A1:B5"].Style.Borders[BordersLineType.EdgeLeft].Color = Color.FromArgb(0, 0, 128);
sheet.Range["A1:B5"].Style.Borders[BordersLineType.EdgeLeft].LineStyle = LineStyleType.Thin;
sheet.Range["A1:B5"].Style.Borders[BordersLineType.EdgeRight].Color = Color.FromArgb(0, 0, 128);
sheet.Range["A1:B5"].Style.Borders[BordersLineType.EdgeRight].LineStyle = LineStyleType.Thin;
sheet.Range["B2:C5"].Style.NumberFormat = "\"$\"#,##0";
//创建饼状图表
Chart chart = sheet.Charts.Add(ExcelChartType.Pie);
//指定用于生成图表的数据区域
chart.DataRange = sheet.Range["B2:B5"];
chart.SeriesDataFromRange = false;
//指定图表所在位置
chart.LeftColumn = 1;
chart.TopRow = 7;
chart.RightColumn = 9;
chart.BottomRow = 26;
//设置图表的名称
chart.ChartTitle = "年销售额";
chart.ChartTitleArea.IsBold = true;
chart.ChartTitleArea.Size = 12;
//设置图表系列的值
Spire.Xls.Charts.ChartSerie cs = chart.Series[0];
cs.CategoryLabels = sheet.Range["A2:A5"];
cs.Values = sheet.Range["B2:B5"];
cs.DataPoints.DefaultDataPoint.DataLabels.HasValue = true;
chart.PlotArea.Fill.Visible = false;
//保存工作薄
workbook.SaveToFile("PieChart.xls");
VB.NET
'创建工作簿
Dim workbook As New Workbook()
'获取工作表
Dim sheet As Worksheet = workbook.Worksheets(0)
'设置工作表的名称
sheet.Name = "饼状图"
sheet.GridLinesVisible = False
'向工作表添加数据
sheet.Range("A1").Value = "年份"
sheet.Range("A2").Value = "2002"
sheet.Range("A3").Value = "2003"
sheet.Range("A4").Value = "2004"
sheet.Range("A5").Value = "2005"
sheet.Range("B1").Value = "销售额"
sheet.Range("B2").NumberValue = 4000
sheet.Range("B3").NumberValue = 6000
sheet.Range("B4").NumberValue = 7000
sheet.Range("B5").NumberValue = 8500
sheet.Range("A1:B1").Style.Font.IsBold = True
sheet.Range("A2:B2").Style.KnownColor = ExcelColors.LightYellow
sheet.Range("A3:B3").Style.KnownColor = ExcelColors.LightGreen1
sheet.Range("A4:B4").Style.KnownColor = ExcelColors.LightOrange
sheet.Range("A5:B5").Style.KnownColor = ExcelColors.LightTurquoise
sheet.Range("A1:B5").Style.Borders(BordersLineType.EdgeTop).Color = Color.FromArgb(0, 0, 128)
sheet.Range("A1:B5").Style.Borders(BordersLineType.EdgeTop).LineStyle = LineStyleType.Thin
sheet.Range("A1:B5").Style.Borders(BordersLineType.EdgeBottom).Color = Color.FromArgb(0, 0, 128)
sheet.Range("A1:B5").Style.Borders(BordersLineType.EdgeBottom).LineStyle = LineStyleType.Thin
sheet.Range("A1:B5").Style.Borders(BordersLineType.EdgeLeft).Color = Color.FromArgb(0, 0, 128)
sheet.Range("A1:B5").Style.Borders(BordersLineType.EdgeLeft).LineStyle = LineStyleType.Thin
sheet.Range("A1:B5").Style.Borders(BordersLineType.EdgeRight).Color = Color.FromArgb(0, 0, 128)
sheet.Range("A1:B5").Style.Borders(BordersLineType.EdgeRight).LineStyle = LineStyleType.Thin
sheet.Range("B2:C5").Style.NumberFormat = """$""#,##0"
'创建饼状图表
Dim chart As Chart = sheet.Charts.Add(ExcelChartType.Pie)
'指定用于生成图表的数据区域
chart.DataRange = sheet.Range("B2:B5")
chart.SeriesDataFromRange = False
'指定图表所在位置
chart.LeftColumn = 1
chart.TopRow = 7
chart.RightColumn = 9
chart.BottomRow = 26
'设置图表的名称
chart.ChartTitle = "年销售额"
chart.ChartTitleArea.IsBold = True
chart.ChartTitleArea.Size = 12
'设置图表系列的值
Dim cs As Spire.Xls.Charts.ChartSerie = chart.Series(0)
cs.CategoryLabels = sheet.Range("A2:A5")
cs.Values = sheet.Range("B2:B5")
cs.DataPoints.DefaultDataPoint.DataLabels.HasValue = True
chart.PlotArea.Fill.Visible = False
'保存工作薄
workbook.SaveToFile("PieChart.xls")
Doughnut Chart 环形图
C#
//创建工作薄
Workbook workbook = new Workbook();
workbook.Version = ExcelVersion.Version2013;
//获取工作表
Worksheet sheet = workbook.Worksheets[0];
sheet.GridLinesVisible = false;
//向工作表添加数据
sheet.Range["A1"].Value = "国家";
sheet.Range["A1"].Style.Font.IsBold = true;
sheet.Range["A2"].Value = "古巴";
sheet.Range["A3"].Value = "墨西哥";
sheet.Range["A4"].Value = "法国";
sheet.Range["A5"].Value = "德国";
sheet.Range["B1"].Value = "销售额";
sheet.Range["B1"].Style.Font.IsBold = true;
sheet.Range["B2"].NumberValue = 6000;
sheet.Range["B3"].NumberValue = 8000;
sheet.Range["B4"].NumberValue = 9000;
sheet.Range["B5"].NumberValue = 8500;
//创建环形图
Chart chart = sheet.Charts.Add();
chart.ChartType = ExcelChartType.Doughnut;
//指定用于生成图表的数据区域
chart.DataRange = sheet.Range["A1:B5"];
chart.SeriesDataFromRange = false;
//指定图表的位置
chart.LeftColumn = 4;
chart.TopRow = 2;
chart.RightColumn = 12;
chart.BottomRow = 22;
//设置图表的名称
chart.ChartTitle = "销售额百分比";
chart.ChartTitleArea.IsBold = true;
chart.ChartTitleArea.Size = 10;
//设置图表系列的值
foreach (ChartSerie cs in chart.Series)
{
cs.DataPoints.DefaultDataPoint.DataLabels.HasPercentage = true;
}
//设置图例的位置
chart.Legend.Position = LegendPositionType.Top;
//保存工作薄
workbook.SaveToFile("DoughnutChart.xlsx");
VB.NET
'创建工作薄
Dim workbook As New Workbook()
workbook.Version = ExcelVersion.Version2013
'获取工作表
Dim sheet As Worksheet = workbook.Worksheets(0)
sheet.GridLinesVisible = False
'向工作表添加数据
sheet.Range("A1").Value = "国家"
sheet.Range("A1").Style.Font.IsBold = True
sheet.Range("A2").Value = "古巴"
sheet.Range("A3").Value = "墨西哥"
sheet.Range("A4").Value = "法国"
sheet.Range("A5").Value = "德国"
sheet.Range("B1").Value = "销售额"
sheet.Range("B1").Style.Font.IsBold = True
sheet.Range("B2").NumberValue = 6000
sheet.Range("B3").NumberValue = 8000
sheet.Range("B4").NumberValue = 9000
sheet.Range("B5").NumberValue = 8500
'创建环形图
Dim chart As Chart = sheet.Charts.Add()
chart.ChartType = ExcelChartType.Doughnut
'指定用于生成图表的数据区域
chart.DataRange = sheet.Range("A1:B5")
chart.SeriesDataFromRange = False
'指定图表的位置
chart.LeftColumn = 4
chart.TopRow = 2
chart.RightColumn = 12
chart.BottomRow = 22
'设置图表的名称
chart.ChartTitle = "销售额百分比"
chart.ChartTitleArea.IsBold = True
chart.ChartTitleArea.Size = 10
'设置图表系列的值
For Each cs As ChartSerie In chart.Series
cs.DataPoints.DefaultDataPoint.DataLabels.HasPercentage = True
Next
'设置图例的位置
chart.Legend.Position = LegendPositionType.Top
'保存工作薄
workbook.SaveToFile("DoughnutChart.xlsx")