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}");
            }
        }
相关推荐
JS.Huang3 分钟前
【JavaScript】原生函数
开发语言·javascript·ecmascript
CoderCodingNo1 小时前
【GESP】C++五级考试大纲知识点梳理, (5) 算法复杂度估算(多项式、对数)
开发语言·c++·算法
星河队长1 小时前
VS创建C++动态库和C#访问过程
java·c++·c#
ftpeak1 小时前
JavaScript性能优化实战
开发语言·javascript·性能优化
William_cl2 小时前
【C# MVC 前置】异步编程 async/await:从 “卡界面” 到 “秒响应” 的 Action 优化指南(附微软官方避坑清单)
microsoft·c#·mvc
一个很帅的帅哥2 小时前
JavaScript事件循环
开发语言·前端·javascript
驰羽2 小时前
[GO]gin框架:ShouldBindJSON与其他常见绑定方法
开发语言·golang·gin
程序员大雄学编程2 小时前
「用Python来学微积分」5. 曲线的极坐标方程
开发语言·python·微积分
yong99902 小时前
C#驱动斑马打印机实现包装自动打印
java·数据库·c#
Jose_lz3 小时前
C#开发学习杂笔(更新中)
开发语言·学习·c#