VB.Net Charts


I am using the chart control in Microsoft Visual Basic studio 2010 express.

I have a pie chart that shows the amount of records per name. The chart is connected to a dataset datatable that is populated by a database query. The datatable contains 3 columns; Name, ID, TotalRecords.

On my pie chart I am showing the xValue as name (so that the labels are readable and not ust a ID) and yValue as TotalRecords. Is there anywhere I can store the ID for later use against the series?

The reason I ask is because I would like to use this ID later to populate the parameters of a database query when the use clicks on particular section of the pie chart...
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.

Robert SchuttSoftware EngineerCommented:
Here is one way to do it. Instead of setting DataSource and member fields (see commented out code) you use Points.DataBind() to indicate the extra field which can be accessed as a property on the Points objects.
Imports System.Windows.Forms.DataVisualization.Charting

Public Class Form1

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Dim dt As New DataTable
        dt.Columns.AddRange({New DataColumn("Name", GetType(String)), New DataColumn("ID", GetType(Integer)), New DataColumn("TotalRecords", GetType(Integer))})
        dt.Rows.Add({"x", 1, 3})
        dt.Rows.Add({"y", 2, 4})
        dt.Rows.Add({"z", 3, 5})
        'Chart1.DataSource = dt
        'Chart1.Series(0).XValueMember = "Name"
        'Chart1.Series(0).YValueMembers = "TotalRecords"
        Chart1.Series(0).Points.DataBind(dt.Rows, "Name", "TotalRecords", "ID=ID") ' note last argument: you could define an alias for the ID field
        Chart1.Series(0).ChartType = DataVisualization.Charting.SeriesChartType.Pie
    End Sub

    Private Sub Chart1_MouseClick(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Chart1.MouseClick
        Dim htr As HitTestResult = Chart1.HitTest(e.X, e.Y)
        If htr.ChartElementType = ChartElementType.DataPoint Or htr.ChartElementType = ChartElementType.DataPointLabel Then
            Dim p As DataPoint = htr.Object
            MsgBox(p("ID")) ' access the ID of the clicked pie part
        ElseIf htr.ChartElementType = ChartElementType.LegendItem Then
            Dim li As LegendItem = htr.Object
            Dim p As DataPoint = Chart1.Series(li.SeriesName).Points(li.SeriesPointIndex)
        End If
    End Sub
End Class

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:
Thank you that works great!

One more question... Is it possible to use all three data fields in the tooltip for the series and also adding addition writting.

E.g. Tooltip as follow

ID = 1
Name = Ted
Total = 100
Robert SchuttSoftware EngineerCommented:
sure, try this:
Chart1.Series(0).ToolTip = "ID: #CUSTOMPROPERTY(ID)\nName: #VALX\nTotal: #VALY"

Open in new window

Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

spen_langAuthor Commented:
Is there any way to format the tooltip custom property, e.g. UCase..?
Robert SchuttSoftware EngineerCommented:
Hmm, I don't think so, no function calls possible from that string AFAIK. Would be easier to do it in the sql, add a new field if neccessary.
spen_langAuthor Commented:
Ok, thanks for your help.

If you have time could you take a look at another question I have posted on .NET charts?

Robert SchuttSoftware EngineerCommented:
I had a quick look, but seems you almost have a solution there now.

Good to see you're already using the techniques learned from this solution! (if I may be so bold ;-)
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

From novice to tech pro — start learning today.