Click here to Skip to main content
15,883,749 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi all,

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 my code:
C#
int avg=0;         

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

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


                            if (!String.IsNullOrEmpty(row["AVG"].ToString()))
                            {
                                avg = Convert.ToInt32(row["AVG"].ToString());
                            }

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

This is the output I get currently:
http://i.stack.imgur.com/BDGcX.png[^]

Appreciate if someone can help me on this, thanks!
Posted
Updated 12-Oct-15 21:08pm
v2

C#
if(int.Parse(row[Avg].ToString()) >=1 && int.Parse(row[Avg].ToString()) >=5)
{             
    Chart1.Series[1].MarkerColor = Color.Blue;
}
else if(int.Parse(row[Avg].ToString()) >=6 && int.Parse(row[Avg].ToString()) >=10)
{
    Chart1.Series[1].MarkerColor = Color.Yellow;
}
else
{
    Chart1.Series[1].MarkerColor=Color.Green;
}

Like this ?

-KR
 
Share this answer
 
Comments
Member 11999641 13-Oct-15 3:06am    
hi KR, thanks for your help! :) But I encountered some issues here.. your solution is for the whole series of the chart, how can I loop through the average value of each point being added to the series of the chart?

For example: Chart1.Series[1].Points.AddXY(row["CUSTOMER"], new object[] { row["Avg"] }); , I want to check what are the average value for each point added on the series, I want to loop through the average value for each point added on the series of the chart. Let's say there are 3 points on the series of the chart, I want to loop through the average value of each point on the series of the chart. Please take a look at my updated post, thanks!
Since you are already inside the foreach loop, you can do something like:

C#
var average = (int)row["Avg"];
if ((average >= 1) && (average <= 5))
{
	Chart1.Series[1].MarkerColor = Color.Blue;
}
else if ((average >= 6) && (average <= 10))
{
	Chart1.Series[1].MarkerColor = Color.Yellow;
}
else
{
	Chart1.Series[1].MarkerColor = Color.Green;
}
 
Share this answer
 
Comments
Member 11999641 13-Oct-15 3:06am    
hi Palash Mondal_, thanks for your help! :) But I encountered some issues here.. your solution is for the whole series of the chart, how can I loop through the average value of each point being added to the series of the chart?

For example: Chart1.Series[1].Points.AddXY(row["CUSTOMER"], new object[] { row["Avg"] }); , I want to check what are the average value for each point added on the series, I want to loop through the average value for each point added on the series of the chart. Let's say there are 3 points on the series of the chart, I want to loop through the average value of each point on the series of the chart. Please take a look at my updated post, thanks!
may help you:
C#
foreach (DataRow row in dt.Rows)
    {
       Chart1.Series[0].Points.AddXY(row["CUSTOMER"], new object[] { row["MIN"], row["MAX"],
       row["25TH_PERCENTILE"], row["75TH_PERCENTILE"], row["AVG"], row["50TH_PERCENTILE"] });

       Chart1.Series[1].Points.AddXY(row["CUSTOMER"], new object[] { row["Avg"] });
       int avg = 0;
       if(!String.IsNullOrEmpty(row["Avg"].ToString()))
         {
             avg = Covert.ToInt16(row["Avg"].ToString());
         }

       if(avg >=1 && avg <=5)
          {
            Chart1.Series[1].MarkerColor = Color.Blue;
          }
       else if(avg >=6 && avg <=10)
          {
            Chart1.Series[1].MarkerColor = Color.Yellow;
          }
        else
          {
            Chart1.Series[1].MarkerColor=Color.Green;
          }
    }
 
Share this answer
 
Comments
Member 11999641 13-Oct-15 3:07am    
hi Rojalin Sahoo, thanks for your help! :) But I encountered some issues here.. your solution is for the whole series of the chart, how can I loop through the average value of each point being added to the series of the chart?

For example: Chart1.Series[1].Points.AddXY(row["CUSTOMER"], new object[] { row["Avg"] }); , I want to check what are the average value for each point added on the series, I want to loop through the average value for each point added on the series of the chart. Let's say there are 3 points on the series of the chart, I want to loop through the average value of each point on the series of the chart. Please take a look at my updated post, thanks!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900