Show / Hide Table of Contents

Set the Category Axis type to Date Axis

Set the category axis type to date with month units. The date formatting is "yyyy-mmm-dd".

Output: DateCategoryAxisMonths.xlsx

var workbook = new Workbook();
var worksheet = workbook.Worksheets[0];

worksheet["I1"].Value = "Series 1";
worksheet.GetRange("I2:I13").SetValue(new[] { 1,2,3,4,5,6,7,8,9,10,11,12 });
worksheet.GetRange("J2:J13").SetValue(new[]
    {
        new DateTime(2020, 1, 1), new DateTime(2020, 2, 1), new DateTime(2020, 3, 1), new DateTime(2020, 4, 1),
        new DateTime(2020, 5, 1), new DateTime(2020, 6, 1), new DateTime(2020, 7, 1), new DateTime(2020, 8, 1),
        new DateTime(2020, 9, 1), new DateTime(2020, 10, 1), new DateTime(2020, 11, 1),
        new DateTime(2020, 12, 1)
    });
worksheet.Range["J2:J13"].Format = new DateTimeFormat("yyyy-mmm-dd");
var chart1 = worksheet.Charts.Add<LineChart>("A1");
chart1.Size.HeightInches = 2.5;
chart1.Size.WidthInches = 3.5;
chart1.DataSeries.Add("Sheet1!$I$1","Sheet1!$J$2:$J$13","Sheet1!$I$2:$I$13");

chart1.CategoryAxis.AxisType = CategoryAxisType.DateAxis;
chart1.CategoryAxis.DateUnits.MajorUnit = TimeUnit.Months;
chart1.CategoryAxis.NumberFormat = new DateTimeFormat("yyyy-mmm-dd");

workbook.SaveAs("DateCategoryAxisMonths.xlsx");
Back to top Generated by DocFX