C#图表有多个系列,每个系列都有不同的x轴

时间:2017-11-24 13:42:42

标签: c# excel-interop

我试图将数据导出到excel文件(工作正常)并创建图表。

我拥有:3列 - ID,值,日期。有多个行具有相同的ID,但值和日期时间不同。

例:
ID - 值 - 日期时间
1 - 14 - 21.11.2017 2:17:08
1 - 15 - 22.11.2017 14:25:45
3 - 12.5 - 21.11.2017 15:12:12
3 - 18.7 - 21.11.2017 19:27:35
3 - 13 - 22.11.2017 0:47:17

我想要的是一个图表,其中Value是Y轴,每个ID是一个系列,Datetime是X轴。但是,每个ID的日期时间都不同。

这应该是它的外观,但在X轴上使用日期而不是数字。 Chart Image

我一直在寻找一个解决方案,并尝试类似问题的解决方案,但没有一个问题在#34;然而。 这就是我到目前为止所做的:

Excel.ChartObjects ChartObjects = (Excel.ChartObjects)WS.ChartObjects();
Excel.ChartObject chartObject = ChartObjects.Add(400, 40, 450, 300);
chartObject.Chart.ChartType = Excel.XlChartType.xlXYScatterLines;
Excel.SeriesCollection oSeriesCollection = (Excel.SeriesCollection)chartObject.Chart.SeriesCollection();

Excel.Axis xAxis = (Excel.Axis)chartObject.Chart.Axes(Excel.XlAxisType.xlCategory, Excel.XlAxisGroup.xlPrimary);
xAxis.HasTitle = true;
xAxis.AxisTitle.Text = "Date";   

Excel.Axis yAxis = (Excel.Axis)chartObject.Chart.Axes(Excel.XlAxisType.xlValue, Excel.XlAxisGroup.xlPrimary);
yAxis.HasTitle = true;
yAxis.AxisTitle.Text = "Value";

int startPos = 4;
int endPos = 0;
int previousCount = 0;
for (int i = 0; i < Count; i++)
{
    startPos = startPos + previousCount;
    endPos = startPos + CountList[i].Count - 1;

    Excel.Series oSeries = oSeriesCollection.NewSeries();
    oSeries.Values = WS.Range["E" + startPos, "E" + endPos];
    //oSeries.XValues = WS.Range["F" + startPos, "F" + endPos];   //doesn't really do anything
    oSeries.Name = "id " + CountList[i].Number.ToString();
    previousCount = CountList[i].Count;
}

如果它有任何区别,date是数据库中的datetime,但在List中存储时会转换为字符串。

如何按照我需要的方式设置x轴?有可能吗?

1 个答案:

答案 0 :(得分:0)

我使用EPPlus做了类似的事情。它是在asp.net MVC 5中,因此可能需要更改保存文档以满足您的需求。在我的情况下,读取年龄是您想要在Y轴上显示的值,它们在X轴上有日期。希望这至少会给你一些建设的东西。注意其中的注释,否则图表不能按预期工作。

        var results = GetResultsSomehow();

        ExcelPackage excel = new ExcelPackage();
        var workSheet = excel.Workbook.Worksheets.Add("Reading Ages");

        workSheet.TabColor = System.Drawing.Color.Black;

        var scatterChart = (ExcelScatterChart)workSheet.Drawings.AddChart("Reading Ages", eChartType.XYScatterSmooth);
        scatterChart.SetPosition(3, 1, 4, 1);
        scatterChart.SetSize(800, 500);
        scatterChart.XAxis.Format = "dd/mm/yyyy";
        // must display hidden data and empty as gaps, otherwise the date axis will explode 
        scatterChart.ShowHiddenData = true;
        scatterChart.DisplayBlanksAs = eDisplayBlanksAs.Gap;

        int rowIndex = 4;
        int stopIndex = 4;
        foreach (var student in results.Select(a=> a.Student).Distinct())
        {
            int start = rowIndex;
            int stop = start + stopIndex;

            workSheet.Cells[rowIndex, 1].Value = student.Surname;
            foreach(var row in results.Where(a=> a.Student == student))
            {
                workSheet.Cells[rowIndex, 2].Value = row.Age;
                workSheet.Cells[rowIndex, 3].Value = row.Date;

                rowIndex++;
                stopIndex++;
            }

            stop = rowIndex;
            var series = scatterChart.Series.Add(workSheet.Cells[start, 2, stop - 1, 2], workSheet.Cells[start, 3, stop - 1, 3]);
            series.Header = student.Surname;           
        }

        string excelName = identifier + " reading ages";
        using (var memoryStream = new MemoryStream())
        {
            Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
            Response.AddHeader("content-disposition", "attachment; filename=" + excelName + ".xlsx");
            excel.SaveAs(memoryStream);
            memoryStream.WriteTo(Response.OutputStream);
            Response.Flush();
            Response.End();
        }