如何为 MSChart 创建多种颜色/系列

How to create multiple colors / series for a MSChart

我有一个 MYSQL 数据库,我已将它链接到我的 .Net 应用程序中的图表。 但是,我有 5 个 x 值要显示(所以有 5 个条),但它们都是相同的颜色,我更希望它们各有不同的颜色。

我想我必须用系列来做,但我不知道怎么做..?

代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace Project_3
{
    public partial class Vince : Form
    {
        public Vince()
        {
            InitializeComponent();
            this.WindowState = FormWindowState.Maximized;
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string query = "";
            string query2 = "";
            if (comboBox1.Text == "Centrum")
            {
                query = "SELECT Aantal FROM Centrum";
                query2 = "Select Aantal FROM Verhuizingen WHERE wijk = 'Centrum'";
                pictureBox1.Visible = true;
                label3.Text = "Inwoners: 4456";
                label4.Text = "Per inwoner vinden er 0,13 misdrijven en 0,08 verhuizingen plaats";
            }
            else if (comboBox1.Text == "Groenoord")
            {
                query = "SELECT Aantal FROM Groenoord";
                query2 = "Select Aantal FROM Verhuizingen WHERE wijk = 'Groenoord'";
                pictureBox1.Visible = true;
                label3.Text = "Inwoners: 9035";
                label4.Text = "Per inwoner vinden er 0,04 misdrijven en 0,06 verhuizingen plaats";
            }

            else if (comboBox1.Text == "Kethel")
            {
                query = "SELECT Aantal FROM Kethel";
                query2 = "Select Aantal FROM Verhuizingen WHERE wijk = 'Kethel'";
                pictureBox1.Visible = true;
                label3.Text = "Inwoners: 3704";
                label4.Text = "Per inwoner vinden er 0,20 misdrijven en 0,04 verhuizingen plaats";
            }
            else if (comboBox1.Text == "Nieuwland")
            {
                query = "SELECT Aantal FROM Nieuwland";
                query2 = "Select Aantal FROM Verhuizingen WHERE wijk = 'Nieuwland'";
                pictureBox1.Visible = true;
                label3.Text = "Inwoners: 14076";
                label4.Text = "Per inwoner vinden er 0,04 misdrijven en 0,08 verhuizingen plaats";
            }

            else if (comboBox1.Text == "Oost")
            {
                query = "SELECT Aantal FROM Oost";
                query2 = "Select Aantal FROM Verhuizingen WHERE wijk = 'Oost'";
                pictureBox1.Visible = true;
                label3.Text = "Inwoners: 11286";
                label4.Text = "Per inwoner vinden er 0,10 misdrijven en 0,11 verhuizingen plaats";
            }
            else if (comboBox1.Text == "Spaland")
            {
                query = "SELECT Aantal FROM Spaland";
                query2 = "Select Aantal FROM Verhuizingen WHERE wijk = 'Spaland'";
                pictureBox1.Visible = true;
                label3.Text = "Inwoners: 8419";
                label4.Text = "Per inwoner vinden er 0,02 misdrijven en 0,03 verhuizingen plaats";
            }
            else if (comboBox1.Text == "West")
            {
                query = "SELECT Aantal FROM West";
                query2 = "Select Aantal FROM Verhuizingen WHERE wijk = 'West'";
                pictureBox1.Visible = true;
                label3.Text = "Inwoners: 4456";
                label4.Text = "Per inwoner vinden er 0,12 misdrijven en 0,21 verhuizingen plaats";
            }
            else if (comboBox1.Text == "Woudhoek")
            {
                query = "SELECT Aantal FROM Woudhoek";
                query2 = "Select Aantal FROM Verhuizingen WHERE wijk = 'Woudhoek'";
                pictureBox1.Visible = true;
                label3.Text = "Inwoners: 12116";
                label4.Text = "Per inwoner vinden er 0,02 misdrijven en 0,02 verhuizingen plaats";
            }
            else if (comboBox1.Text == "Zuid")
            {
                query = "SELECT Aantal FROM Zuid";
                query2 = "Select Aantal FROM Verhuizingen WHERE wijk = 'Zuid'";
                pictureBox1.Visible = true;
                label3.Text = "Inwoners: 5929";
                label4.Text = "Per inwoner vinden er 0,05 misdrijven en 0,06 verhuizingen plaats";
            }
            string connectionString = "datasource=127.0.0.1;port=3306;username=root;password=;database=project3";
            MySqlConnection databaseConnection = new MySqlConnection(connectionString);
            MySqlCommand dbquery = new MySqlCommand(query, databaseConnection);
            MySqlDataReader reader;
            List<int> yValues = new List<int>();
            try
            {
                databaseConnection.Open();
                reader = dbquery.ExecuteReader();
                if (reader.HasRows)
                {
                    int count = reader.FieldCount;
                    while (reader.Read())
                    {

                        //List<string> xValues = new List<string>();

                        yValues.Add(Int32.Parse(reader.GetString(0)));
                        //xValues.Add(reader.GetName(i));



                    }


                }
                databaseConnection.Close();
                MySqlCommand dbquery2 = new MySqlCommand(query2, databaseConnection);
                MySqlDataReader reader2;
                databaseConnection.Open();
                reader2 = dbquery2.ExecuteReader();
                if (reader2.HasRows)
                {
                    int count = reader2.FieldCount;
                    while (reader2.Read())
                    {

                        //List<string> xValues = new List<string>();

                        yValues.Add(Int32.Parse(reader2.GetString(0)));
                        //xValues.Add(reader.GetName(i));



                    }


                }
                databaseConnection.Close();
                string[] xValues = { "Diefstal ", "Geweld ", "Inbraak", "Vernieling", "Verhuizingen" };
                chart1.Series[0].Points.DataBindXY(xValues, yValues);


            }


            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }

        private void chart1_Click(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void Vince_Load(object sender, EventArgs e)
        {

        }

        private void label2_Click_1(object sender, EventArgs e)
        {

        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {

        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void label4_Click(object sender, EventArgs e)
        {

        }
    }
}

您不需要而且很可能也不想要多个系列。

你不想要它的原因是它让事情在不应该的时候变得复杂。

另一个是,不同系列中的列将放置在同一个 x-values 上,以便它们可以并排放置。如果所有人都有不同的 x-values 位置将会漂移。

而是简单地为每个数据点单独着色。

将它们一一添加时,您可以直接添加。

使用 DataBinding,您需要在绑定发生后执行此操作..:[=​​13=]

for (int i = 0; i < chart1.Series[0].Points.Count; i++)
{
    chart1.Series[0].Points[i].Color = colors[i];
}

假设您有合适的颜色列表或数组:

Color[] colors = { Color.Green, Color.Blue, Color.Wheat, Color.AliceBlue, Color.Teal};

注意边几类DataBinding a Chart do support extended properties, most don't and even those that do, do not support binding Colors. See 进行讨论..!