免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
MsChart鼠標(biāo)點(diǎn)擊,彈出數(shù)據(jù)點(diǎn)DataPoint信息
    最近的一個(gè)項(xiàng)目,大量用到微軟MSChart控件,控件中提供了針對數(shù)據(jù)點(diǎn)的ToolTip(即鼠標(biāo)放上去,自動(dòng)出現(xiàn)信息,移走后,信息消失),但是卻沒有提供點(diǎn)擊數(shù)據(jù)點(diǎn)(DataPoint)時(shí),彈出相應(yīng)信息的功能。這會(huì)造成用戶無法進(jìn)行有選擇性的比較查看多個(gè)數(shù)據(jù)點(diǎn)信息。盡管可以顯示所有數(shù)據(jù)點(diǎn)的Y值,但是在數(shù)據(jù)點(diǎn)較多時(shí),這會(huì)造成圖表的凌亂。

    經(jīng)過大量對MsChart的研究,現(xiàn)在實(shí)現(xiàn)該功能,即:鼠標(biāo)點(diǎn)擊數(shù)據(jù)點(diǎn)時(shí),彈出相應(yīng)信息顯示區(qū)域,并可自由拖動(dòng)該區(qū)域;雙擊該信息顯示區(qū)域,該區(qū)域消失。

   實(shí)現(xiàn) 此功能的關(guān)鍵就是chart控件中的GetToolTipText事件,微軟解釋是:”在顯示工具提示之前發(fā)生以獲取工具提示文本“。我的理解就是只要鼠標(biāo)在char控件范圍內(nèi),該事件就一直跟蹤鼠標(biāo)軌跡,即掃描鼠標(biāo)指針的坐標(biāo)X值和Y值;如果指針放到數(shù)據(jù)點(diǎn)上,就彈出ToolTipText。

    對應(yīng)到具體方法如:private void chart1_GetToolTipText(object sender, ToolTipEventArgs e)

其中ToolTipEventArgs 類的HitTestResult對象,就可以的得到數(shù)據(jù)點(diǎn)DataPoint的信息:

HitTestResult:獲取 HitTestResult 對象,該對象提供了有關(guān)引發(fā)GetToolTipText事件的圖表元素的信息。
Text:獲取或設(shè)置當(dāng)鼠標(biāo)光標(biāo)停留在圖表元素上時(shí)所顯示的工具提示(如果有)。此屬性也可以為此圖表元素設(shè)置工具提示。
X:獲取引發(fā) GetToolTipText 事件時(shí)鼠標(biāo)光標(biāo)停留的點(diǎn)的 X 坐標(biāo)。
Y:獲取引發(fā) GetToolTipText 事件時(shí)鼠標(biāo)光標(biāo)停留的點(diǎn)的 Y 坐標(biāo)。

因此,我的思路是:

在Form中設(shè)置一個(gè)toolTipEventArgs作為內(nèi)部屬性,一旦指針移動(dòng)到char里的數(shù)據(jù)點(diǎn)上,在引發(fā)GetToolTipText事件時(shí),將事件中得到的的ToolTipEventArgs賦值給toolTipEventArgs屬性,并在點(diǎn)擊鼠標(biāo)的chart1_Click事件中,使用toolTipEventArgs屬性中獲取的DataPoint信息,填充動(dòng)態(tài)生成的Label的Text。

然后使用Label的MouseDown,MouseMove,MouseUp實(shí)現(xiàn)Label的可拖拽。使用Label的MouseDoubleClick實(shí)現(xiàn)Label的消除。

代碼如下(VS2010):

首先在Form中拖入一個(gè)Chart控件,命名為chart1。

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Windows.Forms.DataVisualization.Charting;namespace MsChartTest{    public partial class Form1 : Form    {        /// <summary>        /// 緩存GetToolTipText事件中的ToolTipEventArgs        /// </summary>        ToolTipEventArgs toolTipEventArgs;        //設(shè)置數(shù)據(jù)        List<string> xList = new List<string>() { "x1", "x2", "x3", "x4", "x5" };        List<string> yList = new List<string>() { "1", "2", "3", "4", "1" };        /// <summary>        /// Form構(gòu)造方法        /// </summary>        public Form1()        {            InitializeComponent();            //綁定數(shù)據(jù)            chart1.Series[0].Points.DataBindXY(xList,yList);            //設(shè)置Chart樣式為折線圖            chart1.Series[0].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line ;            chart1.Series[0].BorderWidth = 1;            chart1.Series[0].IsValueShownAsLabel = true;            //設(shè)置數(shù)據(jù)點(diǎn)樣式為圓點(diǎn)            chart1.Series[0].MarkerStyle = System.Windows.Forms.DataVisualization.Charting.MarkerStyle.Circle;        }        private bool isMouseDown = false;        private Point mouseOffset; //記錄鼠標(biāo)指針的坐標(biāo)        private void lbl_ToolTip_MouseDown(object sender, MouseEventArgs e)        {            if (e.Button == MouseButtons.Left)            {                mouseOffset.X = e.X;                mouseOffset.Y = e.Y;                isMouseDown = true;            }        }        private void lbl_ToolTip_MouseMove(object sender, MouseEventArgs e)        {            if (isMouseDown)            {                int left = ((Label )sender).Left + e.X - mouseOffset.X;                int top = ((Label)sender).Top + e.Y - mouseOffset.Y;                ((Label)sender).Location = new Point(left, top);            }        }        private void lbl_ToolTip_MouseUp(object sender, MouseEventArgs e)        {            if (e.Button == MouseButtons.Left)            {                isMouseDown = false;            }        }        private void chart1_Click(object sender, EventArgs e)        {            MouseEventArgs mouseEventArgs = (MouseEventArgs)e;            //判斷鼠標(biāo)是否放在了數(shù)據(jù)點(diǎn)之上            if (mouseEventArgs.X == toolTipEventArgs.X && mouseEventArgs.Y == toolTipEventArgs.Y)            {                //動(dòng)態(tài)生成Label                Label lbl_ToolTip = new Label();                //使用數(shù)據(jù)點(diǎn)的x值和y值格式化Label的Text                lbl_ToolTip.Text = string.Format("X={0};Y={1}",                    chart1.Series[0].Points[toolTipEventArgs.HitTestResult.PointIndex].AxisLabel,                    chart1.Series[0].Points[toolTipEventArgs.HitTestResult.PointIndex].YValues[0]);                lbl_ToolTip.Location = new Point(mouseEventArgs.X, mouseEventArgs.Y );                lbl_ToolTip.BackColor = Color.LemonChiffon;                lbl_ToolTip.BorderStyle = BorderStyle.FixedSingle;                //Label訂閱相關(guān)的可拖拽事件                lbl_ToolTip.MouseDown += new MouseEventHandler(lbl_ToolTip_MouseDown);                lbl_ToolTip.MouseMove += new MouseEventHandler(lbl_ToolTip_MouseMove);                lbl_ToolTip.MouseUp += new MouseEventHandler(lbl_ToolTip_MouseUp);                //Label訂閱雙擊消除事件                lbl_ToolTip.MouseDoubleClick += new MouseEventHandler(lbl_ToolTip_MouseDoubleClick);                //這里是保證將自動(dòng)生成的Label可以放置在chart1之上的關(guān)鍵                this.Controls.Add(lbl_ToolTip);                //BringToFront()一定要在this.Controls.Add(lbl_ToolTip)之后,否則不能覆蓋chart1                lbl_ToolTip.BringToFront();            }        }        /// <summary>        /// 雙擊事件        /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        void lbl_ToolTip_MouseDoubleClick(object sender, MouseEventArgs e)        {            ((Label)sender).Dispose();        }        /// <summary>        /// GetToolTipText事件        /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        private void chart1_GetToolTipText(object sender, System.Windows.Forms.DataVisualization.Charting.ToolTipEventArgs e)        {            //我的理解就是只要鼠標(biāo)在char控件范圍內(nèi),該事件就一直跟蹤鼠標(biāo)軌跡,即掃描鼠標(biāo)指針的坐標(biāo)X值和Y值            //因此當(dāng)鼠標(biāo)指針放置在數(shù)據(jù)點(diǎn)上時(shí),即e.HitTestResult.ChartElementType == ChartElementType.DataPoint,可將e賦值給toolTipEventArgs屬性            if (e.HitTestResult.ChartElementType == ChartElementType.DataPoint)            {                toolTipEventArgs = e;            }        }    }}

效果如下:

本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊舉報(bào)
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
C#中MouseDown和MouseUp事件
關(guān)于C#事件處理函數(shù)中的參數(shù)(object sender, EventArgs e)
c# winform 如何用鼠標(biāo)滾輪改變pictureBox中圖片的大小`?
C#鼠標(biāo)拖動(dòng)控件并繪制虛框介紹
一步一步學(xué)Silverlight 2系列(4):鼠標(biāo)事件處理
筆記4:與事件的交互
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服