Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to display the tooltip or just the value but it is not showing when I tried someone else's code in displaying the value of the hovered datapoint.

What I have tried:

I tried to set the tooltip in chartseries to #VAL{C}, but to no avail. I also input this in my formload, but it is still not working.

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


I tried this code as well:

VB
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 
Posted
Updated 12-Aug-23 9:33am
v3
Comments
Graeme_Grant 12-Aug-23 8:37am    
What chart library?
Ronalyn Ablang 2023 12-Aug-23 8:47am    
WinForms.DataVisualization by Microsoft 1.8.0

1 solution

If you look at the Github Repo[^], there are samples on how to:
1. Use Tooltips[^]
2. Custom ToolTips[^]

From the UsingTooltips example:
C#
	/// <summary>
	/// This method sets tooltips for chart elements
	/// </summary>
	private void ToolTipChanghed()
	{
		// Set ToolTips for Data Point Series
		Chart1.Series[0].ToolTip = SeriesToolTip.Text;

		// Set ToolTips for legend items
		Chart1.Series[0].LegendToolTip = LegendToolTip.Text;

		// Set ToolTips for the Data Point labels
		Chart1.Series[0].LabelToolTip = PointLabelToolTip.Text;

		// Set ToolTips for second Data Point
		Chart1.Series[0].Points[1].ToolTip = PointToolTip.Text;
	}

	private void UsingToolTips_Load(object sender, System.EventArgs e)
	{
		// Set ToolTips
		ToolTipChanghed();
	}

	private void ToolTip_TextChanged(object sender, System.EventArgs e)
	{
		// Set ToolTips
		ToolTipChanghed();
	}

	private void textBox1_TextChanged(object sender, System.EventArgs e)
	{
		// Set ToolTips
		ToolTipChanghed();
	}
}

You want VB code ... I find ChatGpt is good at converting code, not 100% but enough to get you close. On the odd occasion, it gets it right.

Use: "Convert the following code from C# to VB.net:" and paste the code underneath. You should get the following:
VB.NET
Imports System

Public Class YourClassName
    ''' <summary>
    ''' This method sets tooltips for chart elements
    ''' </summary>
    Private Sub ToolTipChanghed()
        ' Set ToolTips for Data Point Series
        Chart1.Series(0).ToolTip = SeriesToolTip.Text

        ' Set ToolTips for legend items
        Chart1.Series(0).LegendToolTip = LegendToolTip.Text

        ' Set ToolTips for the Data Point labels
        Chart1.Series(0).LabelToolTip = PointLabelToolTip.Text

        ' Set ToolTips for second Data Point
        Chart1.Series(0).Points(1).ToolTip = PointToolTip.Text
    End Sub

    Private Sub UsingToolTips_Load(sender As Object, e As EventArgs)
        ' Set ToolTips
        ToolTipChanghed()
    End Sub

    Private Sub ToolTip_TextChanged(sender As Object, e As EventArgs)
        ' Set ToolTips
        ToolTipChanghed()
    End Sub

    Private Sub textBox1_TextChanged(sender As Object, e As EventArgs)
        ' Set ToolTips
        ToolTipChanghed()
    End Sub
End Class

It looks like the code generated is okay. I have not tested it.

Best thing to do is download the code, compile and run the sample projects. See how it works.

Update

I just downloaded and ran the ChartSamples project and had a poke around. Choose the Index tab, then scroll down to ToolTips, and there you will find 3 examples. All 3 show ToolTips.
 
Share this answer
 
v2
Comments
Ronalyn Ablang 2023 15-Aug-23 5:09am    
thank you so much I will try this now

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900