Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you view the value of a chart point on mouse over?

Tags:

vb.net

charts

I have various points on a chart. I would like to be able to show the exact value of the point in a tooltip on mousing over that particular point.

Example:

Chart1.Series("Series1").Points.AddXY("Jul", 600)
Chart1.Series("Series1").Points.AddXY("aug", 458)

On mousing over these points on the chart, the tooltip text should show "600" or "458".

Edit:

This gets me close but it only shows the value of the mouse position on the point, not the full value of the point:

 Private Sub Chart1_GetToolTipText(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataVisualization.Charting.ToolTipEventArgs) Handles Chart1.GetToolTipText
    If e.HitTestResult.PointIndex >= 0 Then
        If e.HitTestResult.ChartElementType = DataVisualization.Charting.ChartElementType.DataPoint Then
            MetroToolTip1.SetToolTip(Chart1, e.y.tostring)
        End If
    End If
End Sub
like image 915
Zach Johnson Avatar asked Aug 26 '16 02:08

Zach Johnson


2 Answers

I realize you found a solution, but the simplest way is to set the 'Series.ToolTip' property.

Chart1.Series(0).ToolTip = "#VAL{0.0}"

The ToolTip makes use of keywords to define what value to display followed by an optional format specifier that follows the MS Custom Numeric Format Strings for the most part. The easiest way to find these keywords is to use the editor exposed in the PropertyGrid to set the ToolTip.

In this example the #VAL tells it to display the y-value. The {0.0} tells it to format the number using the "0.0" format string.

Edit: I found a table on the Dundas site (MS bought the control from them), that lists the Keywords and explains more about the format specifier usage. http://support2.dundas.com/Default.aspx?article=1132

Keywords documentation from MSDN: Keywords [rs_vsDataVis]

like image 136
TnTinMn Avatar answered Sep 22 '22 14:09

TnTinMn


This is the code needed to show the point values:

Private Sub chart1_GetToolTipText(sender As Object, e As ToolTipEventArgs) Handles Chart1.GetToolTipText
    ' Check selected chart element and set tooltip text for it
    Select Case e.HitTestResult.ChartElementType
        Case ChartElementType.DataPoint
            Dim dataPoint = e.HitTestResult.Series.Points(e.HitTestResult.PointIndex)
            e.Text = dataPoint.YValues(0).ToString
            Exit Select
    End Select
End Sub
like image 33
Zach Johnson Avatar answered Sep 25 '22 14:09

Zach Johnson