Felicia Soh
Felicia Soh

Reputation: 865

Loop Through Average Value in Each Row of Datatable

I have a datatable which consists of many datarows with columns: CUSTOMER, MAX, MIN, AVG, 25TH_PERCENTILE, 50TH_PERCENTILE AND 75TH_PERCENTILE. I also have a foreach loop which will loop through each row of datatable and assign each datarow data into each series of the chart to plot the chart.

I am able to loop through average value in each row of datatable. But now I faced with other issues..

Question: How can I loop through the average value of each point being added to the series of the chart? (Please look at my code below: )

This is what I have tried (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;
                    }
                }

This is the output I get: enter image description here

But the codes I used only get each index instead of average value of each index. Can someone help me on this? Thanks.

Upvotes: 1

Views: 896

Answers (2)

jsanalytics
jsanalytics

Reputation: 13188

Get the index of the added point like this:

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

and then use it to change the marker color for each average point in your series like this:

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

You should get something like this: enter image description here

EDIT: This is what your foreach loop should look like:

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

Upvotes: 1

NeddySpaghetti
NeddySpaghetti

Reputation: 13495

If I understand your question correctly, you can extract the row data into local variables and then perform the logic on those. I am assuming the data types, but perhaps something like this:

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

Upvotes: 0

Related Questions