asp.net column chart coloring of each column in c#

与世无争的帅哥 提交于 2019-12-25 05:06:50

问题


i m using c# for first time in a asp.net website, in which i am creating charts. i have created a column chart- 2D, and the code goes like this,

protected void chrtTickets_Load(object sender, EventArgs e)
        {
            using (SqlConnection connection = new SqlConnection(ConnectionString))
            {

                connection.Open();


                SqlCommand cmmd6 = new SqlCommand("getHowTicketsLoggedChart", connection);
                cmmd6.CommandType = CommandType.StoredProcedure;


                DataSet ds1 = new DataSet();

                SqlDataAdapter dad = new SqlDataAdapter(cmmd6);

                dad.Fill(ds1);

                chrtTickets.DataSource = ds1;


                chrtTickets.Series["Series1"].XValueMember = "MonYYYY";

                chrtTickets.Series["Series1"].YValueMembers = "aa";


                chrtTickets.DataBind();
                connection.Close();


            }
        }

but i have the stored procedure which returns me another field "Description" along with MonYYYy and aa as they are used in xaxis and yaxis.

but according to the "description" field value say: email, phone, portal. i want to assign a different color to each column depending upon each row's description value, if its Email-> red and so on etc.

can anyone please guide me through this?

what i mean is my data will be be like this: so i will not know what will be color type value in advance, this is the table values returned to me from db by my stored procedure.

color type| y-axiz value | x-axis


Email Connector| 24| Dec 2011 Phone | 32 | Dec 2011 Email Connector |643 | Jan 2012 Internal | 32 | Jan 2012 Phone |455| Jan 2012 Portal |2 | Jan 2012 Sales Order| 2| Jan 2012

it cold be one of the 5 types ex. email, portal but many x-axis value.

datapoints will not be known. they are dynamic.


回答1:


you can try this it should randomly chose a color for the next bar

Color[] colors = new Color[] { Color.Red, Color.Green, Color.Wheat, Color.Gray. Color.Black,  Color.Blue};
foreach (Series series in chrtTickets.Series)
{
    foreach (DataPoint point in series.Points)
    {
        Random random = new Random();
        int randomNumber = random.Next(0, 5);
        point.LabelBackColor = colors[ randomNumber];
    }
}

here is main article http://forums.asp.net/t/1652369.aspx/1



来源:https://stackoverflow.com/questions/10714387/asp-net-column-chart-coloring-of-each-column-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!