C# 使用鼠標點擊對Chart控件實現(xiàn)數(shù)據(jù)提示效果
前言
上一篇文章C# Chart控件標記問題詳解,我們對C#Chart控件標記問題做了一系列的處理,今天是對上一篇文章的一個擴展,使用鼠標點擊事件對Chart上面的折線點進行數(shù)據(jù)展示,是另外的一種展示方式,不明白的同學可以去看看我上一篇文章,這篇文章使用的方式就是點擊一下就彈出一個小的提示框,可以在提示框中寫我們自己想要寫的數(shù)據(jù),創(chuàng)作不易,大家點贊關(guān)注評論收藏,你的點贊是我創(chuàng)作的動力,謝謝啦!??!
效果展示
使用tooltip的方式使用鼠標點擊Chart中的折線,實現(xiàn)Chart的數(shù)據(jù)提示效果,需要使用鼠標的點擊事件獲取點擊位置的x,y的坐標,并對這個坐標在Chart折線圖中找到對應的點的值,找到對應點之后使用chart的point屬性對這個點的值的獲取,從而展示,后面有詳細的操作步驟,以及代碼邏輯,可以直接跳的最后面的解決方案去看。
解決方案
圖一圖二是對界面的一些的一些的設(shè)置
主要的代碼邏輯
整個項目的界面代碼,主要的這個功能主要的代碼在鼠標的那個觸發(fā)函數(shù)的代碼,直接復制那個函數(shù)的代碼,粘貼就好了,不是很難。
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Windows.Forms.DataVisualization.Charting; namespace TestIC00 { public partial class Form2 : Form { public Form2() { InitializeComponent(); } private void Form2_Load(object sender, EventArgs e) { } public int index = 0; public int x = 0; public bool flag = false; private void button1_Click(object sender, EventArgs e) { timer1.Enabled = !timer1.Enabled; } private void timer1_Tick(object sender, EventArgs e) { Random random = new Random(); this.chart1.ChartAreas[0].AxisX.MajorGrid.Interval = 1;//網(wǎng)格間隔 this.chart1.ChartAreas[0].AxisX.MinorGrid.Interval = 1; this.chart1.ChartAreas[0].AxisY.MajorGrid.Interval = 1;//網(wǎng)格間隔 this.chart1.ChartAreas[0].AxisY.MinorGrid.Interval = 1; this.chart1.ChartAreas[0].AxisX.LabelStyle.Interval = 1;//設(shè)置X軸的值的間隔大小 this.chart1.ChartAreas[0].AxisX.MajorGrid.LineColor = Color.Gray;//設(shè)置X軸網(wǎng)格線顏色 this.chart1.ChartAreas[0].AxisY.MajorGrid.LineColor = Color.Gray;//設(shè)置Y軸網(wǎng)格線顏色 chart1.ChartAreas[0].AxisX.ScrollBar.Enabled = true;//啟動滾動條 this.chart1.ChartAreas[0].AxisY.LabelStyle.Enabled = false;//使Y軸的刻度隱藏 chart1.ChartAreas[0].AxisX.ScaleView.Scroll(System.Windows.Forms.DataVisualization.Charting.ScrollType.Last);//啟用視圖實現(xiàn)數(shù)據(jù)滾動 int value = random.Next(0, 20);//產(chǎn)生隨機數(shù)進行賦值 chart1.Series[0].Points.AddXY(x,value);//對折線圖添加數(shù)據(jù) x++; if (flag)//判斷標記,如果是true表示只標記最新,需要去掉前面的一個值 { this.chart1.Series[0].Points[index].MarkerStyle = MarkerStyle.Circle;//設(shè)置標記的形狀為圓形 this.chart1.Series[0].Points[index].MarkerColor = Color.Red;//形狀顏色設(shè)置 this.chart1.Series[0].Points[index].MarkerBorderWidth = 3;//形狀大小設(shè)置 this.chart1.Series[0].Points[index].MarkerSize = 10;//設(shè)置我們展示標記的大小 this.chart1.Series[0].Points[index].Label = "功能:" + this.chart1.Series[0].Name + "\r\n" + "值:" + value.ToString();//對標記展示的值 this.chart1.Series[0].Points[index].IsValueShownAsLabel = true;//展示標記 this.chart1.Series[0].Points[index - 1].MarkerBorderWidth = 0;//改前一個標記的大小 this.chart1.Series[0].Points[index - 1].MarkerSize = 0;//形狀大小 this.chart1.Series[0].Points[index - 1].Label = "";//展示數(shù)據(jù) this.chart1.Series[0].Points[index - 1].IsValueShownAsLabel = false;//不展示 } else//對數(shù)據(jù)一直標記 { this.chart1.Series[0].Points[index].MarkerStyle = MarkerStyle.Circle; this.chart1.Series[0].Points[index].MarkerColor = Color.Red; this.chart1.Series[0].Points[index].MarkerBorderWidth = 3; this.chart1.Series[0].Points[index].MarkerSize = 10; this.chart1.Series[0].Points[index].Label = "功能:" + this.chart1.Series[0].Name + "\r\n" + "值:" + value.ToString(); this.chart1.Series[0].Points[index].IsValueShownAsLabel = true; } //也可以加一種狀態(tài)是什么也不標記,你們自己對那個狀態(tài)值的處理就可以啦 index++; } private void button2_Click(object sender, EventArgs e) { flag = !flag; } private void chart1_MouseClick(object sender, MouseEventArgs e) { HitTestResult mytestresult = new HitTestResult();//它表示命中測試的返回值 mytestresult = chart1.HitTest(e.X, e.Y);//獲取我們Chart控件的折線圖在這個坐標的值 if (e.Button == MouseButtons.Left)//判斷是否是鼠標左鍵點擊 { if (mytestresult.ChartElementType == ChartElementType.DataPoint)//判斷我們點擊這個返回集是否是chart的數(shù)據(jù)點的類型 { toolTip1.AutoPopDelay = 5000;//表示tooltip在這個控件中保留展示的時間 toolTip1.InitialDelay = 1000;//表示鼠標指針必須在這里靜止的時間 toolTip1.ReshowDelay = 500;//可以縮短或延長在顯示上一個工具提示窗口后顯示工具提示窗口之前等待的時間tooltip toolTip1.ShowAlways = true;//獲取或設(shè)置一個值,該值指示是否顯示工具提示窗口,甚至是在其父控件不活動的時候。 try { toolTip1.SetToolTip(chart1, "名稱:" + mytestresult.Series.Name + "\r\n" +"值:"+chart1.Series[0].Points[mytestresult.PointIndex]);//設(shè)置ToolTip展示的值的內(nèi)容,mytestresult.Series.Name表示折線的名字,chart1.Series[0].Points[mytestresult.PointIndex]表示chart圖表中的第0(從0開始也就是第一條)條折線的數(shù)據(jù)點的第多少個, //也可以設(shè)置chart1.Series[0].Points[mytestresult.PointIndex].XValue;這個代表你點擊這個點的x軸的值,chart1.Series[0].Points[mytestresult.PointIndex].YValues[0];這個代表你點擊的點Y軸的值 } catch (Exception) { } } } } } }
總結(jié)
這篇文章是對上一篇文章的擴展,使用的是ToolTip技術(shù)和鼠標點擊的事件,再加上測試返回集,雖然很簡單,但是也是可以學到東西的,C#的技術(shù)就是先簡單再難嘛,積少成多之后才會成長才會進步,我們要不斷的學習不斷的探索,才能有學習的動力,才會有學習的欲望
到此這篇關(guān)于C# 使用鼠標點擊對Chart控件實現(xiàn)數(shù)據(jù)提示效果的文章就介紹到這了,更多相關(guān)C# Chart控件 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#使用Selenium+PhantomJS抓取數(shù)據(jù)
本文主要介紹了C#使用Selenium+PhantomJS抓取數(shù)據(jù)的方法步驟,具有很好的參考價值,下面跟著小編一起來看下吧2017-02-02C#調(diào)用AForge實現(xiàn)攝像頭錄像的示例代碼
這篇文章主要介紹了C#調(diào)用AForge實現(xiàn)攝像頭錄像的示例代碼,非常具有實用價值,需要的朋友可以參考下2017-09-09淺析C# web訪問mysql數(shù)據(jù)庫-整理歸納總結(jié)
本篇文章是對C#中的web訪問mysql數(shù)據(jù)庫的一些知識點進行了整理歸納總結(jié),需要的朋友可以參考下2013-07-07