1
votes

what i want to do: I have multiple files with different CreationDates. The X-Axis = Dates and the Y-Axis = Values from 0-100. I want to create a Line Chart displaying all the Values of each Day marked with an X -> e.g. (10.07.2017 = 50 and 60 -> displaying an X at 50 and one at 60 both at the 10.07.2017)

This is done already the code is this one:

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 
                                }

I am using a Chart with Series[0] ChartType = points

Now i want to mark points in Series1 for each existing X-Value(date). The Y-Value should be the average of all existing Y-Values of series[0] of this specific date. The Series1 ChartType = Line

Example:

Series[0] Points are:

X-Value = 10.07.2017 Y-Values = 10;20;30

X-Value = 11.07.2017 Y-Values = 50;60;

So Series1 Points SHOULD BE:

X-Value = 10.07.2017 Y-Value = 20 (average of 10,20,30)

X-Value = 11.07.2017 Y-Value = 55 (average of 50,60)

While Series[0] only displays the Marked Points (no connection between ech) and Series1 will draw a line between eacht point.

I researched already but couldnt find a specific answer for this...

What i tried:

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

But this does not work it says i cant convert double[] to double =/

Edit: I tried to use a FinancialFormula too, but this one does not really go through the average, it just predicts the further trend i used:

     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"]);

enter image description here

Here is an example of the current Chart. Now what i want to do is a Line displaying the Average of each X-Value (date) the red line (trend) was my attempt but this just does forecast the next values which is nice to have too but not what i wanted. I need to draw a line Through the Average of thepoints -> the start of the red line should be higher conidering there are 2 points on 100 and 2 on 90 it should be at 95 not ~92 where it is

1
Series[0] Points are: X-Value = 10.07.2017 Y-Values = 10;20;30 X-Value = 11.07.2017 Y-Values = 50;60; Are these two points? Or 3? Or 6 ?? - Can you post an image of the chart so far?TaW
sorry my bad, these are total 2 x values the first is X value (10.07.2017 -> a date) has 3 Y-Values ( 10 and 20 and30) and the second X value (11.07.2017) has 2 Y values (50 and 60) Ill add an image in a sec!christian890
I uploaded a picture of the current Graphchristian890

1 Answers

2
votes

Here is an example using LINQ; it first collects the averages per x-value from a data series and then adds them to an average series:

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

Note the LINQ:

  • I first group by the x-values, ie your date. (Make sure they are actually just dates and don't have varying timespanes, which would prevent the grouping).
  • Then I create an anonymous object from the gouping key and the average of the grouped data.
  • finally I copy them to a list from which I can create the DataPoints.

enter image description here

Note the the chart statistical/financial functions are not well-suited here as they work from one or more series but on a point by point basis.