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}");
            }
        }
相关推荐
晨星shine16 小时前
GC、Dispose、Unmanaged Resource 和 Managed Resource
后端·c#
用户298698530141 天前
.NET 文档自动化:Spire.Doc 设置奇偶页页眉/页脚的最佳实践
后端·c#·.net
用户3667462526741 天前
接口文档汇总 - 2.设备状态管理
c#
用户3667462526741 天前
接口文档汇总 - 3.PLC通信管理
c#
Ray Liang2 天前
用六边形架构与整洁架构对比是伪命题?
java·python·c#·架构设计
Scout-leaf5 天前
WPF新手村教程(三)—— 路由事件
c#·wpf
用户298698530145 天前
程序员效率工具:Spire.Doc如何助你一键搞定Word表格排版
后端·c#·.net
mudtools6 天前
搭建一套.net下能落地的飞书考勤系统
后端·c#·.net
玩泥巴的6 天前
搭建一套.net下能落地的飞书考勤系统
c#·.net·二次开发·飞书
唐宋元明清21887 天前
.NET 本地Db数据库-技术方案选型
windows·c#