3

Can anyone help me to check my code? When my mouse hover to the line graph, it does not pop up tooltip to show the information. Is it anything wrong? Thank you.

My idea is when the mouse move along the line graph, it will pop out a tootip to shpw the x-axis value and y-axis value, but there is nothing pop out when my mouse over the line graph. Here is my code, the mouse over event I copied from somewhere else:

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

                        }
                    }
                }
            }


        }



    }
}
helloworld1234
  • 327
  • 1
  • 8
  • 19
  • I would check if the coordinates you are sending are correct (x,y), then if your tooltip control is a child of your chart control, if that does not work then you can try an already working library, I am working on this because I dont like current open source options https://github.com/beto-rodriguez/Live-Charts – bto.rdz Nov 29 '15 at 04:17
  • Yourc code works but..:When doing a Hittest you need to actually hit a DataPoint, not just the line between two points! The same is true when adding tooltips to the datapoints themselves btw.. So it doesn't work well for line or point chart types. - Also: what values would you want to see for the line between? The interpolated values? You can calculate them but a hittest won't help you.. – TaW Nov 29 '15 at 15:36
  • You may want to study [this post](http://stackoverflow.com/questions/33899354/get-y-value-of-series-from-the-x-cursor-position-where-a-chartarea-is-clicked-on/33905483?s=1|0.1013#33905483).. – TaW Nov 29 '15 at 23:03

1 Answers1

3

try this.

It's work for my financial (stick, candlestick) charts. Show not YValue[0] of DataPoint as most of the examples, but YValue of axis Y.

    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);
            }
        }
    }
Evgeny Ivanov
  • 504
  • 6
  • 14