Kevin Badger
Kevin Badger

Reputation: 91

How do I change and display a tooltip on a chart in c# when the mouse hovers over a bar?

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?

Upvotes: 9

Views: 28538

Answers (4)

relatively_random
relatively_random

Reputation: 5126

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

Upvotes: 16

Evgeny Ivanov
Evgeny Ivanov

Reputation: 534

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);
            }
        }
    }

Upvotes: 1

Lummo
Lummo

Reputation: 1169

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

Upvotes: 10

Hassan Boutougha
Hassan Boutougha

Reputation: 3929

    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] );
   }
}

Upvotes: 4

Related Questions