How to change and display a tooltip on a chart in C # when the mouse hovers over a panel?

I have System.Windows.Forms.DataVisualization.Charting.chart , and I want to show some information about the panel in the chart when you hover over it. But I don’t see where to set the tooltip.

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

but how can I choose the value of x or y that I look through to change the text?

+7
source share
4 answers

You can also add a tooltip to DataPoint when creating 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 is a bit neater / cleaner than replacing the text in the GetToolTipText event

+8
source
  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] ); } } 
+3
source

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

  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); } } } 
+1
source

I am surprised that no one mentioned a simple and standard solution, so I have to answer a 5-year question.

Just add keywords to the tooltip. They are automatically replaced by the values ​​of the points you are pointing. Something like that:

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

They should cover almost all of the options for using tooltips. In rare cases, they do not cover, you can use what other answers offer.

Additional information: https://msdn.microsoft.com/en-us/library/dd456687.aspx

+1
source

All Articles