如何更改图表系列颜色 [英] How to change chart series color

查看:295
本文介绍了如何更改图表系列颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个系列,其中用数据库中的记录填充图表.显示了七个记录,但颜色鲜亮.

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

点击按钮后的图表:

推荐答案

您正在尝试在将点添加到系列中之前更改其颜色.将下面的块移出while循环,并在尝试按索引访问之前检查序列中是否存在足够的点,

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;
}

这篇关于如何更改图表系列颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆