經(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; } } }}
效果如下:
聯(lián)系客服