Reputation: 13
I have a MYSQL Database and I've got it linked to a chart in my .Net application. However, I have 5 x values to display (so 5 bars) but they all are the same color, and I'd prefer to have them each a different color.
I believe I have to do it with series but I have no clue how..?
Code:
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)
{
}
}
}
Upvotes: 1
Views: 147
Reputation: 54433
You don't need and most likely also don't want multiple series.
On reason why you don't want it is that it makes matters complicated when they shouldn't be.
Another is, that the columns in different series will be positioned so that they can sit beside each other on the same x-values. If all have different x-values the positions will drift.
Instead simply color each DataPoint individually.
When adding them one by one you can do it right along.
With DataBinding you need to do it after the binding has happend..:
for (int i = 0; i < chart1.Series[0].Points.Count; i++)
{
chart1.Series[0].Points[i].Color = colors[i];
}
This assumes that you have a suitable List or array of colors:
Color[] colors = { Color.Green, Color.Blue, Color.Wheat, Color.AliceBlue, Color.Teal};
Note that while a few types of DataBinding a Chart do support extended properties, most don't and even those that do, do not support binding Colors. See here for a discussion..!
Upvotes: 1