【问题标题】:C# chart with multiple series, each with different x axis具有多个系列的 C# 图表,每个系列具有不同的 x 轴
【发布时间】:2017-11-24 13:42:41
【问题描述】:

我正在尝试将数据导出到 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.

我一直在寻找解决方案,并尝试解决类似问题的解决方案,但还没有一个是“正确的”。 这是我到目前为止所拥有的:

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;
}

如果有任何区别,日期是数据库中的日期时间,但在将其存储在列表中时会转换为字符串。

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

【问题讨论】:

    标签: c# excel-interop


    【解决方案1】:

    我使用 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();
            }
    

    【讨论】:

    • 遗憾的是,这对我也没有帮助,因为我必须使用互操作,它没有 ShowHiddenData 属性等等。感谢您的努力,欢迎提出任何其他建议!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多