Aspose.Cells如何对Excel工作表中创建的图表进行样式设置

作者:控件中国网   出处:控件中国网   2016-10-19 09:19:46   阅读:22

开发人员在使用Aspose.Cells时,可以通过控件提供的API在Excel中根据单元格数据来创建图表,并且还可以对创建的图表进行外观的设置,如绘制区域的背景、颜色设置、图表线条的设置、图表标题、轴标题以及刻度、图表中的表格线等,很多图表元素都可以根据不同的需求进行各种设置,下面的代码讲述了如何对图表绘制区域进行背景色等的设置,具体如下:
LV3EUJK.png
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Adding a new worksheet to the Workbook 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(60);
// Adding a sample value to "B2" cell
worksheet.Cells["B2"].PutValue(32);
// 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.Column, 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);
// Setting the foreground color of the plot area
chart.PlotArea.Area.ForegroundColor = Color.Blue;
// Setting the foreground color of the chart area
chart.ChartArea.Area.ForegroundColor = Color.Yellow;
// Setting the foreground color of the 1st SeriesCollection area
chart.NSeries[0].Area.ForegroundColor = Color.Red;
// Setting the foreground color of the area of the 1st SeriesCollection point
chart.NSeries[0].Points[0].Area.ForegroundColor = Color.Cyan;
// Filling the area of the 2nd SeriesCollection with a gradient
chart.NSeries[1].Area.FillFormat.SetOneColorGradient(Color.Lime, 1, Aspose.Cells.Drawing.GradientStyleType.Horizontal, 1);
// Saving the Excel file
workbook.Save("book1.out.xls");
Copyright© 2006-2015 ComponentCN.com all rights reserved.重庆磐岩科技有限公司(控件中国网) 版权所有 渝ICP备12000264号 法律顾问:元炳律师事务所
客服软件
live chat