改下面就行,效果如下
private void chart1_MouseMove(object sender, MouseEventArgs e)
{
Point point = new Point(e.X, e.Y);
chart1.ChartAreas[0].CursorX.SetCursorPixelPosition(point, true);
if (chart1.Series.Count == 0) return;
var x = (int)chart1.ChartAreas[0].CursorX.Position;
if (x < 0 || x >= chart1.Series[0].Points.Count) return;
var builder = new StringBuilder();
builder.AppendLine("X轴:" + chart1.Series[0].Points[x].AxisLabel);
foreach (var s in chart1.Series) builder.AppendLine(s.Name + "Y轴:" + Math.Round(s.Points[x].YValues[0], 2));
chart1.ShowTooltip(toolTip1, builder.ToString(), 4, 3, 10000);
}
注意添加扩展类及往winform中添加tooltip控件
public static class CToolTip
{
public static void ShowTooltip(this Control control, ToolTip tip, string message)
{
Point _mousePoint = Control.MousePosition;
int _x = control.PointToClient(_mousePoint).X;
int _y = control.PointToClient(_mousePoint).Y;
tip.Show(message, control, _x, _y);
tip.Active = true;
}
public static void ShowTooltip(this Control control, ToolTip tip, string message, int durationTime)
{
Point _mousePoint = Control.MousePosition;
int _x = control.PointToClient(_mousePoint).X;
int _y = control.PointToClient(_mousePoint).Y;
tip.Show(message, control, _x, _y, durationTime);
tip.Active = true;
}
public static void ShowTooltip(this Control control, ToolTip tip, string message, int xoffset, int yoffset)
{
Point _mousePoint = Control.MousePosition;
int _x = control.PointToClient(_mousePoint).X;
int _y = control.PointToClient(_mousePoint).Y;
tip.Show(message, control, _x + xoffset, _y + yoffset);
tip.Active = true;
}
public static void ShowTooltip(this Control control, ToolTip tip, string message, int xoffset, int yoffset, int durationTime)
{
Point _mousePoint = Control.MousePosition;
int _x = control.PointToClient(_mousePoint).X;
int _y = control.PointToClient(_mousePoint).Y;
tip.Show(message, control, _x + xoffset, _y + yoffset, durationTime);
tip.Active = true;
}
}
参考:
C# chart图表的简单使用+Tooltip的信息提示_C#Code- Sheep的博客-CSDN博客_c# chart tooltip
//设置图表轴箭头形状 chart1.ChartAreas[0].AxisX.ArrowStyle = AxisArrowStyle.Triangle; chart1.ChartAreas[0].AxisY.ArrowStyle = AxisArrowStyle.Triangle;//点击点提示线 chart1.ChartAreas[0].CursorX.IsUserEnabled = true;//用户缩放 .
https://blog.csdn.net/weixin_39448579/article/details/120543053