' Name: Plot_Area
'
' Title: Plot area time series graph
'
' Topics: Views
'
' Description: Plot data as area under line. AreaPolMax is the full data set, while
'              AreaPolLim is that part of the data under the guideline limit.
'              
' Author: Michael Silberbauer 2001
'
' Institute for Water Quality Studies, Department of Water and Sanitation
' Private Bag X313 PRETORIA South Africa 0001
' Requires:
'
' Self:av.Run( "Plot_Area", { MinX, MaxX, MinY, MaxY, OriginX, OriginY, ScaleX, ScaleY, theVTab, theBitMap, Plotitem, Guideline } )
'
' 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)
Guideline = Self.Get(11)
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 data as a polygon:
' the part exceeding the set guideline is red.

totalBits = theBitMap.GetSize
selBits = theBitMap.Count
AreaPolMax = List.Make
AreaPolLim = List.Make
FirstArea = true
av.ClearMsg
av.ClearStatus
VarLast = -1
DatLast = -1
record = 0
av.ShowMsg("Plotting area graph of"++PlotItem.AsString++"...")
if (selBits>0) then
  VariableYg = OriginY+((Guideline-MinY)*ScaleY)
  for each rec in theBitMap
    record = record+1
    av.SetStatus(100*(record/selBits))
    Variable = theVTab.ReturnValue(theVarField,rec)
    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)
    if (Variable>=0) then
      VariableX = OriginX+((nDate-MinX)*ScaleX)
      DatLastX = OriginX+((DatLast-MinX)*ScaleX)
      VariableY = OriginY+((Variable-MinY)*ScaleY)
      VarYLast = OriginY+((VarLast-MinY)*ScaleY)
      if (FirstArea) then
        AreaPolMax.Add(VariableX@OriginY)
        AreaPolLim.Add(VariableX@OriginY)
        AreaPolMax.Add(VariableX@VariableY)
        if (Variable<=Guideline) then
          AreaPolLim.Add(VariableX@VariableY)
        else
          AreaPolLim.Add(VariableX@VariableYg)
        end
        FirstArea = false
      else
        ' Four possibilities: (y0 is VarYLast, y1 is VariableY, yg is VariableYg, xg is the intermediate coordinate)
        ' 1) y0 <  yg and y1 <  yg

        if ((VarLast<guideline) and (Variable<guideline)) then
          AreaPolMax.Add(VariableX@VariableY)
          AreaPolLim.Add(VariableX@VariableY)
        end
        ' 2) y0 <  yg and y1 >= yg

        if ((VarLast<guideline) and (Variable>=guideline)) then
          VariableXg = ((VariableYg-VarYLast)*((VariableX-DatLastX)/(VariableY-VarYLast)))+DatLastX
          AreaPolMax.Add(VariableX@VariableY)
          AreaPolLim.Add(VariableXg@VariableYg)
          AreaPolLim.Add(VariableX@VariableYg)
        end
        ' 3) y0 >= yg and y1 <  yg

        if ((VarLast>=guideline) and (Variable<guideline)) then
          VariableXg = ((VariableYg-VarYLast)*((VariableX-DatLastX)/(VariableY-VarYLast)))+DatLastX
          AreaPolMax.Add(VariableX@VariableY)
          'AreaPolLim.Add( VariableX@VariableYg )

          AreaPolLim.Add(VariableXg@VariableYg)
          AreaPolLim.Add(VariableX@VariableY)
        end
        ' 4) y0 >= yg and y1 >= yg

        if ((VarLast>=guideline) and (Variable>=guideline)) then
          AreaPolMax.Add(VariableX@VariableY)
          AreaPolLim.Add(VariableX@VariableYg)
        end
      end
      VarLast = Variable
      DatLast = nDate
    end
  end
end
AreaPolMax.Add(VariableX@OriginY)
Point0 = AreaPolMax.Get(0)
AreaPolMax.Add(Point0)
AreaPolLim.Add(VariableX@OriginY)
Point0 = AreaPolLim.Get(0)
AreaPolLim.Add(Point0)
mPol = polygon.Make({AreaPolMax})
gmPol = GraphicShape.Make(mPol)
aSym = RasterFill.Make
aSym.SetColor(Color.GetRed)
aSym.SetOLColor(Color.GetRed)
gmPol.SetSymbol(aSym)
theGraphics.Add(gmPol)
mPol = polygon.Make({AreaPolLim})
gmPol = GraphicShape.Make(mPol)
aSym = RasterFill.Make
aSym.SetColor(Color.GetBlue)
aSym.SetOLColor(Color.GetBlue)
gmPol.SetSymbol(aSym)
theGraphics.Add(gmPol)
theGraphics.EndBatch
return {}