使用Chart.SetChartDataRange方法轻松设置图表。
Contents
[
Hide
]
Aspose.Cells现在提供Chart.SetChartDataRange()方法来轻松设置图表。使用此方法,您不需要分别添加系列和分类轴数据。
以下示例代码解释了使用Chart.SetChartDataRange()方法轻松设置图表。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); | |
// Create new workbook | |
Workbook workbook = new Workbook(FileFormatType.Xlsx); | |
// Access first worksheet | |
Worksheet worksheet = workbook.Worksheets[0]; | |
// Add data for chart | |
// Category Axis Values | |
worksheet.Cells["A2"].PutValue("C1"); | |
worksheet.Cells["A3"].PutValue("C2"); | |
worksheet.Cells["A4"].PutValue("C3"); | |
// First vertical series | |
worksheet.Cells["B1"].PutValue("T1"); | |
worksheet.Cells["B2"].PutValue(6); | |
worksheet.Cells["B3"].PutValue(3); | |
worksheet.Cells["B4"].PutValue(2); | |
// Second vertical series | |
worksheet.Cells["C1"].PutValue("T2"); | |
worksheet.Cells["C2"].PutValue(7); | |
worksheet.Cells["C3"].PutValue(2); | |
worksheet.Cells["C4"].PutValue(5); | |
// Third vertical series | |
worksheet.Cells["D1"].PutValue("T3"); | |
worksheet.Cells["D2"].PutValue(8); | |
worksheet.Cells["D3"].PutValue(4); | |
worksheet.Cells["D4"].PutValue(2); | |
// Create Column chart with easy way | |
int idx = worksheet.Charts.Add(ChartType.Column, 6, 5, 20, 13); | |
Chart ch = worksheet.Charts[idx]; | |
ch.SetChartDataRange("A1:D4", true); | |
// Save the workbook | |
workbook.Save(dataDir + "output_out.xlsx", SaveFormat.Xlsx); |