C#图表中鼠标移动并显示数据

本文介绍了一种方法,通过在图表控件上添加MouseMove事件来实现实时数据显示。当鼠标移动到图表上的特定点时,会显示一个label控件,展示对应的时间和数值信息。同时,展示了如何使用GetPropertyValue方法获取对象属性值,以及如何为图表系列设置数据绑定和提示信息,以便在图表中显示扬尘、噪音、温度和湿度等监测数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

效果图:
在这里插入图片描述
在这里插入图片描述

1.首先在页面上添加一个label控件并 默认隐藏
在这里插入图片描述

2.给该图表添加MouseMove鼠标移动事件:

/// <summary>
/// 鼠标经过时发生
/// </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();
           // 获取当前焦点所属区域名称
           string areaName = ObjectUtil.GetPropertyValue(Result.Object, "LegendText").ToString();
           // 获取当前焦点y轴的值
           double yValue = Result.Series.Points[Result.PointIndex].YValues[0];

           // 鼠标经过时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);
   }
}

另外(以下与上述无关)图表添加数据后绑定提示:

在这里插入图片描述

/// <summary>
/// 扬尘监测、噪音监测、温度检测、湿度监测
/// </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:内部,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:内部,Outside:外部,Disabled:禁用
    chart1.Series["Series2"]["PieLabelStyle"] = "Outside";
    chart1.Series["Series2"].Points.DataBindXY(xData, yData1);
}
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

哎呦喂O_o嗨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值