cs
/// <summary>
/// C#中鼠标移动获取Chart图形上的坐标值
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void chart1_GetToolTipText(object sender, System.Windows.Forms.DataVisualization.Charting.ToolTipEventArgs e)
{
if (e.HitTestResult.ChartElementType == ChartElementType.DataPoint)
{
this.Cursor = Cursors.Cross;
int i = e.HitTestResult.PointIndex;
System.Text.StringBuilder dpStr = new System.Text.StringBuilder();
foreach (var item in chart1.Series)
{
DataPoint dp = item.Points[i];
dpStr.Append(item.Name + " X:" + dp.XValue + " Y:" + dp.YValues[0] + "\r\n");
}
e.Text = dpStr.ToString();
}
else
{
this.Cursor = Cursors.Default;
}
}
=========================================================================
cs
this.chart1.MouseClick += new MouseEventHandler(chart1_MouseClick);
cs
/// <summary>
/// C#中鼠标点击获取Chart图形上的坐标值
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void chart1_MouseClick(object sender, MouseEventArgs e)
{
// 获取点击位置的点的坐标
HitTestResult result = chart1.HitTest(e.X, e.Y);
if (result.ChartElementType == ChartElementType.DataPoint)
{
int index = result.PointIndex;
DataPoint point = chart1.Series[0].Points[index];
double xValue = point.XValue;
double yValue = point.YValues[0];
MessageBox.Show($"Clicked at: X = {xValue}, Y = {yValue}");
}
}