Bar Chart random color in asp.Net


Display bars in random colors:

IDictionary<string, int> list= new Dictionary<string, int>();
list.Add(“Val 1”, 90);
list.Add(“Val  2”, 89);
list.Add(“Val 3”, 50);
list.Add(“Val 5”, 87);

chrtBar2.Series[“Categories”].XValueMember = “key”;
chrtBar2.Series[“Categories”].YValueMembers = “value”;
chrtBar2.ChartAreas[“MainChartArea”].AxisX.MajorGrid.Enabled = false;
chrtBar2.ChartAreas[“MainChartArea”].AxisX.MajorTickMark.Enabled = true;
chrtBar2.ChartAreas[“MainChartArea”].AxisX.MinorGrid.Enabled = false;
chrtBar2.ChartAreas[“MainChartArea”].AxisX.MinorTickMark.Enabled = false;
chrtBar2.ChartAreas[“MainChartArea”].AxisY.MajorGrid.Enabled = false;
chrtBar2.ChartAreas[“MainChartArea”].AxisY.MajorTickMark.Enabled = true;
chrtBar2.ChartAreas[“MainChartArea”].AxisY.MinorGrid.Enabled = false;
chrtBar2.ChartAreas[“MainChartArea”].AxisY.MinorTickMark.Enabled = false;
chrtBar2.Series[“Categories”][“PointWidth”] = “1.0”;
chrtBar2.Series[“Categories”].BorderWidth = 1;
chrtBar2 .Series[“Categories”].BorderColor = Color.White;
chrtBar2.DataSource = list;
chrtBar2.DataBind();

//Set random color
Random random = new Random();foreach (var item in chrtBar2.Series[“Categories”].Points)

{
Color c = Color.FromArgb(random.Next(0, 255), random.Next(0, 255), random.Next(0, 255));
item.Color = c;
}

Each time will change the bar colors  when page will refreshed.

Advertisements
Tagged with: , , ,
Posted in .Net, Asp.Net, C#

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Top Rated

Error: Twitter did not respond. Please wait a few minutes and refresh this page.

Blog Stats
  • 29,596 hits
%d bloggers like this: