9

I have a single series which populates my chart with records from a database. Seven records are been displayed but in the sanme color.

I' trying to change each bar color without success

winforms bar chart

Below are the lines i tried but i gave me one big green bar (:

        private void button1_Click(object sender, EventArgs e)
    {
        /*First empty the chart2 to fire the current data*/
        if (cbChart.SelectedItem == null)
        {
            chart.Visible = false;
            chart.Controls.Clear();
        }
        else
            //pnchart.Controls.Clear();
        chart.Visible = true;
        chart.Titles.Clear();


        /*Add a new title*/
        Title bezeichung = new Title("Finance" + Environment.NewLine + "(GWU)", Docking.Top, new Font("Yu Gothic", 8, FontStyle.Bold), Color.Black);
        chart.Titles.Add(bezeichung);          
        chart.Titles.Add(bezeichung2);



         if (cbChart.SelectedItem != null)
        {
      string S =    ConfigurationManager.ConnectionStrings[""].ConnectionString;
      SqlConnection con = new SqlConnection(S);
      SqlCommand cmd = new SqlCommand();
      cmd.Connection = con;
      cmd.CommandType = CommandType.StoredProcedure;
      cmd.CommandText = ("[dbo].[storedprocedure]");
      cmd.Parameters.AddWithValue("@Table_Name", cbChart.SelectedValue.ToString());
      SqlDataReader myReader;  // DataReader to just read Data from the Datbase

            try
            {
                //DO SOMETHING
                con.Open();
                myReader = cmd.ExecuteReader();

                while (myReader.Read())
                {

                   //Parameters (Seriesname, x-axis data & y-axis data)
                    this.chart.Series["Series"].Points.AddXY(myReader["Finance"], myReader["GWU"]);

                    // remove grid lines
                    chart.ChartAreas[0].AxisX.MajorGrid.LineWidth = 0;
                    chart.ChartAreas[0].AxisY.MajorGrid.LineWidth = 0;
                    chart.ChartAreas[0].AxisX.LabelStyle.Angle = -45;

      chart.Series["series1"].Points[0].Color = Color.Green;
      chart.Series["series1"].Points[1].Color = Color.Red;
      chart.Series["series1"].Points[2].Color = Color.PowderBlue;
      chart.Series["series1"].Points[3].Color = Color.Peru;
      chart.Series["series1"].Points[4].Color = Color.Pink;
      chart.Series["series1"].Points[5].Color = Color.Purple;
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                con.Close();
            }
        }

         else
         {

MessageBox.Show("Bitte ", "Info", MessageBoxButtons.OK, MessageBoxIcon.Warning);
         }

    }

That’s the error message I received after running it: The index lies outside the valid range, index must not be negative and must be lower than the size of the list

And the chart after hitting the button:

enter image description here

mikybrain
  • 169
  • 1
  • 4
  • 14
  • What Type is your chart object? – Zack Mar 27 '15 at 14:34
  • A button object. if the btn is clicked, it connects to the database and the popultaes the chart. – mikybrain Mar 27 '15 at 14:39
  • Your code should work fine. Do you run it each time after the data points have been added? Is the `ChartType = Column`? – TaW Mar 27 '15 at 16:44
  • A Button does not have a Series property, so it is obviously not a Button. If you navigate to the definition of `chart` what class is it an instance of, is what I'm asking... – Zack Mar 27 '15 at 17:07
  • @Zack: What do you think it is? It quite obviously is a `Chart` control. – TaW Mar 27 '15 at 21:22
  • Hi I' ve edited my code. I droped the chart from the Tools panel into the form. Took a btn and the applied my code above. I hope this can help solve my problem if it is possible – mikybrain Mar 27 '15 at 22:55

1 Answers1

12

You are trying to change the color of points before they are added in the series. Move the below block out of the while loop and check if enough points exists in series before you try to access by index,

  chart.Series["series1"].Points[0].Color = Color.Green;
  chart.Series["series1"].Points[1].Color = Color.Red;
  chart.Series["series1"].Points[2].Color = Color.PowderBlue;
  chart.Series["series1"].Points[3].Color = Color.Peru;
  chart.Series["series1"].Points[4].Color = Color.Pink;
  chart.Series["series1"].Points[5].Color = Color.Purple;

Below are the changes you need to make in code,

while (myReader.Read())
{
//Parameters (Seriesname, x-axis data & y-axis data)
this.chart.Series["Series"].Points.AddXY(myReader["Finance"], myReader["GWU"]);

}

if(chart.ChartAreas.Count > 0)
{
chart.ChartAreas[0].AxisX.MajorGrid.LineWidth = 0;
chart.ChartAreas[0].AxisY.MajorGrid.LineWidth = 0;
chart.ChartAreas[0].AxisX.LabelStyle.Angle = -45;
}


if(chart.Series["series1"].Points.Count > 5)
{
chart.Series["series1"].Points[0].Color = Color.Green;
chart.Series["series1"].Points[1].Color = Color.Red;
chart.Series["series1"].Points[2].Color = Color.PowderBlue;
chart.Series["series1"].Points[3].Color = Color.Peru;
chart.Series["series1"].Points[4].Color = Color.Pink;
chart.Series["series1"].Points[5].Color = Color.Purple;
}
ASN
  • 588
  • 4
  • 15