C#中鼠标点击获取Chart图形上的坐标值

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}");
            }
        }
相关推荐
raoxiaoya1 分钟前
同时安装多个版本的golang
开发语言·后端·golang
浅陌sss30 分钟前
C#中实现XML解析器
xml·c#
cloues break.1 小时前
C++进阶----多态
开发语言·c++
我不会编程5552 小时前
Python Cookbook-6.10 保留对被绑定方法的引用且支持垃圾回收
开发语言·python
道剑剑非道2 小时前
QT开发技术【qcustomplot 曲线与鼠标十字功能】
开发语言·qt·计算机外设
刘婉晴2 小时前
【环境配置】Mac电脑安装运行R语言教程 2025年
开发语言·macos·r语言
Despacito0o2 小时前
C++核心编程:类与对象全面解析
开发语言·c++
Tiger Z2 小时前
R 语言科研绘图第 43 期 --- 桑基图-冲击
开发语言·r语言·贴图
全栈师3 小时前
C#中分组循环的做法
开发语言·c#
FAREWELL000753 小时前
C#进阶学习(十六)C#中的迭代器
开发语言·学习·c#·迭代器模式·迭代器