C# 将鼠标悬停在折线图上时显示工具提示
有人能帮我检查代码吗?当鼠标悬停在折线图上时,不会弹出工具提示来显示信息。有什么问题吗?多谢各位 我的想法是,当鼠标沿直线图移动时,它会弹出一个tootip来shpw x轴值和y轴值,但当鼠标悬停在直线图上时,不会弹出任何内容。这是我的代码,我从其他地方复制的鼠标悬停事件:C# 将鼠标悬停在折线图上时显示工具提示,c#,C#,有人能帮我检查代码吗?当鼠标悬停在折线图上时,不会弹出工具提示来显示信息。有什么问题吗?多谢各位 我的想法是,当鼠标沿直线图移动时,它会弹出一个tootip来shpw x轴值和y轴值,但当鼠标悬停在直线图上时,不会弹出任何内容。这是我的代码,我从其他地方复制的鼠标悬停事件: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Dra
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 Project42
{
public partial class Form1 : Form
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
public Form1()
{
InitializeComponent();
chart1.Series[0].Points.AddXY("A", 1);
chart1.Series[0].Points.AddXY("B", 12);
chart1.Series[0].Points.AddXY("C", 23);
chart1.Series[0].Points.AddXY("D", 32);
chart1.Series[0].Points.AddXY("E", 39);
chart1.Series[0].Points.AddXY("F", 43);
chart1.Series[0].Points.AddXY("G", 55);
chart1.Series[0].Points.AddXY("H", 59);
chart1.Series[0].Points.AddXY("I", 67);
}
Point? prevPosition = null;
ToolTip tooltip = new ToolTip();
void chart1_MouseMove(object sender, MouseEventArgs e)
{
var pos = e.Location;
if (prevPosition.HasValue && pos == prevPosition.Value)
return;
tooltip.RemoveAll();
prevPosition = pos;
var results = chart1.HitTest(pos.X, pos.Y, false,
ChartElementType.DataPoint);
foreach (var result in results)
{
if (result.ChartElementType == ChartElementType.DataPoint)
{
var prop = result.Object as DataPoint;
if (prop != null)
{
var pointXPixel = result.ChartArea.AxisX.ValueToPixelPosition(prop.XValue);
var pointYPixel = result.ChartArea.AxisY.ValueToPixelPosition(prop.YValues[0]);
// check if the cursor is really close to the point (2 pixels around the point)
if (Math.Abs(pos.X - pointXPixel) < 2 &&
Math.Abs(pos.Y - pointYPixel) < 2)
{
tooltip.Show("X=" + prop.XValue + ", Y=" + prop.YValues[0], this.chart1,
pos.X, pos.Y - 15);
}
}
}
}
}
}
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows.Forms;
使用System.Windows.Forms.DataVisualization.Charting;
命名空间项目42
{
公共部分类Form1:Form
{
静态类程序
{
///
///应用程序的主要入口点。
///
[状态线程]
静态void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(新Form1());
}
}
公共表格1()
{
初始化组件();
图表1.系列[0].点.AddXY(“A”,1);
图1.系列[0].点.AddXY(“B”,12);
图表1.系列[0].点.AddXY(“C”,23);
图表1.系列[0].点.AddXY(“D”,32);
图表1.系列[0].点.AddXY(“E”,39);
图表1.系列[0].点.AddXY(“F”,43);
图1.系列[0].点.AddXY(“G”,55);
图1.系列[0].点.AddXY(“H”,59);
图1.系列[0].点.AddXY(“I”,67);
}
点?位置=空;
工具提示工具提示=新工具提示();
无效图表1\u MouseMove(对象发送器,MouseEventArgs e)
{
var pos=e.位置;
if(prevPosition.HasValue&&pos==prevPosition.Value)
返回;
工具提示:RemoveAll();
位置=位置;
var结果=图表1.HitTest(位置X,位置Y,错误,
ChartElementType.DataPoint);
foreach(结果中的var结果)
{
if(result.ChartElementType==ChartElementType.DataPoint)
{
var prop=结果。对象作为数据点;
如果(prop!=null)
{
var pointXPixel=result.ChartArea.AxisX.ValueToPixelPosition(prop.XValue);
var pointYPixel=result.chartraea.AxisY.ValueToPixelPosition(prop.YValues[0]);
//检查光标是否真的靠近点(点周围2个像素)
如果(数学绝对值(位置X-点X像素)<2&&
数学绝对值(位置Y-点像素)<2)
{
工具提示.Show(“X=“+prop.XValue+”,Y=“+prop.yvalue[0],this.chart1,
位置X、位置Y-15);
}
}
}
}
}
}
}
试试这个
这是我的财务(棒,烛台)图表的工作。不像大多数示例那样显示DataPoint的YValue[0]
,而是显示Y轴的YValue
Point? prevPosition = null;
ToolTip tooltip = new ToolTip();
private void chart_MouseMove(object sender, MouseEventArgs e)
{
var pos = e.Location;
if (prevPosition.HasValue && pos == prevPosition.Value)
return;
tooltip.RemoveAll();
prevPosition = pos;
var results = chart.HitTest(pos.X, pos.Y, false, ChartElementType.DataPoint); // set ChartElementType.PlottingArea for full area, not only DataPoints
foreach (var result in results)
{
if (result.ChartElementType == ChartElementType.DataPoint) // set ChartElementType.PlottingArea for full area, not only DataPoints
{
var yVal = result.ChartArea.AxisY.PixelPositionToValue(pos.Y);
tooltip.Show(((int)yVal).ToString(), chart, pos.X, pos.Y - 15);
}
}
}
我会检查你发送的坐标是否正确(x,y),然后如果你的工具提示控件是你的图表控件的子控件,如果它不起作用,那么你可以尝试一个已经在工作的库,我正在做这件事,因为我不喜欢你的C代码工作的当前开源选项,但是..:在进行Hittest时,你需要实际命中一个数据点,不仅仅是两点之间的线!顺便说一句,向数据点本身添加工具提示时也是如此。。因此,它不适用于线图或点图类型另外:您希望看到中间线的值是什么?插值?你可以计算它们,但命中测试对你没有帮助你可能想学习。。