日付軸
Contents
[
Hide
]
考えられる使用シナリオ
日付を使用するワークシート データからグラフを作成し、グラフの水平 (カテゴリ) 軸に沿って日付をプロットすると、Aspose.cells によってカテゴリ軸が日付 (時間スケール) 軸に自動的に変更されます。 日付軸は、ワークシートの日付が連続した順序または同じ基本単位でない場合でも、特定の間隔または基本単位 (日、月、年など) で日付を時系列順に表示します。 既定では、Aspose.cells は、ワークシート データ内の任意の 2 つの日付間の最小差に基づいて、日付軸の基本単位を決定します。たとえば、日付間の最小差が 7 日である株価のデータがある場合、Excel は基本単位を日に設定しますが、株価のパフォーマンスを見たい場合は、基本単位を月または年に変更できます。より長い期間。
日付軸を Microsoft Excel のように処理する
新しい Excel ファイルを作成し、グラフの値を最初のワークシートに入れる次のサンプル コードを参照してください。 次に、チャートを追加し、軸 にタイムスケール次に、基本単位を [日数] に設定します。
サンプルコード
This file contains 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
// 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"); |