Attribute VB_Name = "Modul4"
Attribute VB_Description = "Zeichnet Diagramme"
'********************************************************************
'* DIAGRAMM.BAS
'* Diverse Functionen zum initialisieren des Diagramms
'* Die Funktionen stammen wahrscheinlich ursprünglich aus dem Buch
'* "MSR mit VB"
'* Letzter Stand : 14.2.2000 , Ingo Gerlach
'* Für mini LogicAnalysator
'***********************************************************************

Option Explicit

Public Type Rectangle
   left As Single
   right As Single
   top As Single
   bottom As Single
End Type

Public Type drawInfoType
  ranges As Rectangle       'Skalenbereich für X und Y
  formatXLabels As String   'Formatstring X-Labels
  formatYLabels As String   'Formatstring Y-Labels
  anzTicksX As Integer      'Anzahl Labels X
  anzTicksY As Integer      'Anzahl Labels Y
  xlog As Integer           '1=log, 0=lin
  ylog As Integer           '1=log, 0=lin
  xLabels As Integer        '1=Labels, 0=keine
  yLabels As Integer        '1=Labels, 0=keine
  title As String           'Beschriftung
End Type


Function CalcWidthXLabels(pictBox As PictureBox, drawInfo As drawInfoType) As Single
' Berechnet die Breite der Beschriftung
Dim delta As Single
Dim tw, twmax As Single
   CalcWidthXLabels = 2 * pictBox.TextHeight("64000")
End Function

Function CalcWidthYLabels(pictBox As PictureBox, drawInfo As drawInfoType) As Single
Dim h, w As Single
Dim delta As Single
Dim twmax  As Single
Dim tw As Single
Dim wert As Single
Dim i As Integer
   h = pictBox.ScaleHeight
   w = pictBox.ScaleWidth
   If drawInfo.ylog = 0 Then
       delta = ((drawInfo.ranges.top) - (drawInfo.ranges.bottom)) / (drawInfo.anzTicksY)
   End If
   If drawInfo.ylog = 1 Then
     delta = (Log10(drawInfo.ranges.top) - Log10(drawInfo.ranges.bottom)) / (drawInfo.anzTicksY)
   End If

   twmax = 0
   For i = 0 To drawInfo.anzTicksY

      If drawInfo.ylog = 0 Then
           wert = (i * delta + (drawInfo.ranges.bottom))
       End If
       If drawInfo.ylog = 1 Then
          wert = Exp10(i * delta + Log10(drawInfo.ranges.bottom))
       End If
    
       tw = pictBox.TextWidth("CH 8"): Rem (Format$(wert, drawInfo.formatYLabels))
       If tw > twmax Then twmax = tw
   Next i
   twmax = twmax * 1.4
   CalcWidthYLabels = twmax
End Function

Sub drawAxis(pictBox As PictureBox, drawInfo As drawInfoType, drawRect As Rectangle, ByVal TimeControl As Boolean)
Dim h, w As Single

' Dim drawRect As Rectangle
Dim xoff, yoff As Single

   pictBox.ForeColor = &H808080
   pictBox.DrawWidth = 1
   h = pictBox.ScaleHeight
   w = pictBox.ScaleWidth
  ' pictBox.Line (0, 0)-(w, h), &HFFFFFF, BF
   xoff = 0.05 * w
   yoff = 0.05 * h
   If drawInfo.xLabels = 1 Then
     yoff = CalcWidthXLabels(pictBox, drawInfo)
   End If
   If drawInfo.yLabels = 1 Then
      xoff = CalcWidthYLabels(pictBox, drawInfo)
   End If
   drawRect.left = xoff
   drawRect.right = w * 0.99
   drawRect.top = h * 0.05
   drawRect.bottom = h - yoff
   pictBox.Line (drawRect.left, drawRect.bottom)-(drawRect.right, drawRect.top), pictBox.BackColor, BF
    
   Rem Werte für Zeichnungsbereich
   DrawArea.left = CInt(drawRect.left) + 5
   DrawArea.right = CInt(drawRect.right) + 5
   DrawArea.top = yoff
   DrawArea.bottom = CInt(drawRect.bottom) + 5
    
   Call DrawXLabels(pictBox, drawRect, drawInfo)
   Call DrawYLabels(pictBox, drawRect, drawInfo, TimeControl)
   pictBox.Line (xoff, h - yoff)-(0.9 * w, h - yoff)
   pictBox.DrawStyle = 0
   pictBox.DrawWidth = 2
   pictBox.Line (drawRect.left, drawRect.top)-(drawRect.right, drawRect.bottom), , B
End Sub

Sub DrawData(pictBox As PictureBox, drawInfo As drawInfoType, drawRect As Rectangle, ByVal xmin As Single, ByVal xmax As Single, y() As Single)
Dim h, w As Single
Dim deltaX As Single
Dim deltaY As Single
Dim dx, dy As Single
Dim werty, wertx As Single
Dim oldx, oldy As Single
Dim i As Integer
Dim xoff, yoff As Single
Dim x As Single
  
 
   h = pictBox.ScaleHeight
   w = pictBox.ScaleWidth
   w = drawRect.right - drawRect.left
   dx = w
   h = drawRect.top - drawRect.bottom
   dy = h

   If drawInfo.xlog = 0 Then
       deltaX = dx / (drawInfo.ranges.right - drawInfo.ranges.left)
   End If
   If drawInfo.xlog = 1 Then
       deltaX = dx / (Log10(drawInfo.ranges.right) - Log10(drawInfo.ranges.left))
   End If
   If drawInfo.ylog = 0 Then
       deltaY = dy / ((drawInfo.ranges.top) - (drawInfo.ranges.bottom))
   End If
   If drawInfo.ylog = 1 Then
     deltaY = dy / (Log10(drawInfo.ranges.top) - Log10(drawInfo.ranges.bottom))
   End If
   
   x = xmin + 0 * (xmax - xmin) / UBound(y, 1)
   If drawInfo.xlog = 0 Then
      wertx = drawRect.left + 1# * (x - drawInfo.ranges.left) * deltaX
   End If
   If drawInfo.xlog = 1 Then
      wertx = drawRect.left + 1# * (Log10(x) - Log10(drawInfo.ranges.left)) * deltaX
   End If

   If drawInfo.ylog = 0 Then
      werty = drawRect.bottom + 1# * (y(0) - drawInfo.ranges.bottom) * deltaY
   End If
   If drawInfo.ylog = 1 Then
      werty = drawRect.bottom + (1# * (Log10(y(0)) - Log10(drawInfo.ranges.bottom))) * deltaY
   End If
   'Primitives Clipping
   If wertx < drawRect.left Then wertx = drawRect.left
   If wertx > drawRect.right Then wertx = drawRect.right
   If werty < drawRect.top Then werty = drawRect.top
   If werty > drawRect.bottom Then werty = drawRect.bottom
   oldx = wertx
   oldy = werty

   For i = 1 To UBound(y, 1)
      x = xmin + i * (xmax - xmin) / UBound(y, 1)
      If drawInfo.xlog = 0 Then
        wertx = drawRect.left + 1# * (x - drawInfo.ranges.left) * deltaX
      End If
      If drawInfo.xlog = 1 Then
        wertx = drawRect.left + 1# * (Log10(x) - Log10(drawInfo.ranges.left)) * deltaX
      End If

      If drawInfo.ylog = 0 Then
        werty = drawRect.bottom + 1# * (y(i) - drawInfo.ranges.bottom) * deltaY
      End If
      If drawInfo.ylog = 1 Then
        werty = drawRect.bottom + 1# * (Log10(y(i)) - Log10(drawInfo.ranges.bottom)) * deltaY
      End If
      'Primitives Clipping
      If wertx < drawRect.left Then wertx = drawRect.left
      If wertx > drawRect.right Then wertx = drawRect.right
      If werty < drawRect.top Then werty = drawRect.top
      If werty > drawRect.bottom Then werty = drawRect.bottom

      pictBox.Line (oldx, oldy)-(wertx, werty)
      oldx = wertx
      oldy = werty
   Next i


End Sub

Sub DrawXLabels(pictBox As PictureBox, drawRect As Rectangle, drawInfo As drawInfoType)
Dim h, w As Single
Dim delta As Single
Dim dx As Single
Dim s As String
Dim wert As Single
Dim i, j As Integer
   h = pictBox.ScaleHeight
   w = pictBox.ScaleWidth
   If drawInfo.xlog = 0 Then
       delta = (drawInfo.ranges.right - drawInfo.ranges.left) / (drawInfo.anzTicksX)
   End If
   If drawInfo.xlog = 1 Then
       delta = (Log10(drawInfo.ranges.right) - Log10(drawInfo.ranges.left)) / (drawInfo.anzTicksX)
   End If

   w = drawRect.right - drawRect.left
   dx = w / (drawInfo.anzTicksX)
   pictBox.DrawStyle = 0
   pictBox.ForeColor = QBColor(14)
   pictBox.CurrentX = w / 2
   pictBox.CurrentY = pictBox.top - pictBox.TextHeight("a")
   pictBox.CurrentX = pictBox.CurrentX - (pictBox.TextWidth(drawInfo.title) / 2)
   pictBox.Print drawInfo.title
   
   For i = 0 To drawInfo.anzTicksX
       pictBox.DrawWidth = 2
       pictBox.CurrentX = drawRect.left + 1# * i * dx
       pictBox.Line (pictBox.CurrentX, drawRect.bottom)-(pictBox.CurrentX, drawRect.top)
       If drawInfo.xlog = 0 Then
          wert = drawInfo.ranges.left + 1# * i * delta
       End If
       If drawInfo.xlog = 1 Then
          wert = Exp10(Log10(drawInfo.ranges.left) + 1# * i * delta)
       End If
       s = Format$(wert, drawInfo.formatXLabels)
       pictBox.CurrentX = pictBox.CurrentX - pictBox.TextWidth(s) / 1.3
       pictBox.CurrentY = h - pictBox.TextHeight("a") * 1.1
       pictBox.ForeColor = &H80FF80
     
       If drawInfo.xLabels = 1 Then pictBox.Print s
       pictBox.ForeColor = &H808080

       If (drawInfo.xlog = 1) And (i >= 0) And (i < drawInfo.anzTicksX) Then
         pictBox.DrawWidth = 1
         For j = 2 To 9
            pictBox.CurrentX = drawRect.left + 1# * (i) * dx + (Log10(1# * j) * dx / 1)
            pictBox.Line (pictBox.CurrentX, drawRect.bottom)-(pictBox.CurrentX, drawRect.top)
          Next j
       End If

     
                           
   Next i
End Sub

Sub DrawYLabels(pictBox As PictureBox, drawRect As Rectangle, drawInfo As drawInfoType, ByVal TimeControl As Boolean)
Dim h, w As Single
Dim delta As Single
Dim dy As Single
Dim wert As Double
Dim i, j, z As Integer
Dim stvar As String

   h = pictBox.ScaleHeight
   w = pictBox.ScaleWidth
   If drawInfo.ylog = 0 Then
       delta = ((drawInfo.ranges.top) - (drawInfo.ranges.bottom)) / (drawInfo.anzTicksY)
   End If
   If drawInfo.ylog = 1 Then
     delta = (Log10(drawInfo.ranges.top) - Log10(drawInfo.ranges.bottom)) / (drawInfo.anzTicksY)
   End If
   h = drawRect.top - drawRect.bottom
   dy = h / (drawInfo.anzTicksY)
   pictBox.DrawStyle = 0
   z = drawInfo.anzTicksY
   For i = 0 To drawInfo.anzTicksY
       pictBox.DrawWidth = 1
       pictBox.CurrentY = drawRect.bottom + 1# * i * dy
       pictBox.Line (drawRect.left, pictBox.CurrentY)-(drawRect.right, pictBox.CurrentY)
       'ChYVal(z) = pictBox.CurrentY
       ChYVal(i) = pictBox.CurrentY
       Rem Debug.Print "Y", ChYVal(z), "CurrentY", pictBox.CurrentY
      
       z = z - 1
       pictBox.CurrentY = pictBox.CurrentY - pictBox.TextHeight("A") / 1
       pictBox.CurrentX = drawRect.left * 0.1
       If drawInfo.ylog = 0 Then
           wert = (i * delta + (drawInfo.ranges.bottom))
           If TimeControl = True Then
            If wert > 24 Then wert = wert - 24
           End If
           Rem Änderung 12.2.2000 f. LA
           stvar = "Ch " + Str(i)
       End If
       If drawInfo.ylog = 1 Then
          wert = Exp10(i * delta + Log10(drawInfo.ranges.bottom))
       End If
       pictBox.ForeColor = &H80FF80
       If i < drawInfo.anzTicksY Then
        If drawInfo.yLabels = 1 Then pictBox.Print stvar: Rem Format$(wert, drawInfo.formatYLabels)
       End If
       pictBox.ForeColor = &H808080
       Rem Letzte Zeile nicht beschriften
       If (drawInfo.ylog = 1) And (i >= 0) And (i < drawInfo.anzTicksY) Then
         pictBox.DrawWidth = 1
         For j = 2 To 9
            pictBox.CurrentY = drawRect.bottom + 1# * (i) * dy + (Log10(1# * j) * dy / 1)
            pictBox.Line (drawRect.left, pictBox.CurrentY)-(drawRect.right, pictBox.CurrentY)
          Next j
       End If


   Next i
End Sub

Function Exp10(x As Single) As Single
   Exp10 = Exp(x * Log(10))
End Function

Function Log10(x As Single) As Single
   If x > 1 Then
      Log10 = Log(x) / Log(10)
   ElseIf (x < 1) And (x > 0) Then
     
      Log10 = -Log(1 / x) / Log(10)
   ElseIf x = 1 Then
      Log10 = 0
  ElseIf x <= 0 Then
      Log10 = -50
   End If


End Function


