uzi42tmp
uzi42tmp

Reputation: 291

Change color of certain chart points

I have a problem changing color in a chart. Following code:

chart1.Series["Test"].Color = Color.Red;
int start = array1.Length;
for (int i = 0; i < chart1.Series["Test"].Points.Count; i++)
{
      if (i >= start)
          chart1.Series["Test"].Points[i].Color = Color.Green;
      chart1.Series["Test"].Points.AddXY(x_axis[i], y_axis[i]);

}

chart1.Series["Test"].Points.DataBindXY(x_axis, y_axis);
chart1.Series["Test"].ChartType = SeriesChartType.Spline;

the case: I have two arrays - one for the x-axis and one for the y-axis. Let's say they have length 32. I have another array (not important) with length of 12. Now I want that my graph changes the color after the 12th point. How can I do that? The code above doesn't work. The graph stays in one color.

Upvotes: 0

Views: 8590

Answers (4)

przemas
przemas

Reputation: 31

In Addition if you want to highlight (red) only some data in your chart you can use something like this:

        for (int i = 0; i < 10; i++)
        {
            string test = chartLeaderBoard.Series["Active Tasks Count"].Points[i].XValue.ToString();

            if (test == selectedUser)
            {
                chartLeaderBoard.Series["Active Tasks Count"].Points[i].Color = Color.Red;
            }
            else
            {
                chartLeaderBoard.Series["Active Tasks Count"].Points[i].Color = Color.Blue;
            }            
        }

in my case string selectedUser = dataGridViewLiderBoard.Rows[currentRow].Cells[0].Value.ToString();

As the chart is based on the datagridview, where I do some additional things to get it.

Upvotes: 0

subbu
subbu

Reputation: 21

you can change the color for some data points by repeating loop for each data point after binding your data.

Points pts = (Microsoft.Office.Interop.Excel.Points)series.Points(Type.Missing);
 foreach (Point pt in pts) { if(yourcondition) {
    pt.Border.Color = (int)XlRgbColor.rgbDarkOrange;
    pt.MarkerBackgroundColor = (int)XlRgbColor.rgbDarkOrange;
    pt.MarkerForegroundColor = (int)XlRgbColor.rgbDarkOrange;
  }
 }

Upvotes: 0

uzi42tmp
uzi42tmp

Reputation: 291

Solved! I had to loop:

            for (int i = 0; i < array1.Length; i++)
                {
                    chart1.Series["Test"].Points[i].Color = Color.Blue;

                }

the chart had to be bound first.

Upvotes: 1

Fratyx
Fratyx

Reputation: 5797

I don't know how many points are in your series while executing the loop and where they come from but by the DataBindXY method all the previous datapoints are removed and the new bound datapoints are created in automatic color (As I understand the working of DataBind)

Maybe you should try looping over the datapoints after the DataBindXY and "post-color" the points.

Upvotes: 0

Related Questions