Tarih Ekseni
Olası Kullanım Senaryoları
Tarihleri kullanan çalışma sayfası verilerinden bir grafik oluşturduğunuzda ve tarihler grafikte yatay (kategori) eksen boyunca çizildiğinde, Aspose.cells kategori eksenini otomatik olarak bir tarih (zaman ölçeği) eksenine değiştirir. Tarih ekseni, çalışma sayfasındaki tarihler sıralı sırada veya aynı temel birimlerde olmasa bile, tarihleri belirli aralıklarla veya gün, ay veya yıl sayısı gibi temel birimlerde kronolojik sırada görüntüler. Varsayılan olarak Aspose.cells, çalışma sayfası verilerindeki herhangi iki tarih arasındaki en küçük farkı temel alarak tarih ekseni için temel birimleri belirler. Örneğin, tarihler arasındaki en küçük farkın yedi gün olduğu hisse senedi fiyatları verileriniz varsa, Excel temel birimi gün olarak ayarlar, ancak stoğun performansını görmek istiyorsanız temel birimi ay veya yıl olarak değiştirebilirsiniz. daha uzun bir süre.
Tarih Eksenini Microsoft Excel gibi kullanın
Lütfen yeni bir Excel dosyası oluşturan ve grafiğin değerlerini ilk çalışma sayfasına koyan aşağıdaki örnek koda bakın. Sonra bir grafik ekliyoruz ve türünü ayarlıyoruz.eksen ileZaman Ölçeği ve ardından temel birimleri Gün olarak ayarlayın.
Basit kod
// Create an instance of Workbook | |
Workbook workbook = new Workbook(); | |
// Get the first worksheet | |
Worksheet worksheet = workbook.Worksheets[0]; | |
// Add the sample values to cells | |
worksheet.Cells["A1"].PutValue("Date"); | |
// 14 means datetime format | |
Style style = worksheet.Cells.Style; | |
style.Number = 14; | |
// Put values to cells for creating chart | |
worksheet.Cells["A2"].SetStyle(style); | |
worksheet.Cells["A2"].PutValue(new DateTime(2022, 6, 26)); | |
worksheet.Cells["A3"].SetStyle(style); | |
worksheet.Cells["A3"].PutValue(new DateTime(2022, 5, 22)); | |
worksheet.Cells["A4"].SetStyle(style); | |
worksheet.Cells["A4"].PutValue(new DateTime(2022, 8, 3)); | |
worksheet.Cells["B1"].PutValue("Price"); | |
worksheet.Cells["B2"].PutValue(40); | |
worksheet.Cells["B3"].PutValue(50); | |
worksheet.Cells["B4"].PutValue(60); | |
// Adda chart to the worksheet | |
int chartIndex = worksheet.Charts.Add(Aspose.Cells.Charts.ChartType.Column, 9, 6, 21, 13); | |
// Access the instance of the newly added chart | |
Aspose.Cells.Charts.Chart chart = worksheet.Charts[chartIndex]; | |
// Add SeriesCollection (chart data source) to the chart ranging from "A1" cell to "B4" | |
chart.SetChartDataRange("A1:B4", true); | |
// Set the Axis type to Date time | |
chart.CategoryAxis.CategoryType = CategoryType.TimeScale; | |
// Set the base unit for CategoryAxis to days | |
chart.CategoryAxis.BaseUnitScale = TimeUnit.Days; | |
// Set the direction for the axis text to be vertical | |
chart.CategoryAxis.TickLabels.DirectionType = ChartTextDirectionType.Vertical; | |
// Fill the PlotArea area with nothing | |
chart.PlotArea.Area.FillFormat.FillType = FillType.None; | |
// Set max value of Y axis. | |
chart.ValueAxis.MaxValue = 70; | |
// Set major unit. | |
chart.ValueAxis.MajorUnit = 10; | |
// Save the file | |
workbook.Save("DateAxis.xlsx"); |