LineChart 通过系列的平均值画线
LineChart Draw Line through average of a Series
我想做什么:
我有多个具有不同 CreationDates 的文件。 X 轴 = 日期,Y 轴 = 0-100 之间的值。我想创建一个折线图,显示标有 X 的每一天的所有值 -> 例如(2017 年 7 月 10 日 = 50 和 60 -> 在 2017 年 7 月 10 日显示 50 处的 X 和 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
}
我正在使用带有系列[0]的图表 ChartType = points
现在我想在 Series1 中为每个现有的 X 值(日期)标记点。 Y 值应该是该特定日期系列 [0] 的所有现有 Y 值的平均值。
系列1 ChartType = Line
示例:
系列[0]分是:
X 值 = 10.07.2017 Y 值 = 10;20;30
X 值 = 11.07.2017 Y 值 = 50;60;
所以系列1 点应该是:
X 值 = 10.07.2017 Y 值 = 20(10、20、30 的平均值)
X 值 = 11.07.2017 Y 值 = 55(50,60 的平均值)
虽然 Series[0] 只显示标记点(每个标记点之间没有连接),而 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
这是一个使用 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
。
请注意,图表 statistical/financial 函数不太适合此处,因为它们适用于一个或多个系列,而是逐点计算。
我想做什么: 我有多个具有不同 CreationDates 的文件。 X 轴 = 日期,Y 轴 = 0-100 之间的值。我想创建一个折线图,显示标有 X 的每一天的所有值 -> 例如(2017 年 7 月 10 日 = 50 和 60 -> 在 2017 年 7 月 10 日显示 50 处的 X 和 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
}
我正在使用带有系列[0]的图表 ChartType = points
现在我想在 Series1 中为每个现有的 X 值(日期)标记点。 Y 值应该是该特定日期系列 [0] 的所有现有 Y 值的平均值。 系列1 ChartType = Line
示例:
系列[0]分是:
X 值 = 10.07.2017 Y 值 = 10;20;30
X 值 = 11.07.2017 Y 值 = 50;60;
所以系列1 点应该是:
X 值 = 10.07.2017 Y 值 = 20(10、20、30 的平均值)
X 值 = 11.07.2017 Y 值 = 55(50,60 的平均值)
虽然 Series[0] 只显示标记点(每个标记点之间没有连接),而 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
这是一个使用 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
。
请注意,图表 statistical/financial 函数不太适合此处,因为它们适用于一个或多个系列,而是逐点计算。