9

I have a System.Windows.Forms.DataVisualization.Charting.chart and I want to show some information about a bar on the chart when you hover over it. But I can't see where to set a tooltip.

I can set this chart3.Series[0].ToolTip = "hello world";

but how do I pick up which x or y value I am hovering over in order to modify the text?

4 Answers 4

16

I'm surprised nobody mentioned the simple and standard solution yet so I'm compelled to answer a 5 year-old question.

Just add chart keywords to the tooltip string. They get automatically replaced by values of the points you hover over. Something like this:

chart3.Series[0].ToolTip = "hello world from #VALX, #VAL";

These should cover almost all chart tooltip use cases. For the rare cases they don't cover, you can use what the other answers suggest.

More info: https://msdn.microsoft.com/en-us/library/dd456687.aspx

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, and you can just set it as a property as well
10

You can also add a tooltip to a DataPoint when you construct it

DataPoint point = new DataPoint();
point.SetValueXY(x, y);
point.ToolTip = string.Format("{0}, {1}", x, y);
series.Points.Add(point);

In my opinion this a bit neater / cleaner than replacing the text in the GetToolTipText event

2 Comments

Yep, but on long series with thousands of points, you end up building thousands of strings that are almost never used.
Sure that's a good point. I've only used it for fairly simple bar graphs. Another option for tooltips which may require more than just the X and Y of the DataPoint is to use the DataPoint Tag property to store a reference to a more complex object and use that for Text generation in the GetToolTipText handler as shown by Hassan.
4
    this.chart1.GetToolTipText += new System.EventHandler<System.Windows.Forms.DataVisualization.Charting.ToolTipEventArgs>(this.Chart1_GetToolTipText);
...
// [2] in x.cs file.
private void Chart1_GetToolTipText(object sender, System.Windows.Forms.DataVisualization.Charting.ToolTipEventArgs e)
{

   // Check selevted chart element and set tooltip text
   if (e.HitTestResult.ChartElementType == ChartElementType.DataPoint)
   {
      int i = e.HitTestResult.PointIndex;
      DataPoint dp = e.HitTestResult.Series.Points[i];
      e.Text = string.Format("{0:F1}, {1:F1}", dp.XValue, dp.YValues[0] );
   }
}

Comments

1

It's work for my financial (stick, candlestick) charts. Show not YValue[0] of DataPoint as most of the examples, but YValue of axis Y.

    Point? prevPosition = null;
    ToolTip tooltip = new ToolTip();

    private void chart_MouseMove(object sender, MouseEventArgs e)
    {
        var pos = e.Location;
        if (prevPosition.HasValue && pos == prevPosition.Value)
            return;
        tooltip.RemoveAll();
        prevPosition = pos;
        var results = chart.HitTest(pos.X, pos.Y, false, ChartElementType.DataPoint); // set ChartElementType.PlottingArea for full area, not only DataPoints
        foreach (var result in results)
        {
            if (result.ChartElementType == ChartElementType.DataPoint) // set ChartElementType.PlottingArea for full area, not only DataPoints
            {
                var yVal = result.ChartArea.AxisY.PixelPositionToValue(pos.Y);
                tooltip.Show(((int)yVal).ToString(), chart, pos.X, pos.Y - 15);
            }
        }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.