Asse della data
Possibili scenari di utilizzo
Quando crei un grafico dai dati del foglio di lavoro che utilizza le date e le date vengono tracciate lungo l’asse orizzontale (categoria) nel grafico, Aspose.cells modifica automaticamente l’asse delle categorie in un asse della data (scala temporale). Un asse della data visualizza le date in ordine cronologico a intervalli o unità di base specifici, ad esempio il numero di giorni, mesi o anni, anche se le date nel foglio di lavoro non sono in ordine sequenziale o nelle stesse unità di base. Per impostazione predefinita, Aspose.cells determina le unità di base per l’asse della data in base alla minima differenza tra due date qualsiasi nei dati del foglio di lavoro. Ad esempio, se si dispone di dati per i prezzi delle azioni in cui la differenza minima tra le date è di sette giorni, Excel imposta l’unità di base su giorni, ma è possibile modificare l’unità di base su mesi o anni se si desidera vedere la performance del titolo nel corso un periodo di tempo più lungo.
Gestisci l’asse della data come Microsoft Excel
Si prega di vedere il seguente codice di esempio che crea un nuovo file Excel e inserisce i valori del grafico nel primo foglio di lavoro. Quindi aggiungiamo un grafico e impostiamo il tipo di fileAsse aScala temporale e quindi imposta le unità di base su Giorni.
Codice d’esempio
// 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"); |