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}");
            }
        }
相关推荐
mudtools5 小时前
.NET驾驭Word之力:玩转文本与格式
c#·.net
唐青枫8 小时前
C#.NET 数据库开发提速秘籍:SqlSugar 实战详解
c#·.net
mudtools1 天前
.NET驾驭Word之力:理解Word对象模型核心 (Application, Document, Range)
c#·.net
侃侃_天下1 天前
最终的信号类
开发语言·c++·算法
echoarts1 天前
Rayon Rust中的数据并行库入门教程
开发语言·其他·算法·rust
Aomnitrix1 天前
知识管理新范式——cpolar+Wiki.js打造企业级分布式知识库
开发语言·javascript·分布式
大飞pkz1 天前
【设计模式】C#反射实现抽象工厂模式
设计模式·c#·抽象工厂模式·c#反射·c#反射实现抽象工厂模式
每天回答3个问题1 天前
UE5C++编译遇到MSB3073
开发语言·c++·ue5
伍哥的传说1 天前
Vite Plugin PWA – 零配置构建现代渐进式Web应用
开发语言·前端·javascript·web app·pwa·service worker·workbox
小莞尔1 天前
【51单片机】【protues仿真】 基于51单片机八路抢答器系统
c语言·开发语言·单片机·嵌入式硬件·51单片机