locked
Points coordinates of chart during move the mouse RRS feed

  • Question

  • Private Sub Chart1_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Chart1.MouseMove
                   Dim mousePoint As Point = New Point(e.X, e.Y)
                   Dim chpt As ChartPoint = ChartControl1.ChartArea.GetValueByPoint(New Point(e.X, e.Y))
                   Dim pt As Point = Chartcontrol1.ChartArea.GetPointByValue(chpt)
            Dim [text] As String = "Mouse point: " + mousePoint.ToString() + vbLf + "Result of method GetValueByPoint: {" + chpt.X.ToString() + "," + chpt.YValues(0).ToString() + "}" + vbLf + "Result of method GetPointByValue: " + pt.ToString()
                   ToolTip1.SetToolTip(Me.Chartcontrol1, text)
        End Sub

    The above three underlined words in the above code is not accepted by VB.net 2008.

    chart point not defined and chartcontrol1 not declared as well as chartcontrol1 is not a member of form1

    Can help and advice which is wrong?

    Wednesday, December 9, 2015 1:55 AM

All replies

  • I am not sure about how your code works. But, here is an example that seems to have the feature you want?

    Imports System.Windows.Forms.DataVisualization.Charting 'sample math chart with mouse down and mouse move events to show points Public Class MousePointSelect Private Sub Form6_Load(sender As Object, e As EventArgs) Handles MyBase.Load Dim MyData(12) As Single For i = 1 To 12 MyData(i) = 5 * Math.Sin(i / 2) Next

    Label1.Text = "Select a Point"

    'setup the chart With Chart1.ChartAreas(0) .AxisX.Title = "Time" .AxisX.MajorGrid.LineColor = Color.LightBlue .AxisX.Minimum = 0 .AxisX.Maximum = 14 .AxisX.Interval = 2 .AxisX.LabelStyle.IsEndLabelVisible = False .AxisY.Title = "Value" .AxisY.MajorGrid.LineColor = Color.LightGray .AxisY.Minimum = -6 .AxisY.Maximum = 6 .AxisY.Interval = 2 .BackColor = Color.AntiqueWhite .BackSecondaryColor = Color.White .BackGradientStyle = GradientStyle.HorizontalCenter .BorderColor = Color.Blue .BorderDashStyle = ChartDashStyle.Solid .BorderWidth = 1 .ShadowOffset = 2 End With Chart1.Series.Clear() Chart1.Series.Add("Monthy Data") With Chart1.Series(0) .ChartType = DataVisualization.Charting.SeriesChartType.Line .BorderWidth = 2 .Color = Color.Blue .MarkerStyle = DataVisualization.Charting.MarkerStyle.Circle .MarkerSize = 8 .IsVisibleInLegend = False For m = 1 To 12 .Points.AddXY(m, MyData(m)) Next End With End Sub Private Sub Chart1_MouseDown(sender As Object, e As MouseEventArgs) Handles Chart1.MouseDown Dim result As HitTestResult = Chart1.HitTest(e.X, e.Y) If result.ChartElementType = ChartElementType.DataPoint Then Chart1.Series(0).Points(result.PointIndex).XValue.ToString() Label1.Text = "Selected Coordinates are: " & Chart1.Series(0).Points(result.PointIndex).XValue.ToString & ", " & Chart1.Series(0).Points(result.PointIndex).YValues(0).ToString("f2") Else If result.ChartElementType <> ChartElementType.Nothing Then Dim elementType As String = result.ChartElementType.ToString() Label1.Text = "Selected Element is: " & elementType End If End If End Sub Private Sub Chart1_MouseMove(sender As Object, e As MouseEventArgs) Handles Chart1.MouseMove Dim result As HitTestResult = Chart1.HitTest(e.X, e.Y) ' If a Data Point or a Legend item is selected. If result.ChartElementType = ChartElementType.DataPoint Or result.ChartElementType = ChartElementType.LegendItem Then ' Set cursor type Me.Cursor = Cursors.Hand Else If result.ChartElementType <> ChartElementType.Nothing And result.ChartElementType <> ChartElementType.PlottingArea Then ' Set cursor type Me.Cursor = Cursors.Hand Else ' Set default cursor Me.Cursor = Cursors.Default End If End If End Sub End Class


    Wednesday, December 9, 2015 2:30 AM
  • Thank you for your help but I have not points in the chart and I need to read the coordinate of the chart area and not only the point in the curve.
    Wednesday, December 9, 2015 6:36 AM
  • Hi Hany,

    Visual Basic forum is aim to discuss and ask questions about the Visual Basic programming language, I will help you move your thread to .NET Report forum for dedicated help.

    Best Regards,
    Herro Wong


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, December 21, 2015 9:38 AM