VB NET Pie Chart Segment Colours


Is it possible to set the colour of each segment of a oie chart in VB NET using the Chart control?

For example I have a pie chart that counts the names in a table, I would like Ted to be red and Fred to be green etc...

I have found out how to do it by index number:
series1.Points(0).Color = Color.Blue

but need something like this
series1.Points("Fred").Color = Color.Green
series1.Points("Ted").Color = Color.Red

Thanks, Greg
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Éric MoreauSenior .Net ConsultantCommented:
You can use LINQ to find your point:

Dim pt = .Series("Canadian provinces").Points.FirstOrDefault(Function(p) p.AxisLabel = "Fred")
If pt IsNot Nothing Then pt.Color = Color.Green

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
spen_langAuthor Commented:
Thanks for your reply. I have just managed to write some code that gives me the desired results.

The colour is returned within the dataset that is bound to the chart series. I have used...

series1.Points.DataBind(ds.Tables(0).Rows, "Name", "Value", "NameColour=NameColour")

Dim iRandom As New Random

For Each dp As DataPoint In series1.Points

	If Not String.IsNullOrEmpty(dp("NameColour").ToString) Then
		dp.Color = System.Drawing.ColorTranslator.FromHtml(dp("NameColour").ToString)
		dp.Color = Color.FromArgb(255, iRandom.Next(0, 255), iRandom.Next(0, 255), iRandom.Next(0, 255))
	End If


Open in new window

Éric MoreauSenior .Net ConsultantCommented:
I have provided a working solution using the details you have provided!
spen_langAuthor Commented:
Thank you
spen_langAuthor Commented:
I've requested that this question be closed as follows:

Accepted answer: 500 points for Éric Moreau's comment #a40922810
Assisted answer: 0 points for spen_lang's comment #a40922832

for the following reason:

This is what I ended up using.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.