我有一个系列,用数据库中的记录填充我的图表。已经显示了七个记录,但是保留了颜色。
我试图改变每个条形颜色而没有成功
下面是我试过的线路,但我给了我一个大绿条(:
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);
}
}
这是我运行后收到的错误消息: 索引位于有效范围之外,索引不能为负,且必须低于列表的大小
按下按钮后的图表:
答案 0 :(得分:5)
您尝试在系列中添加点之前更改点的颜色。将下面的块移出while循环,并在尝试按索引访问之前检查是否有足够的点存在,
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;
以下是您需要在代码中进行的更改
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;
}