Menu

FindNearestPoint usage example

Help
Stephen
2004-11-11
2012-09-19
  • Stephen

    Stephen - 2004-11-11

    Hello,

    First off, thank you for this library and the work you keep putting into it.

    Is there any sample code on how to use FindNearestPoint ?

    I have a date graph (stock quotes) and want to be able to click or hover for details.

    Thank you.

     
    • John Champion

      John Champion - 2004-11-11

      Hi,
      Here's an example for using FindNearestPoint() that should give the general idea:

      private void Form1_MouseDown( object sender, System.Windows.Forms.MouseEventArgs e )
      {
      CurveItem curve;
      int iPt;

      if ( myPane.FindNearestPoint( new PointF( e.X, e.Y ), out curve, out iPt ) )
      MessageBox.Show( String.Format( "label = {0} X = {1}", curve.Label, curve.Points[iPt].ToString("e2") ) );
      else
      MessageBox.Show( "No Point Found" );
      }

      John

       
    • Stephen

      Stephen - 2004-11-11

      Ok, I expanded this a bit but need a little help.

      Instead of a MessageBox, I'm trying to put a label. I got it to work correctly to show when there is no point found, but I can't figure out how to associate the coordinates of the point with a new label I am creating.

      Code I am starting with, the "You did not click on a point" message works fine.

          ZedGraph.TextItem mousePosNoFound = null;
      
          private void TrackGraph3_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
          {
              CurveItem curve; 
              int iPt;
      
              // Check if the first time we got a click.
              if (mousePosNoFound == null)
              {
                  // Create the label we will update each non-point click.
                  mousePosNoFound = new TextItem("Click point for detail", 0.01F, 0.02F);
                  mousePosNoFound.CoordinateFrame = ZedGraph.CoordType.AxisFraction;
                  mousePosNoFound.AlignH = ZedGraph.AlignH.Left;
                  mousePosNoFound.AlignV = ZedGraph.AlignV.Top;
                  mousePosNoFound.FontSpec.Size = 10;
                  myPane.TextList.Add(mousePosNoFound);
              }
      
              bool didFindCurve = myPane.FindNearestPoint( new PointF( e.X, e.Y ), out curve, out iPt );
              if ( didFindCurve ) 
              {
                  // Stuck here
                  // How to I create a TextItem that is at the same coordinates as the point that was clicked?
      
      
                  // remove the no found point message.
                  mousePosNoFound.Text = "";
              }
              else
              {
                  // Inform user they did not click on a point.
                  string outText1 = "No Point Found: x: " + e.X.ToString() + " y: " + e.Y.ToString(); 
                  mousePosNoFound.Text = outText1;
              }
      
              // Force Redraw
              TrackGraph3_Resize("dummyvar", e);
          }
      
       
      • John Champion

        John Champion - 2004-11-11

        Try this for the didFindCurve branch:

        mousePosNoFound.Text = curve[iPt].ToString();
        mousePosNoFound.X = curve[iPt].X;
        mousePosNoFound.Y = curve[iPt].Y;
        mousePosNoFound.CoordinateFrame = CoordType.AxisXYScale;
        Invalidate();

        You can add a little to the X or Y coordinate to offset the text from the data point (and also use AlignH & AlignV).

        I previously mentioned the TextItem.Location class -- sorry to mislead you, that's actually in the new version (CVS).
        John

         
        • Stephen

          Stephen - 2004-11-12

          Getting close!

          I had to cast these:

                              newPosition2.X = (float)curve[iPt].X;
                              newPosition2.Y = (float)curve[iPt].Y;
          

          But the X value seems to be rounded or something. The Y is perfect, but all the labels appear on the same X lines (rounding?) instead of where the points are. Any idea?

           
          • John Champion

            John Champion - 2004-11-12

            Is this a for a bar chart, where each bar within a cluster has the same X value? If so, you would need to user CurveItem.Bar.CalcBarCenter to get the actual pixel position of the bar. This is starting to get into the ZedGraph internal interface, which is probably best avoided since it will change from time to time.

            Alternatively, you can use GraphPane.ReverseTransform to convert a mouse pt (from the mouse click) into a X,Y user coordinate. (even if you use CalcBarCenter, you would still need to use ReverseTransform to convert the pixel position to a user scale position).

            If it's not a bar chart or if this isn't the problem, you can post your code in the support forum and I'll take a look at it.
            John

             
    • Stephen

      Stephen - 2004-11-11

      Thank you, will give this a try.

       

Log in to post a comment.

MongoDB Logo MongoDB