简体   繁体   中英

C# Excel Interop Charts with multi series - X axis as DateTime

I have an excel addin implemented in C#. This connects to a server and obtains <key, value1, value2, value3..> pairs, where key is DateTime. After data is fetched, this is populated in Excel worksheet. This data is used to plot multi-series line graph. I am using ChartWizard and on the Chart object I add multiple series using .NewSeries option. What is the best way to specify that my X axis is DateTime. I see the behaviour differs when I use ScatterLines or XLLines. I am unable to find an approach where I can say to the chart that my X axis is DateTime and specify a dateformat to same Any help will be appreciated. Vikron

Microsoft.Office.Tools.Excel.Chart chart = worksheet.Controls.AddChart(0, 0, xx, yy, "Trend");
chart.ChartWizard(Type.Missing,
        Microsoft.Office.Interop.Excel.XlChartType.xlXYScatterLines,
        Type.Missing,
        XlRowCol.xlColumns,
        Type.Missing,
        Type.Missing,
        true,
        "Trend",
        "Time stamp",
        Type.Missing,
        Type.Missing);
chart.AutoScaling = false;
chart.ChartStyle = 34;
chart.Legend.Position = XlLegendPosition.xlLegendPositionBottom;
chart.HasTitle = true;
chart.ChartType = XlChartType.xlXYScatterLines;
chart.ChartTitle.Text = "Trend";

foreach (series)
{
    Excel.Series xlSeries = (Excel.Series)((Excel.SeriesCollection)chart.SeriesCollection()).NewSeries();
    Microsoft.Office.Interop.Excel.Range seriesRange = worksheet.get_Range(series.seriesBeginRange,
                                                        worksheet.get_Range(series.seriesBeginRange).get_End(XlDirection.xlDown));
    xlSeries.Name = series.seriesName;
    xlSeries.XValues = timeSeriesRange; //This has datetime
    xlSeries.ChartType = Microsoft.Office.Interop.Excel.XlChartType.xlXYScatterLines;
    xlSeries.Values = seriesRange;
}

cell.NumberFormat = "MM/dd/yy HH:mm";
cell.Value = ts;

Found, if not best, something that works for me. When I was populating with DateTime formatted to the desired date time display pattern, the Cell type was getting set to "General" and of-course the value was treated as "String", with this xlLines did worked perfectly. Then the problem with this is that user will not be able to modify the DateTime format into a different display pattern as the cells were updated as "String". Now, I populate the Excel cell with C# DateTime object. I set the format as

cell.NumberFormat = "m/d/yy h:mm;@";

To obtain the exact NumberFormat to Format Axis=>Number=>Category=>Format Code properties in Excel. This will let Excel treat the Type as Time and user can modify the date time format. Then, I modified to xlXLScatterLines, modified Tickspaces as below:

            Axis xAxis = (Excel.Axis)chart.Axes(Excel.XlAxisType.xlCategory);
            TickLabels ticks = xAxis.TickLabels;
            xAxis.MinimumScale = timeSeriesRange.Value2[1, 1];
            xAxis.MaximumScale = timeSeriesRange.Value2[timeSeriesRange.Count, 1];
            xAxis.MajorTickMark = XlTickMark.xlTickMarkCross;
            xAxis.MinorTickMark = XlTickMark.xlTickMarkInside;
            xAxis.MajorUnit = 0.005;
            xAxis.HasMinorGridlines = true;
            xAxis.HasMajorGridlines = true;
            ticks.Orientation = XlTickLabelOrientation.xlTickLabelOrientationUpward;
            chart.PlotArea.Select();
            chart.PlotArea.Height = 300;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM