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#中創(chuàng)建和讀取XML文件的實現(xiàn)方法
項目中需要將前臺頁面中的信息保存下來并存儲為xml文件格式到數(shù)據(jù)庫中去。因此我先在這里通過一個小實例來學習xml的創(chuàng)建與讀取2013-09-09
C#調(diào)用FFmpeg操作音視頻的實現(xiàn)示例
本文主要介紹了C#調(diào)用FFmpeg操作音視頻的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-01-01
c# Process.Start()找不到系統(tǒng)文件的解決方法
vs1027在X64應用程序下執(zhí)行process.start()時,OK;但是在X86應用程序下執(zhí)行process.start(),報錯:找不到系統(tǒng)文件,本文就詳細的介紹一下解決方法,感興趣的可以了解一下2023-09-09
C#程序中使用LINQ to XML來查詢XML格式數(shù)據(jù)的實例
這篇文章主要介紹了C#程序中使用LINQ to XML來查詢XML格式數(shù)據(jù)的實例,LINQ to XML是.NET框架中集成的接口,可以將XML數(shù)據(jù)放到內(nèi)存中進行處理,需要的朋友可以參考下2016-03-03
C#實現(xiàn)軟件開機自啟動功能(不需要管理員權(quán)限)
在本文中,我們探討了如何使用C#語言實現(xiàn)應用程序在系統(tǒng)啟動時自動運行的功能,同時避免了對管理員權(quán)限的需求,文章通過代碼示例講解的非常詳細,對大家的學習或工作有一定的幫助,需要的朋友可以參考下2025-04-04

