循环遍历数据表的每一行中的平均值

时间:2015-10-13 03:34:30

标签: c# asp.net if-statement charts datatable

我有一个数据表,其中包含许多带有列的数据行:CUSTOMER,MAX,MIN,AVG,25TH_PERCENTILE,50TH_PERCENTILE和75TH_PERCENTILE。我还有一个foreach循环,它将循环遍历每一行数据表,并将每个数据行数据分配到图表的每个系列中以绘制图表。

我能够在每行数据表中循环平均值。但现在我面临其他问题......

问题:如何循环添加到系列图表每个点平均? (请查看下面的代码:)

这就是我尝试过的(Foreach Loop):

                    int avg = 0;
                    int INDEX = 0;

                    foreach (DataRow row in dt.Rows)
                {
                    avg = (int)row["AVG"];

                    Chart1.Series[0].Points.AddXY(row["CUSTOMER"], new object[] { row["MIN"], row["MAX"], row["25TH_PERCENTILE"], row["75TH_PERCENTILE"], row["AVG"], row["50TH_PERCENTILE"] });

                    INDEX = Chart1.Series[1].Points.AddXY(row["CUSTOMER"], new object[] { row["AVG"] });

                    if ((INDEX >= 1) && (INDEX <= 10))
                    {
                        Chart1.Series[1].Points[INDEX].MarkerColor = Color.Green;
                    }
                    else if ((INDEX >= 11) && (INDEX <= 30))
                    {
                        Chart1.Series[1].Points[INDEX].MarkerColor = Color.Yellow;
                    }
                    else if ((INDEX >= 31) && (INDEX <= 50))
                    {
                        Chart1.Series[1].Points[INDEX].MarkerColor = Color.Red;
                    }
                }

这是我得到的输出: enter image description here

但是我使用的代码只获得每个索引而不是每个索引的平均值。有人可以帮我吗?感谢。

2 个答案:

答案 0 :(得分:1)

获取添加点的索引,如下所示:

            index = Chart1.Series[1].Points.AddXY(row["Customer"], new object[] { row["Avg"] });

然后用它来更改系列中每个平均点的标记颜色,如下所示:

                Chart1.Series[1].Points[index].MarkerColor = // some color here according to your if statement;

你应该得到这样的东西: enter image description here

编辑:这就是您的foreach循环应该是这样的:

double avg = 0;
int index = 0;

        foreach (DataRow row in dt.Rows)
        {
            avg = (double)row["Avg"];

            Chart1.Series[0].Points.AddXY(row["Customer"], new object[] { row["Min"], row["Max"], row["Percentile25"], row["Percentile75"], row["Avg"], row["Percentile50"]});
            index = Chart1.Series[1].Points.AddXY(row["Customer"], new object[] { row["Avg"] });

            if (avg >= 0 && avg <= 30)
            {
                Chart1.Series[1].Points[index].MarkerColor = Color.Green;
            }
            else if (avg > 30 && avg <= 40)
            {
                Chart1.Series[1].Points[index].MarkerColor = Color.Yellow;
            }
            else if (avg > 40 && avg <= 60)
            {
                Chart1.Series[1].Points[index].MarkerColor = Color.Red;
            }
        }

答案 1 :(得分:0)

如果我正确理解了您的问题,您可以将行数据提取到局部变量中,然后对这些变量执行逻辑。我假设数据类型,但可能是这样的:

double average; 

foreach (DataRow row in dt.Rows)
{
    average = row["AVG"];

    Chart1.Series[0].Points.AddXY(row["CUSTOMER"], new object[] { row["MIN"], row["MAX"],
    row["25TH_PERCENTILE"], row["75TH_PERCENTILE"], average, row["50TH_PERCENTILE"] });

    Chart1.Series[1].Points.AddXY(row["CUSTOMER"], new object[] { average }); 

    if(average >= 1 && average <= 5)
    {             
        Chart1.Series[1].MarkerColor = Color.Blue;
    }
    elseif(average >=6 && average <= 10)
    {
        Chart1.Series[1].MarkerColor = Color.Yellow;
    }
    else
    {
        Chart1.Series[1].MarkerColor=Color.Green;
    }
}