vb.net 如何在鼠标悬停时查看图表点的值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39157387/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 20:02:20  来源:igfitidea点击:

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

vb.netcharts

提问by Zach Johnson

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".

将鼠标悬停在图表上的这些点上时,工具提示文本应显示“600”或“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

回答by TnTinMn

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

我意识到您找到了解决方案,但最简单的方法是设置“Series.ToolTip”属性。

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

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

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

工具提示使用关键字来定义要显示的值,后跟一个可选的格式说明符,该说明符大部分遵循 MS自定义数字格式字符串。找到这些关键字的最简单方法是使用在 PropertyGrid 中公开的编辑器来设置 ToolTip。

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

在这个例子中,#VAL告诉它显示 y 值。该{0.0}告诉它格式化使用“0.0”格式字符串的数量。

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

编辑:我在 Dundas 网站上找到了一张表格(MS 从他们那里购买了控件),其中列出了关键字并解释了有关格式说明符用法的更多信息。 http://support2.dundas.com/Default.aspx?article=1132

Keywords documentation from MSDN: Keywords [rs_vsDataVis]

来自 MSDN 的关键字文档:关键字 [rs_vsDataVis]

回答by Zach Johnson

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