Finding series item clicked in a chart in VB 2008

2019-07-25 13:53发布

问题:

I am using VB 2008 with the Microsoft Chart Controls for .NET Framework. Using a pie chart, I would like to find the selected item when the chart is clicked or double clicked.

I am have the click and doubleclick events as shown here, which I have confirmed is being hit, and the the eventarts contains the x,y position of the click.

Private Sub Chart_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs)
Private Sub Chart_Click(ByVal sender As Object, ByVal e As System.EventArgs)

What I would really like to find out is what series item was clicked or doubleclicked (what pie slice).

This is being done in a windows forms application.

How do I get the series item clicked or doubleclicked?

回答1:

The following gives you the chart element under the Mouse.

Dim HTR as HitTestResult
Dim SelectDataPoint As DataPoint

HTR = Chart1.HitTest(e.x,e.y)
SelectDataPoint = Chart1.Series(0).Points(HTR.PointIndex)

Note that you should probably do some checking to make sure it is a Series that the user clicks on by checking the HTR.ChartElementType. Oh, and this should go in a MouseUp event, since the e that I use are MouseEventArgs.