C#实现图表中鼠标移动并显示数据
作者:哎呦喂O_o嗨 时间:2022-10-30 20:45:30
本文实例为大家分享了C#实现图表中鼠标移动并显示数据的具体代码,供大家参考,具体内容如下
效果图:
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);
}
来源:https://blog.csdn.net/weixin_42402326/article/details/111942594
标签:C#,鼠标移动,显示
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
详解SpringBoot禁用Swagger的三种方式
2022-02-28 23:49:08
Android Studio finish()方法的使用与解决app点击“返回”(直接退出)
2022-07-31 09:37:10
![](https://img.aspxhome.com/file/2023/9/137809_0s.jpg)
java swing标准对话框具体实现
2021-12-30 02:48:50
![](https://img.aspxhome.com/file/2023/6/108196_0s.jpg)
详解Java中日志跟踪的简单实现
2023-03-28 00:18:48
C# 实现简易的串口监视上位机功能附源码下载
2023-10-12 11:12:19
![](https://img.aspxhome.com/file/2023/1/106421_0s.png)
Idea中maven项目实现登录验证码功能
2023-11-29 16:50:51
![](https://img.aspxhome.com/file/2023/5/60575_0s.png)
Thymeleaf 3.0 自定义标签方言属性的实例讲解
2023-03-24 20:40:23
C语言转义字符实例详解
2021-08-23 02:12:20
Spring Boot 集成Mybatis实现主从(多数据源)分离方案示例
2023-08-10 07:43:21
![](https://img.aspxhome.com/file/2023/5/64485_0s.png)
Android实现圆角Button按钮
2022-02-04 10:05:45
![](https://img.aspxhome.com/file/2023/2/113282_0s.jpg)
Mybatis中and和循环or混用操作(or转换成in)
2023-09-19 11:08:34
Android游戏开发实践之人物移动地图的平滑滚动处理
2023-12-26 19:45:41
![](https://img.aspxhome.com/file/2023/5/117155_0s.gif)
Java锁擦除与锁粗化概念和使用详解
2022-02-09 15:32:30
winform实现限制及解除鼠标移动范围的方法
2023-07-17 22:11:51
maven实现jar包导入+导出方式
2023-12-13 03:32:22
![](https://img.aspxhome.com/file/2023/2/67142_0s.png)
Spring Security结合JWT的方法教程
2023-01-24 20:52:59
springboot中使用rabbitt的详细方法
2023-06-17 09:57:43
Android EasyBarrage实现轻量级弹幕效果
2022-03-07 06:46:31
![](https://img.aspxhome.com/file/2023/0/138370_0s.gif)
Java实现蓝桥杯G将军的示例代码
2022-05-01 08:44:58
java用applet画图用到的方法(涉及双缓冲)
2021-07-09 17:27:32