【问题标题】:Display tooltip when mouse over the line chart鼠标悬停在折线图上时显示工具提示
【发布时间】:2015-11-29 01:31:47
【问题描述】:

谁能帮我检查我的代码?当我的鼠标悬停在折线图上时,它不会弹出工具提示来显示信息。有什么问题吗?谢谢。

我的想法是当鼠标沿折线图移动时,它会弹出一个提示来显示x轴值和y轴值,但是当我的鼠标在折线图上时没有任何弹出。这是我的代码,我从其他地方复制的鼠标悬停事件:

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);

                        }
                    }
                }
            }


        }



    }
}

【问题讨论】:

  • 我会检查您发送的坐标是否正确(x,y),然后如果您的工具提示控件是图表控件的子控件,如果这不起作用,那么您可以尝试已经工作图书馆,我正在研究这个,因为我不喜欢当前的开源选项github.com/beto-rodriguez/Live-Charts
  • Yourc 代码有效,但是..:在进行 Hittest 时,您需要实际命中一个 DataPoint,而不仅仅是两点之间的线!顺便说一句,将工具提示添加到数据点本身时也是如此。所以它不适用于折线图或点图类型。 - 另外:你想看到什么值之间的界限?插值?您可以计算它们,但命中测试对您没有帮助..
  • 你可能想学习this post..

标签: c#


【解决方案1】:

试试这个。

这适用于我的财务(棒、烛台)图表。大多数示例不显示DataPointYValue[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);
            }
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-16
    • 1970-01-01
    • 1970-01-01
    • 2011-04-22
    • 2011-09-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多