在鼠标悬停时查看图表中点的值 [英] see th value of points in chart when hovering mouse on

查看:135
本文介绍了在鼠标悬停时查看图表中点的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨。我的项目中有一个图表。我希望用户在将鼠标悬停在工具提示上时查看每个点的值。我该怎么做?

编辑:

有人给我发了以下代码,我试过但是我无法改进它。

Hi.I have a chart in my project.I want the user to see the value of each point in a tooltip ,when hovering the mouse on it.How can I do that?

Some One sent me the following codes,I tried but I couldn't improve it.

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.PlottingArea);
     foreach (var result in results)
     { 
        if (result.ChartElementType == ChartElementType.PlottingArea)
         {
             var xVal = result.ChartArea.AxisX.PixelPositionToValue(pos.X);
                    var yVal = result.ChartArea.AxisY.PixelPositionToValue(pos.Y); 
                    tp.Show("X=" + xVal + ", Y=" + yVal, this.chart2, pos.X, pos.Y - 15);
         }
     }
 } 



它显示我移动鼠标的值。但是我希望仅在显示值时显示指针靠近系列点。

再次编辑:


It shows the values where ever I move the mouse.But I want the values be shown only when the pointer is near the series points.
Edit again:

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.PlottingArea);
     foreach (var result in results)
     { 
        if (result.ChartElementType == ChartElementType.PlottingArea)
         {
             series2.ToolTip = "X=#VALX, Y=#VALY";
         }
     }
 } 





但我想只在指针显示时显示值 系列点。

推荐答案

你正在使用的图表控件必须支持此功能。那么,你使用什么图表控件?
The chart control you're using has to support this feature. So, what chart control are you using?


可能是这篇文章可以帮助:) ..查看工具提示部分或下载源代码参考

< br $> b $ b

A使用MSChart for .NET的指南 [ ^ ]



honeyashu
May be this Article could Help :).. See the Tool Tip section or download the source for reference


A Guide to using MSChart for .NET[^]

honeyashu


void chart1_GetToolTipText(object sender, ToolTipEventArgs e)
        {   HitTestResult hitTestResult = chart1.HitTest(e.X, e.Y);
            
                if (hitTestResult.PointIndex >= 0 )
        if( hitTestResult.ChartElementType == ChartElementType.DataPoint)
        {    tooltip.RemoveAll();
              
                var results = chart1.HitTest(e.X, e.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(e.X - pointXPixel) < 2 &&
                                Math.Abs(e.Y - pointYPixel) < 2)
                            {
                                tooltip.Show(prop.XValue +
                                    "," + prop.YValues[0], chart1,
                                    e.X, e.Y - 15);
                            }
                        }
                    }
                }
            }
        }


这篇关于在鼠标悬停时查看图表中点的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆