Setting line ends to series point and Annotate tool textbox

TeeChart for ActiveX, COM and ASP
Post Reply
tirby
Newbie
Newbie
Posts: 84
Joined: Mon Mar 16, 2009 12:00 am

Setting line ends to series point and Annotate tool textbox

Post by tirby » Thu Mar 07, 2013 3:02 pm

Hi!

I am using an AnnotateTool and a DrawLine tool on the TChart.
Both items are created at runtime on startup with an index of 0.

I have a push button that toggles a boolean variable EventMode.
I also have an EventCount variable.

When EventMode=true, the user can click a point on a series trace.
The Annotate texbox is created, and displays the value of the EventCount.
A DrawLine also is created and drawn from the series trace point that was clicked to the bottom of the Annotate textbox.
Once all the above is completed, the EventMode is then automatically set back to false.

The user can add an additional Event by repeating the above tasks, to create as many "Events" as he likes.

Anytime from this point on the user may hold down the Shift key and use the mouse to reposition the Annotate text box.
The DrawLine will stay attached to both the series trace point and the bottom of the Annotate text box.
The user may also elect to delete a single Annotate/DrawLine pair or all of them at any time.
The user may not grab a hold of the DrawLine and move it.

This would work sort-of like the way the Marks function of the series works.

I'm having a great deal of difficulty bringing this to fruition.
The main problem I'm having is getting the DrawLine ends attached to the appropiate locations.

Any help would be greatly apreciated!
Thanks!

tirby
Newbie
Newbie
Posts: 84
Joined: Mon Mar 16, 2009 12:00 am

Re: Setting line ends to series XY point and textbox

Post by tirby » Fri Mar 08, 2013 3:14 pm

Hi everyone!

I switched out the Annotate and DrawLine tools for a Textbox and a Line.

Here is some code and explanation:

At this point the Textbox and Line have been created.
When the mouse button is down on the TChart - On a series:
This routine captures the SeriesIndex and the ValuIndex of the selected point of the series.

Code: Select all

Private Sub TChart1_OnClickSeries(ByVal SeriesIndex As Long, ByVal ValueIndex As Long, ByVal Button As TeeChart.EMouseButton, ByVal Shift As TeeChart.EShiftState, ByVal x As Long, ByVal y As Long)

    SeriesSelectedIndex = SeriesIndex
    SeriesSelectedValueIndex = ValueIndex
    
End Sub
This next bit of code sets the location of the textbox and the X2Y2 end of the Line.
At this point, I have set the point with some arbitrary number (x2=1500, y2=3000).
They need to be set to the XY value of the selected point on the series.

Code: Select all

Private Sub TChart1_OnMouseUp(ByVal Button As TeeChart.EMouseButton, ByVal Shift As TeeChart.EShiftState, ByVal x As Long, ByVal y As Long)

    Load Text1(grfEventID)
    Text1(grfEventID).Text = Trim$(Str$(grfEventID))
       
    Text1(grfEventID).Left = GraphMouseX
    Text1(grfEventID).Top = GraphMouseY
    Text1(grfEventID).Visible = True
        
    Load Line1(grfEventID)
    Line1(grfEventID).x2 = 1500 'TChart1.Series(SeriesSelected).XValues.Value(SeriesSelectedValueIndex)
    Line1(grfEventID).y2 = 3000 'TChart1.Series(SeriesSelected).YValues.Value(SeriesSelectedValueIndex)
        
    Line1(grfEventID).Visible = True

End Sub
This code sets the XY to use for positioning the Textbox.

Code: Select all

Private Sub TChart1_OnMouseMove(ByVal Shift As TeeChart.EShiftState, ByVal x As Long, ByVal y As Long)

    GraphMouseX = TChart1.Series(0).XScreenToValue(x)
    GraphMouseY = TChart1.Series(0).YScreenToValue(y)        

End Sub
And this section of code sets the X1Y1 of the line to the Textbox

Code: Select all

Private Sub Text1_MouseDown(Index As Integer, Button As Integer, Shift As Integer, x As Single, y As Single)

    Line1(Index).x1 = Text1(Index).Left
    Line1(Index).y1 = Text1(Index).Top
    
End Sub
How can I tie the Line1.x2 and Line1.y2 to selected series XY values?

Yeray
Site Admin
Site Admin
Posts: 9612
Joined: Tue Dec 05, 2006 12:00 am
Location: Girona, Catalonia
Contact:

Re: Setting line ends to series point and Annotate tool textbox

Post by Yeray » Fri Mar 08, 2013 5:04 pm

Hello,

I started an example with Annotation and a DrawLine tools before you replied. Here it is what I have, if it helps:

Code: Select all

Dim EventMode As Boolean
Dim EventCount As Integer
Dim annotSelected As Integer

Private Sub Form_Load()  
  TChart1.Aspect.View3D = False

  TChart1.AddSeries scPoint
  TChart1.Series(0).FillSampleValues
  
  annotSelected = -1
  EventCount = 0
  EventMode = True
  Button1.Caption = "Toggle EventMode"
  Label1.Caption = "EventMode =" + Str$(EventMode)
End Sub

Private Sub Button1_Click()
  EventMode = Not EventMode
  Label1.Caption = "EventMode =" + Str$(EventMode)
End Sub

Private Sub TChart1_OnMouseDown(ByVal Button As TeeChart.EMouseButton, ByVal Shift As TeeChart.EShiftState, ByVal X As Long, ByVal Y As Long)
  Dim valueIndex As Integer
  valueIndex = TChart1.Series(0).Clicked(X, Y)
  If EventMode And valueIndex > -1 Then
    If annotSelected > -1 Then
      TChart1.Tools.Items(annotSelected).asAnnotation.Shape.Pen.Color = vbBlack
      TChart1.Tools.Items(annotSelected + 1).asDrawLine.Lines.Items(0).Pen.Color = vbBlack
    End If
    annot = TChart1.Tools.Add(tcAnnotate)
    dline = TChart1.Tools.Add(tcDrawLine)
    annotSelected = annot
    With TChart1.Tools.Items(annot).asAnnotation
      .Text = Str$(EventCount)
      .Left = X - (.Bounds.Right - .Bounds.Left) / 2
      .Top = Y - 50
      .Shape.Pen.Color = vbRed
    End With
      
    Dim XStart, YStart, XEnd, YEnd As Double
    XStart = TChart1.Series(0).XValues.Value(valueIndex)
    YStart = TChart1.Series(0).YValues.Value(valueIndex)
    With TChart1.Tools.Items(annot).asAnnotation.Bounds
      XEnd = TChart1.Axis.Bottom.CalcPosPoint(.Left + (.Right - .Left) / 2)
      YEnd = TChart1.Axis.Left.CalcPosPoint(.Bottom)
    End With
    With TChart1.Tools.Items(dline).asDrawLine
      .EnableDraw = False
      .EnableSelect = False
      .AddLine XStart, YStart, XEnd, YEnd
      .Lines.Items(0).Pen.Color = vbRed
    End With
      
    EventCount = EventCount + 1
  Else
    Dim i As Integer
    For i = 0 To TChart1.Tools.Count - 1
      If (i Mod 2 = 0) Then 'is annotation
        If (X >= TChart1.Tools.Items(i).asAnnotation.Bounds.Left) And _
           (X <= TChart1.Tools.Items(i).asAnnotation.Bounds.Right) And _
           (Y >= TChart1.Tools.Items(i).asAnnotation.Bounds.Top) And _
           (Y <= TChart1.Tools.Items(i).asAnnotation.Bounds.Bottom) Then
          TChart1.Tools.Items(annotSelected).asAnnotation.Shape.Pen.Color = vbBlack
          TChart1.Tools.Items(annotSelected + 1).asDrawLine.Lines.Items(0).Pen.Color = vbBlack
          annotSelected = i
          TChart1.Tools.Items(annotSelected).asAnnotation.Shape.Pen.Color = vbRed
          TChart1.Tools.Items(annotSelected + 1).asDrawLine.Lines.Items(0).Pen.Color = vbRed
        Else
          If TChart1.Tools.Items(i + 1).asDrawLine.Clicked(X, Y) > -1 Then
            TChart1.Tools.Items(annotSelected).asAnnotation.Shape.Pen.Color = vbBlack
            TChart1.Tools.Items(annotSelected + 1).asDrawLine.Lines.Items(0).Pen.Color = vbBlack
            annotSelected = i
            TChart1.Tools.Items(annotSelected).asAnnotation.Shape.Pen.Color = vbRed
            TChart1.Tools.Items(annotSelected + 1).asDrawLine.Lines.Items(0).Pen.Color = vbRed
          End If
        End If
      End If
    Next i
  End If
End Sub

Private Sub TChart1_OnMouseMove(ByVal Shift As TeeChart.EShiftState, ByVal X As Long, ByVal Y As Long)
  Dim pointer As Integer
  pointer = vbDefault
  If EventMode And TChart1.Series(0).Clicked(X, Y) > -1 Then
    pointer = vbCrosshair
  Else
    Dim i, tmp As Integer
    For i = 0 To TChart1.Tools.Count - 1
      If (i Mod 2 = 0) Then 'is annotation
        If (X >= TChart1.Tools.Items(i).asAnnotation.Bounds.Left) And _
           (X <= TChart1.Tools.Items(i).asAnnotation.Bounds.Right) And _
           (Y >= TChart1.Tools.Items(i).asAnnotation.Bounds.Top) And _
           (Y <= TChart1.Tools.Items(i).asAnnotation.Bounds.Bottom) Then
          pointer = vbCrosshair
        Else
          If TChart1.Tools.Items(i + 1).asDrawLine.Clicked(X, Y) > -1 Then
            pointer = vbCrosshair
          End If
        End If
      End If
    Next i
  End If
  Screen.MousePointer = pointer
End Sub
Best Regards,
ImageYeray Alonso
Development & Support
Steema Software
Av. Montilivi 33, 17003 Girona, Catalonia (SP)
Image Image Image Image Image Image Please read our Bug Fixing Policy

tirby
Newbie
Newbie
Posts: 84
Joined: Mon Mar 16, 2009 12:00 am

Re: Setting line ends to series point and Annotate tool textbox

Post by tirby » Fri Mar 08, 2013 8:48 pm

Yeray,
Thanks,

I tried the example you sent.
The point at which the point line is attached always goes to a data point.
It really needs to attach itself based on the time line rather than on a data point.
Also, i'm using series scLine, not scPoint.

Example:
2 data points:
x=1, y=40
x=4, y=40
A blue lin is drawn between the two points.
The user clicks at x=2,y=40
The DrawLine needs to anchor itself to the blue line at x=2,y=40.
The Annotate textbox should be some distance above the blue line.
I also need to be able to move the annotate textbox to a different position.

I'm not opposed to abandoning the Textbox-Line method I've started.
So, if this will do the trick I'm all for it!

Thanks

tirby
Newbie
Newbie
Posts: 84
Joined: Mon Mar 16, 2009 12:00 am

Re: Setting line ends to series point and Annotate tool textbox

Post by tirby » Sun Mar 10, 2013 11:04 pm

Yeray,

Thanks again for the code sample!
From it I was able to figure out how to do what I was trying to accomplish.

I did end up using the Annotate and DrawLine tools.
Once I understood how the indexing works on the tools, I was able to write a routine in the resize method of the form.
I have a small project that I made to work out these issues. I can post it if you like, just let me know!

Thanks again!

Yeray
Site Admin
Site Admin
Posts: 9612
Joined: Tue Dec 05, 2006 12:00 am
Location: Girona, Catalonia
Contact:

Re: Setting line ends to series point and Annotate tool textbox

Post by Yeray » Mon Mar 11, 2013 10:31 am

Hello,

I'm glad to hear you found how to make it work as you required, and I'm also glad to hear the code I posted helped you to do so.
Of course, we'll be happy to see your code if it's small and general enough for other users' benefit.

Don't hesitate to let us know if you find any other problem
Best Regards,
ImageYeray Alonso
Development & Support
Steema Software
Av. Montilivi 33, 17003 Girona, Catalonia (SP)
Image Image Image Image Image Image Please read our Bug Fixing Policy

Post Reply