【问题标题】:LineChart Draw Line through average of a SeriesLineChart 通过系列的平均值绘制线
【发布时间】:2017-07-11 10:36:52
【问题描述】:

我想做什么: 我有多个具有不同创建日期的文件。 X 轴 = 日期,Y 轴 = 0-100 之间的值。我想创建一个折线图,显示用 X 标记的每一天的所有值 -> 例如(2017 年 7 月 10 日 = 50 和 60 -> 在 2017 年 7 月 10 日在 50 和 60 显示 X)

这已经完成了,代码就是这个:

for (int i=0; i< orderedList.Count(); i++)//iterating through Files
                                {
                                    int max_col = 0, max_rows = 0;
                                    double[][] temp = FileManager.ReadCSV(orderedList[i].FullName.ToString(),ref max_rows, ref max_col); //reading the file to a double[][]
                                     chartGraphic.Series[0].Points.AddXY(orderedList[i].CreationTime.Date, 100-Statistics.Prozent_NIO(temp, 1)); //adding the point to the Chart -> Prozent_NIO returns the y-value 
                                }

我正在使用带有 Series[0] ChartType = points 的图表

现在我想在 Series1 中为每个现有的 X 值(日期)标记点。 Y 值应该是该特定日期的 series[0] 的所有现有 Y 值的平均值。 系列1ChartType = Line

示例:

系列[0]点是:

X 值 = 10.07.2017 Y 值 = 10;20;30

X 值 = 11.07.2017 Y 值 = 50;60;

所以 Series1 点数应该是:

X 值 = 10.07.2017 Y 值 = 20(10、20、30 的平均值)

X 值 = 11.07.2017 Y 值 = 55(50,60 的平均值)

虽然 Series[0] 仅显示标记点(ech 之间没有连接),但 Series1 将在每个点之间画一条线。

我已经研究过,但找不到具体的答案...

我尝试了什么:

double temp_average=0;
double temp_number=0;
for (int i=0; i< orderedList.Count(); i++)// (FileInfo file in orderedList)
  {
    int max_col = 0, max_rows = 0;
    double[][] temp = FileManager.ReadCSV(orderedList[i].FullName.ToString(),ref max_rows, ref max_col);                       chartGraphic.Series[0].Points.AddXY(orderedList[i].CreationTime.Date, 100-Statistics.Prozent_NIO(temp, 1));
    if (i != 0)
     {
        if (orderedList[i].CreationTime.Date == orderedList[i - 1].CreationTime.Date) //times are similar -> add value to temp_averge and count number up
           {

              temp_average += Convert.ToDouble(chartGraphic.Series[0].Points[i - 1].YValues);
              temp_number++;
           }
           else //Times different -> add last y value then calculate average and reset the variables
             {

                temp_average += Convert.ToDouble(chartGraphic.Series[0].Points[i - 1].YValues); //add last point
                temp_number++;
                chartGraphic.Series[1].Points.AddXY(orderedList[i - 1].CreationTime.Date, temp_average / temp_number); // add point in series 1 
                  temp_average = 0;
                  temp_number = 0;*/
               }
        }
   }

但这不起作用,它说我无法将 double[] 转换为 double =/

编辑: 我也尝试使用 FinancialFormula,但这个并没有真正通过平均值,它只是预测我使用的进一步趋势:

     chartGraphic.Series.Add("TrendLine");
     chartGraphic.Series["TrendLine"].ChartType = Graph.SeriesChartType.Line;
    chartGraphic.Series["TrendLine"].Color = Color.Red;
    chartGraphic.DataManipulator.IsStartFromFirst = true;
    chartGraphic.DataManipulator.FinancialFormula(FinancialFormula.Forecasting, "Linear, 1, false, false", chartGraphic.Series[0], chartGraphic.Series["TrendLine"]);

这是当前图表的示例。现在我想做的是一条显示每个 X 值(日期)平均值的线,红线(趋势)是我的尝试,但这只是预测下一个值,这也很好,但不是我想要的。我需要通过点的平均值画一条线->考虑到 100 上有 2 个点和 90 上有 2 个点,红线的起点应该更高,它应该在 95 而不是 ~92 的位置

【问题讨论】:

  • 系列[0] 点是:X 值 = 10.07.2017 Y 值 = 10;20;30 X 值 = 11.07.2017 Y 值 = 50;60; 是这两点吗?还是3?还是6?? - 你能发布一张到目前为止的图表吗?
  • 对不起,这些是总共 2 个 x 值,第一个是 X 值(10.07.2017 -> 一个日期)有 3 个 Y 值(10 和 20 和 30),第二个 X 值(11.07 .2017) 有 2 个 Y 值(50 和 60)我会在几秒钟内添加一个图像!
  • 我上传了当前Graph的图片

标签: c# charts average linechart


【解决方案1】:

这是一个使用 LINQ 的示例;它首先从数据系列中收集每个 x 值的平均值,然后将它们添加到平均系列中:

Series dataSeries = chartGraphic.Series[0];
Series avgSeries = chartGraphic.Series["TrendLine"];

var grouped = dataSeries.Points.GroupBy(x => x.XValue)
                               .Select(x => new { xval = x.Key, 
                                           yavg = x.Average(y => y.YValues[0]) })
                               .ToList();

foreach (var kv in grouped) avgSeries.Points.AddXY(kv.xval, kv.yavg);

注意 LINQ:

  • 我首先按 x 值分组,即您的日期。 (确保它们实际上只是日期并且没有不同的时间跨度,这会阻止分组)。
  • 然后,我根据组合键和分组数据的平均值创建一个匿名对象。
  • 最后我将它们复制到一个列表中,我可以从中创建DataPoints

请注意,图表统计/财务函数在此处并不适用,因为它们从一个或多个系列开始工作,而是逐点进行。

【讨论】:

  • 非常感谢!它就像一个魅力! =)正是我需要的!祝你有美好的一天,再次感谢!
  • 请问Series[0] 在这里做什么?如果我想选择多个系列并从中计算平均值,我将如何实施此解决方案?谢谢
  • 顾名思义,它是保存原始数据的系列。请注意,问题是关于几个数据点具有相同 x 值的系列! 如果是您的问题的情况,您可以尝试编写类似的内容。是吗??
  • @TaW 不幸的是:/。我有几个要用作数据的 csv 文件,然后绘制文件产生的趋势线/平均线,并显示文件曲线。
  • @TaW 如果您想看一下,我已经创建了一个问题。谢谢! stackoverflow.com/questions/51261839/…
猜你喜欢
  • 2021-08-28
  • 2018-12-28
  • 1970-01-01
  • 1970-01-01
  • 2018-06-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-28
相关资源
最近更新 更多