' Name: Plot_Stick
'
' Title: Plot stick time-series graphs (thin bars)
'
' Topics: Views
'
' Description: Plot data as vertical thick lines ("sticks") colour-coded
'              according to compliance with guidelines.
'              
' Author: Michael Silberbauer 2001
' Institute for Water Quality Studies, Department of Water and Sanitation
' Private Bag X313 PRETORIA South Africa 0001
' Updated 2001-05-04
' Requires:
'
' Self:av.Run( "Plot_Stick", { MinX, MaxX, MinY, MaxY, OriginX, OriginY, ScaleX, ScaleY, theVTab, theBitMap, PlotItem, Guideline, ColList } )
'
' Results:
' -----------------------------------------------------------------------------

MinX = Self.Get(0)
MaxX = Self.Get(1)
MinY = Self.Get(2)
MaxY = Self.Get(3)
OriginX = Self.Get(4)
OriginY = Self.Get(5)
ScaleX = Self.Get(6)
ScaleY = Self.Get(7)
theVTab = Self.Get(8)
theBitMap = Self.Get(9)
PlotItem = Self.Get(10)
Guidelines = Self.Get(11)
ColList = Self.Get(12)
nib = 1
theProject = av.GetProject
theView = av.GetActiveDoc
theGraphics = theView.GetGraphics
theDisplay = theView.GetDisplay
theVarField = theVTab.FindField(PlotItem.AsString)
the_nDateField = theVTab.FindField("Ndate")
theDateField = theVTab.FindField("Date")
theTimeField = theVTab.FindField("Time")

' Plot each data point as a vertical bar:
' the colour of each segment depends on the water quality guidelines.

totalBits = theBitMap.GetSize
selBits = theBitMap.Count
av.ClearMsg
av.ClearStatus
record = 0
'if nothing to plot, don't go on:

if (selBits<=0) then
  return {}
end
av.ShowMsg("Plotting graph of"++PlotItem.AsString++"...")

'Work out the y-values of each guideline:

Y_List = List.Make
for each Guideline in Guidelines
  VariableYg = OriginY+((Guideline-MinY)*ScaleY)
  Y_List.Add(VariableYg)
end
for each rec in theBitMap
  record = record+1
  av.SetStatus(100*(record/selBits))
  Guidelast = 0
  nGuide = 0
  for each Guideline in Guidelines
  ' calculate X

    TimeText = theVTab.ReturnValue(theTimeField,rec)
    nDate = theVTab.ReturnValue(theDateField,rec)
    nDate = nDate.SetFormat("J").AsString.AsNumber
    hTime = (TimeText.AsNumber/100).Round
    mTime = (TimeText.AsNumber/100)-hTime
    mTime = mTime/60
    nDate = nDate+((hTime+mTime)/24)
    VariableX = OriginX+((nDate-MinX)*ScaleX)
    
    ' read Y

    Variable = theVTab.ReturnValue(theVarField,rec)
    ' if Y is less than Guidelast, break out of the Guideline loop:

    if (Variable<Guidelast) then
      'break

    end
    ' calculate Y

    VariableY = OriginY+((Variable-MinY)*ScaleY)
    GuidelastY = OriginY+((Guidelast-MinY)*ScaleY)
    ' if Y lies between Guidelast and Guideline, join Guidelast and Y, then break out of the loop

    if ((Variable>=Guidelast) and (Variable<Guideline)) then
      aLine = Line.Make(VariableX@GuidelastY,VariableX@VariableY)
      gLine = GraphicShape.Make(aLine)
      FatLine = BasicPen.Make
      FatLine.SetSize(nib)
      FatLine.SetColor(ColList.Get(nGuide))
      aSymbol = gLine.SetSymbol(FatLine)
      theGraphics.AddBatch(gLine)
      'break

    end
    'if Y is bigger than Guideline, draw a line from Guidelast to Guideline

    if (Variable>=Guideline) then
      aLine = Line.Make(VariableX@GuidelastY,VariableX@Y_List.Get(nGuide))
      gLine = GraphicShape.Make(aLine)
      FatLine = BasicPen.Make
      FatLine.SetSize(nib)
      FatLine.SetColor(ColList.Get(nGuide))
      aSymbol = gLine.SetSymbol(FatLine)
      theGraphics.AddBatch(gLine)
    end
    'increase Guidelast to current Guideline

    Guidelast = Guideline
    nG = nGuide
    nGuide = nGuide+1
  end
  'if Y is still bigger than the biggest Guideline, join Guidelast to Y

  if (Variable>=Guidelast) then
    GuidelastY = OriginY+((Guidelast-MinY)*ScaleY)
    aLine = Line.Make(VariableX@GuidelastY,VariableX@VariableY)
    gLine = GraphicShape.Make(aLine)
    FatLine = BasicPen.Make
    FatLine.SetSize(nib)
    FatLine.SetColor(ColList.Get(nGuide))
    aSymbol = gLine.SetSymbol(FatLine)
    theGraphics.AddBatch(gLine)
  end
end
theGraphics.EndBatch
return {}