ChartControl 鼠标事件

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sinat_15155817/article/details/48527295

今天使用dev 中的 ChartControl 控件,绘制了一个柱状图,想在鼠标移动到柱状时,显示提示信息

刚开始一直在找 SeriesPoint 中的mouse 事件,它其实是一个对象,根本不存在鼠标事件,

最后还是通过  ChartControl 控件 Mouse事件解决了。


<pre class="csharp" name="code"> private void button1_Click(object sender, EventArgs e1)
        {
            var SeriesPoint1 = new SeriesPoint("111", new object[] { ((object)(123)) });
            var SeriesPoint2 = new SeriesPoint("222", new object[] { ((object)(222)) });
            var SeriesPoint3 = new SeriesPoint("333", new object[] { ((object)(333)) });
            var SeriesPoint4 = new SeriesPoint("444", new object[] { ((object)(444)) });
            SeriesPoint1.Tag = "SeriesPoint1";
            SeriesPoint2.Tag = "SeriesPoint2";
            SeriesPoint3.Tag = "SeriesPoint3";
            SeriesPoint4.Tag = "SeriesPoint4";
            this.chartControl1.Series[0].Points.AddRange(new SeriesPoint[] { SeriesPoint1, SeriesPoint2, SeriesPoint3, SeriesPoint4 });

            var ToolTip = new ToolTip();
            this.chartControl1.MouseMove += ((s, e) =>
            {
                var chart = s as ChartControl;
                var MouseEventArgs = e as MouseEventArgs;
                var hitInfo = chart.CalcHitInfo(MouseEventArgs.X, MouseEventArgs.Y);
                if (hitInfo.InSeries)
                {
                    var SeriesPoint = hitInfo.SeriesPoint;
                    ToolTip.SetToolTip(chart, SeriesPoint.Tag != null ? SeriesPoint.Tag.ToString() : "");
                }
                else
                {
                    ToolTip.SetToolTip(chart, "");
                }
            });

        }



鼠标未移动到柱子上时



鼠标移动到 444 上显示的提示信息为 SeriesPoint4


关键是这句代码

var hitInfo = chart.CalcHitInfo(MouseEventArgs.X, MouseEventArgs.Y);

阅读更多
换一批

没有更多推荐了,返回首页