C#實現(xiàn)圖表中鼠標移動并顯示數(shù)據(jù)
本文實例為大家分享了C#實現(xiàn)圖表中鼠標移動并顯示數(shù)據(jù)的具體代碼,供大家參考,具體內(nèi)容如下
效果圖:
1.首先在頁面上添加一個label控件并 默認隱藏:
2.給該圖表添加MouseMove鼠標移動事件:
/// <summary> /// 鼠標經(jīng)過時發(fā)生 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void chart1_MouseMove(object sender, MouseEventArgs e)? { ? ?try ? ?{ ? ? ? ?HitTestResult Result = new HitTestResult(); ? ? ? ?Result = chart1.HitTest(e.X, e.Y); ? ? ? ?if (Result.Series != null && Result.Object != null) ? ? ? ?{ ? ? ? ? ? ?// 獲取當前焦點x軸的值 ? ? ? ? ? ?string xValue = ObjectUtil.GetPropertyValue(Result.Object, "AxisLabel").ToString(); ? ? ? ? ? ?// 獲取當前焦點所屬區(qū)域名稱 ? ? ? ? ? ?string areaName = ObjectUtil.GetPropertyValue(Result.Object, "LegendText").ToString(); ? ? ? ? ? ?// 獲取當前焦點y軸的值 ? ? ? ? ? ?double yValue = Result.Series.Points[Result.PointIndex].YValues[0]; ? ? ? ? ? ?// 鼠標經(jīng)過時label顯示 ? ? ? ? ? ?skinLabel4.Visible = true; ? ? ? ? ? ?skinLabel4.Text = "時間:"+ xValue + "\n"+ areaName + ":"+ yValue + "ug/m^3"; ? ? ? ? ? ?skinLabel4.Location = new Point(e.X, e.Y - 20); ? ? ? ?} ? ? ? ?else ? ? ? ?{ ? ? ? ? ? ?// 鼠標離開時label隱藏 ? ? ? ? ? ?skinLabel4.Visible = false; ? ? ? ?} ? ?} ? ?catch (Exception se) ? ?{ ? ? ? ?// 鼠標離開時label隱藏 ? ? ? ?skinLabel4.Visible = false; ? ?} }
3.其中GetPropertyValue() 獲取對象中的某個屬性 方法如下:
public class ObjectUtil { ? ?/// <summary> ? ?/// 獲取某個對象中的屬性值 ? ?/// </summary> ? ?/// <param name="info"></param> ? ?/// <param name="field"></param> ? ?/// <returns></returns> ? ?public static object GetPropertyValue(object info, string field) ? ?{ ? ? ? ?if (info == null) return null; ? ? ? ?Type t = info.GetType(); ? ? ? ?IEnumerable<System.Reflection.PropertyInfo> property = from pi in t.GetProperties() where pi.Name.ToLower() == field.ToLower() select pi; ? ? ? ?return property.First().GetValue(info, null); ? ?} }
另外(以下與上述無關(guān))圖表添加數(shù)據(jù)后綁定提示:
/// <summary> /// 揚塵監(jiān)測、噪音監(jiān)測、溫度檢測、濕度監(jiān)測 /// </summary> /// <param name="_Chart"></param> private void ChartTemperatureMethod(Chart _Chart) { ? ? List<string> xData = new List<string>() {"0", "4:00", "8:00", "12:00", "16:00", "20:00", "24:00" }; ? ? List<int> yData = new List<int>() { 0,21, 35, 48, 40, 27, 7 }; ? ? List<int> yData1 = new List<int>() { 0,5, 18, 25, 68, 50, 30 }; ? ? string iss = "#VALX"; ? ? // 需要提示的信息 ? ? chart1.Series["Series1"].ToolTip = "時間:#VALX\nPM2.5:#VALYug/m^3\tPM10:" + yData1[xData.IndexOf("#VALX") + 1] + "ug/m^3"; ? ? // 標簽顯示 Inside:內(nèi)部,Outside:外部,Disabled:禁用 ? ? chart1.Series["Series1"]["PieLabelStyle"] = "Outside"; ? ? chart1.Series["Series1"].Points.DataBindXY(xData, yData); ? ? // 需要提示的信息 ? ? chart1.Series["Series2"].ToolTip = "時間:#VALX\nPM2.5:" + yData[xData.IndexOf("#VALX") + 1] + "ug/m^3\tPM10:#VALYug/m^3"; ? ? // 標簽顯示 Inside:內(nèi)部,Outside:外部,Disabled:禁用 ? ? chart1.Series["Series2"]["PieLabelStyle"] = "Outside"; ? ? chart1.Series["Series2"].Points.DataBindXY(xData, yData1); }
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#動態(tài)創(chuàng)建Access數(shù)據(jù)庫及表的方法
這篇文章主要介紹了C#動態(tài)創(chuàng)建Access數(shù)據(jù)庫及表的方法,以實例形式分析了創(chuàng)建access數(shù)據(jù)庫及在access數(shù)據(jù)庫中建表的完整過程,是非常實用的技巧,需要的朋友可以參考下2014-12-12C# 添加、修改以及刪除Excel迷你圖表的實現(xiàn)方法
下面小編就為大家分享一篇C# 添加、修改以及刪除Excel迷你圖表的實現(xiàn)方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2017-12-12用序列化實現(xiàn)List<T> 實例的深復制(推薦)
下面小編就為大家?guī)硪黄眯蛄谢瘜崿F(xiàn)List<T> 實例的深復制(推薦)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-02-02