Change the color randomly for each column in a Chart c #

0

Good morning, I have a program that allows me to graph by connecting to a database. But, I would like it to change color automatically for each column, since it only allows me to keep a color (blue) in the graphics area.

This is the method that allows me to generate my graphics.

protected void GetData (string strmes)

    {

        DataSet resultado = new DataSet();            

        clsConexioncs conexion_server = new clsConexioncs();

        conexion_server.Conexion = ConfigurationManager.ConnectionStrings["DefaultDW"].ToString();           


        //conexion_server.PreparaComandoSP("sp_telemtria_femsa");
        conexion_server.PreparaComandoSP("sp_femsa_telemetria_mensual");

        conexion_server.AgregarParametro("Mes", SqlDbType.VarChar, strmes);


        resultado = conexion_server.EjecutaComandoDataSet();


        Graficas_Semanal.Titles.Add("No. Camiones Promedio Mes Actual");
        Graficas_Semanal.ChartAreas["ChartArea"].AxisX.MajorGrid.Enabled = false;
        Graficas_Semanal.ChartAreas["ChartArea"].AxisY.MajorGrid.Enabled = false;




        Grafica_Dona.Titles.Add("Mes Actual");


        /*
        Graficas_Semanal.Series["Series"].Points.AddXY("Mongue", 125);*/

        foreach (DataRow row in resultado.Tables[0].Rows)
        {



            Graficas_Semanal.Series["Series"].Points.AddXY(row["CeEmplazamiento"], row["Totales"]);

            Grafica_Dona.Series["Series_Dona"].Points.AddXY(row["CeEmplazamiento"], row["Totales"]);

        }

        //Graficas_Semanal.Series["Series"].Points.DataBindXY(resultado.Tables[0],RowNotInTableException[);
        //Graficas_Semanal.Series["Series"].Points.AddXY(nombs, barras);

    }
    
asked by Ric_hc 10.03.2017 в 17:06
source

1 answer

1

To color a specific column we can do it in the following way:

miGrafica.Series[miSerie].Points[numColumna].Color = Color.Black;

For example, if you want to color the first column of the series Series of your graph Grafica_Semanal1 would be:

Graficas_Semanal.Series["Series"].Points[0].Color = Color.Red;

Since it is a vector of points, the first point is 0, you can choose which one to color.

If you want to color it by adding the point to the graph, you can use the Count property of the point vector:

Graficas_Semanal.Series["Series"].Points.Count;

That will give you the number of points you have on your graph, just remember to subtract one at the time of use.

If you want the color to be random you can use Random to generate a random value between 0 and 255, and pass it as a parameter to the function.

Color.FromArgb(r,g,b);

Then the foreach would be like this:

//Añadimos una variable de tipo Random para generar el color aleatorio.
Random r = new Random();

foreach (DataRow row in resultado.Tables[0].Rows)
{
    Graficas_Semanal.Series["Series"].Points.AddXY(row["CeEmplazamiento"],row["Totales"]);
    Grafica_Dona.Series["Series_Dona"].Points.AddXY(row["CeEmplazamiento"],row["Totales"]);
    //Calcula la cantidad de puntos añadido y al último añadido darle el color
    //Graficas_Semanal.Series["Series"].Points.Count-1

    //Asigna el color al último punto añadido a la serie "Series"
    // de la gráfica Graficas_Semanal
    Graficas_Semanal
        .Series["Series"]
        .Points[Graficas_Semanal
        .Series["Series"].Points.Count-1]
        .Color = Color.FromArgb(r.Next(0, 255), r.Next(0, 255), r.Next(0, 255));

    //Asigna el color al último punto añadido a la serie "Series"
    // de la grafica Graficas_Semanal
    Grafica_Dona
       .Series["Series_Dona"]
       .Points[Grafica_Dona
       .Series["Series"]
       .Points.Count]
       .Color = Color.FromArgb(r.Next(0, 255), r.Next(0, 255), r.Next(0, 255));
}
    
answered by 11.03.2017 / 11:08
source