ZedGraph - How to make a horizontal line drag and drop?

I have straight horizontal lines that I want the user to be able to drag vertically. How is this possible? I think the best option for choosing a line would be a fixed number of pixels next to the line. Therefore, if the mouse is +/- 2 pixels, I have to change the mouse cursor and make the line draggable. I see that the CurveItem class has IsSelectable and IsSelected properties. Will they have any functions in solving this problem? I cannot understand why they are intended for reading class documentation.


EDIT:

It seems that FindNearestPoint (and FindNearestObject ) only searches for actual points. How will I choose to work along the entire straight line? I think I will need to make my own regular β€œFind” procedure, which will go through all the lines that I want to check, and for each calculate its imaginary point Y based on the position of the mouse X ( ? ) I also think about the slant lines for this goals, for horizontal / vertical lines it will be a little easier. At least it seems to be necessary for the curve, but I suppose the same thing needs to be done to select (in the middle of the section) LineObj?

I really did not know about the existence of LineObj . It seems that LineObj cannot change the coordinates of X2 / Y2 , since they are ReadOnly . So is it possible to drag point X2 / Y2 to LineObj in general?


EDIT 2:

There seems to be a problem with FindNearestPoint on the JapaneseCandleStick chart; When I click on the graph panel, it does not return the index of the nearest bar, but I believe that instead it selects the index with the nearest Y value, no matter how far from the x axis it is. Sometimes it is a bar to the right of the mouse, sometimes to the left of the mouse. So should it work?

I made this custom function myself, so I think everything is fine. Nevertheless, it would be nice to understand why FindNearestPoint acts this way.

This is the mouseDown code:

   ' Find nearest curve point:
   Dim ciNearestCurve As CurveItem
   Dim iNearestCurve As Integer
   Dim b As Boolean = zgc.GraphPane.FindNearestPoint(mousePt, zgc.GraphPane.CurveList, ciNearestCurve, iNearestCurve)
   If b Then
       With ciNearestCurve(iNearestCurve)
           Debug.Print(Date.FromOADate(.X) & " " & .Y & " " & .Z)
       End With
+3
2

.

LineObj , FindNearestObject.

, " " , .

:
- MouseDown, MouseUp MouseMove
- MouseDown, /, , - ,


:
, myCurve, . FindNearestPoint, , .

, :

// mousePt is where you clicked
CurveItem nearestCurve = null;
int nearestID = -1;

myPane.FindNearestPoint(mousePt, out nearestCurve, out nearestID);
if(nearestCurve!=null)
   // remember the curve somewhere. 

MouseMove MouseUp, , . Y ( Y2), , , , X.

, (dy), :

for(int i=0; i<nearestCurve.Points.Count; i++)
    nearestCurve.Points[i].Y += dy;

, LineObj.Location.Y2 :

, Y2 Y.

Width/Height , .

+1

-, bretddog:

, FindNearestPoint JapaneseCandleStick; , , , Y, , x. , . ?

, , , . , FindNearestPoint .

JapaneseCandleStick, Line, , . ZedGraph , , , "" , .

, . , , . :

''' <summary>
''' To obtain the nearest curve and its index on ZedGraph stick
''' </summary>
''' <param name="GraphPane">The graphpane on wich you are working</param>
''' <param name="PointLocation">Mouse location</param>
''' <param name="NearestCurve">Reference of the nearest curve</param>
''' <param name="NearestCurveIndex">Index of the nearest curve</param>
''' <returns>True if a curve is found</returns>
''' <remarks></remarks>
Private Function FindNearestCurve(ByVal GraphPane As ZedGraph.GraphPane, ByVal PointLocation As System.Drawing.Point, ByRef NearestCurve As CurveItem, ByRef NearestCurveIndex As Integer) As Boolean
    Try
        Dim MinDist As Double = -1 'error if < 0
        Dim DistTemp As Double
        Dim a, b As Double
        Dim Curve As CurveItem
        Dim ValX, ValY As Double
        Dim NormX, NormY As Double

        'ini
        NearestCurveIndex = -1
        GraphPane.ReverseTransform(PointLocation, ValX, ValY) 'To use real values
        NormX = GraphPane.XAxis.Scale.Max - GraphPane.XAxis.Scale.Min 'To normalize value when we haven't orthonormal axis
        NormY = GraphPane.YAxis.Scale.Max - GraphPane.YAxis.Scale.Min 'To normalize value when we haven't orthonormal axis

        'We looking for the nearest curve
        For j = 0 To GraphPane.CurveList.Count - 1
            Curve = GraphPane.CurveList.Item(j)
            If Curve.IsVisible = True Then
                'We generate all coefficient (a and b) of straight line interpolation (equation y=ax+b)
                For i = 0 To Curve.NPts - 2 '-2 because we work on intervals
                    'we check if interval is close to the point (to prevent case where the complete interpolation curve is the nearest curve but the real segment is far to the point)
                    If (Curve.Points.Item(i + 1).Y >= ValY And Curve.Points.Item(i).Y <= ValY) Or
                            (Curve.Points.Item(i + 1).Y <= ValY And Curve.Points.Item(i).Y >= ValY) Or
                            (Curve.Points.Item(i + 1).X >= ValX And Curve.Points.Item(i).X <= ValX) Or
                            (Curve.Points.Item(i + 1).X <= ValX And Curve.Points.Item(i).X >= ValX) Then

                        'We calculate straight line interpolation coefficient a and b
                        'Vertical line case
                        If (Curve.Points.Item(i + 1).X / NormX - Curve.Points.Item(i).X / NormX) = 0 Then
                            'We calculate directly the distance
                            DistTemp = Math.Abs(Curve.Points.Item(i).X / NormX - ValX / NormX)
                        Else 'All other case
                            'a = (yi+1 - yi) / (xi+1 - xi)
                            a = (Curve.Points.Item(i + 1).Y / NormY - Curve.Points.Item(i).Y / NormY) / (Curve.Points.Item(i + 1).X / NormX - Curve.Points.Item(i).X / NormX)
                            'b = yi - a*xi
                            b = Curve.Points.Item(i).Y / NormY - a * Curve.Points.Item(i).X / NormX
                            'We calculate the minimum distance between the point and all straight line interpolation
                            DistTemp = Math.Abs(a * ValX / NormX - ValY / NormY + b) / Math.Sqrt(1 + a * a)
                        End If
                        'We test if it the minimum and save corresponding curve
                        If MinDist = -1 Then
                            MinDist = DistTemp 'first time
                            NearestCurveIndex = j
                        ElseIf DistTemp < MinDist Then
                            MinDist = DistTemp
                            NearestCurveIndex = j
                        End If
                    End If
                Next
            End If
        Next

        'Return the result
        If NearestCurveIndex >= 0 And NearestCurveIndex < GraphPane.CurveList.Count Then
            NearestCurve = GraphPane.CurveList.Item(NearestCurveIndex)
            Return True
        Else
            NearestCurve = Nothing
            NearestCurveIndex = -1
            Return False
        End If

    Catch ex As Exception
        NearestCurve = Nothing
        NearestCurveIndex = -1
        Return False
    End Try
End Function

, , , , (, / , ). :

  • , , If Curve.IsVisible = True ;
  • X, Y , ;
  • , False NearestCurve = Nothing NearestCurveIndex = -1;
  • , , ( , ), LineObj;
  • , , , ( - , ). , ( ).

, , , . ZedGraph (a b) Add, ( ).

, , ZedGraph.

+1

Source: https://habr.com/ru/post/1767339/


All Articles