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:
To change the color of a data series: Select the data series you wish to edit. Click the Format button on the Chart toolbar (or double-click the data series). Use the Format Data Series dialog box to pick a new color. Click the OK button to accept the Data Series color changes.
On the Design tab, in the Type group, click Change Chart Type. In the Change Chart Type dialog box, click a chart type that you want to use. The first box shows a list of chart type categories, and the second box shows the available chart types for each chart type category.
Click the chart you want to change. In the upper-right corner, next to the chart, click Chart Styles. Click Color and pick the color scheme you want, or click Style and pick the option you want.
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With