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
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: