Kategori eksenini nasıl ayarlarım
Olası Kullanım Senaryoları
Bir çalışma sayfasında bir grafik oluşturduktan sonra onun için kategori eksenini ayarlayabilirsiniz. Bu makalede, Aspose.Cells kullanarak Excel grafik için kategori eksenini nasıl ayarlayacağınızı, örnek kod ile göstereceğiz.
Örnek kodlardaki adımlar
-
Yeni bir çalışma kitabı oluşturun.
-
İlk çalışma sayfasında yeni bir grafik oluşturun.
-
İlk çalışma sayfasındaki hücrelere bazı değerler ekleyin.
-
Şimdi kategori eksenini ayarlayabilirsiniz, bunun için iki yol vardır: hücre verileri kullanarak veya doğrudan dizeleri kullanarak, her ikisi de örnek kodda gösterilmiştir.
-
Değer eksenini ayarlayın, sonucu görmek için çalışma kitabını kaydedin.
Örnek Kod
//How to Set Category Axis | |
//Your local test path | |
String path = @""; | |
//Create a new workbook | |
Workbook workbook = new Workbook(); | |
Worksheet worksheet = workbook.Worksheets[0]; | |
worksheet.Name = ("CHART"); | |
// Add a chart to the worksheet | |
int chartIndex = worksheet.Charts.Add(ChartType.Column, 8, 0, 20, 10); | |
Chart chart = worksheet.Charts[chartIndex]; | |
//Add some values to cells | |
worksheet.Cells["A1"].PutValue("Sales"); | |
worksheet.Cells["A2"].PutValue(100); | |
worksheet.Cells["A3"].PutValue(150); | |
worksheet.Cells["A4"].PutValue(130); | |
worksheet.Cells["A5"].PutValue(160); | |
worksheet.Cells["A6"].PutValue(150); | |
worksheet.Cells["B1"].PutValue("Days"); | |
worksheet.Cells["B2"].PutValue(1); | |
worksheet.Cells["B3"].PutValue(2); | |
worksheet.Cells["B4"].PutValue(3); | |
worksheet.Cells["B5"].PutValue(4); | |
worksheet.Cells["B6"].PutValue(5); | |
//Some values in string | |
String Sales = "100,150,130,160,150"; | |
String Days = "1,2,3,4,5"; | |
//Set Category Axis Data with string | |
chart.NSeries.CategoryData = "{" + Days + "}"; | |
//Or you can set Category Axis Data with data in cells, try it! | |
//chart.NSeries.CategoryData = "B2:B6"; | |
//Add Series to the chart | |
chart.NSeries.Add("Demand1", true); | |
//Set value axis with string | |
chart.NSeries[0].Values = "{" + Sales + "}"; | |
chart.NSeries.Add("Demand2", true); | |
//Set value axis with data in cells | |
chart.NSeries[1].Values = "A2:A6"; | |
//Set some Category Axis properties | |
chart.CategoryAxis.TickLabels.RotationAngle = 45; | |
chart.CategoryAxis.TickLabels.Font.Size = 8; | |
chart.Legend.Position = LegendPositionType.Bottom; | |
//Save the workbook to view the result file | |
workbook.Save(path + "Output.xlsx"); |