' Name: Plot
'
' Title: Plot water quality data time series (called by Plot_Menu)
'
' Topics: Views
'
' Description: Reads chemical data from an INFO file,
'              and plots graphs
'              Institute for Water Quality Studies, Department of Water and Sanitation
'              Private Bag X313 PRETORIA South Africa 0001
'              Initial idea from:
'              Palle Due Larsen in "Graphics the hard way: Time Series in Avenue"
'              at http://www.esri.com/library/userconf/proc96/TO250/PAP246/P246.HTM
'
' Author: Michael Silberbauer 2001-04-25
'
' Requires: Plot_Menu prompts for the input data and calls Plot
'           av.Run( "Plot", { theVTab, Station, PlotItem, Date1, Date2, TypeList, GraphType, vType, Ymax_set, ColList, Guidelines } )
' Self:
'
' Results:
' -----------------------------------------------------------------------------

theVTab = Self.Get(0)
Station = Self.Get(1)
PlotItem = Self.Get(2)
Date1 = Self.Get(3)
Date2 = Self.Get(4)
TypeList = Self.Get(5)
GraphType = Self.Get(6)
vType = Self.Get(7)
Ymax_set = Self.Get(8)
ColList = Self.Get(9)
Guidelines = Self.Get(10)
MinX = Date1
MinX.SetFormat("J")
MinX = MinX.AsString.AsNumber
MaxX = Date2
MaxX.SetFormat("J")
MaxX = 1+MaxX.AsString.AsNumber
DrawStick = false
DrawArea = false
DrawRect = false
if ((GraphType=TypeList.Get(0)) or (GraphType=nil)) then
  DrawStick = true
end
if (GraphType=TypeList.Get(1)) then
  DrawArea = true
end
if (GraphType=TypeList.Get(2)) then
  if (MaxX-MinX<93) then
    DrawRect = true
  else
    ' Rectangles don't work if too thin...

    DrawStick = true
  end
end
theVarField = theVTab.FindField(PlotItem.AsString)
the_nDateField = theVTab.FindField("Ndate")
theDateField = theVTab.FindField("Date")
theTimeField = theVTab.FindField("Time")
theBitMap = theVTab.GetSelection
Date1.SetFormat("yyyyMMdd")
Date2.SetFormat("yyyyMMdd")
expr = "( ( [Station] ="++Station.AsString.Quote++") and ( [Date] >="++Date1.AsString+".AsDate ) and ( [Date] <="++Date2.AsString+".AsDate ))"
theVTab.Query(expr,theBitMap,#VTAB_SELTYPE_NEW)
theVTab.UpdateSelection
totalBits = theBitMap.GetSize
selBits = theBitMap.Count
Date1.SetFormat("yyyy-MM-dd")
Date2.SetFormat("yyyy-MM-dd")
ViewName = Station.AsString+"_"+PlotItem.AsString+"_"+_UseType+"_"+Date1.AsString+"_"+Date2.AsString+"_"+GraphType+vType.AsString
myDoc = av.GetProject.FindDoc(Viewname)
if (myDoc<>nil) then
  'if( MsgBox.MiniYesNo ("Delete"++ViewName+"?", TRUE ) ) then

  av.GetProject.RemoveDoc(myDoc)
  'end

end
Date1.SetFormat("yyyyMMdd")
Date2.SetFormat("yyyyMMdd")
theView = View.Make
theView.SetName(ViewName)
theView.SetTOCWidth(0)
ScreenWidth = System.ReturnScreenSizePixels.GetX
ScreenHeight = System.ReturnScreenSizePixels.GetY
WinWidth = 0.9*ScreenWidth
WinHeight = 0.2*WinWidth
aWindow = theView.GetWin
aWindow.Resize(WinWidth,WinHeight)
aWindow.Activate
theGraphics = theView.GetGraphics
theDisplay = theView.GetDisplay
' Return the extent (the enclosing rectangle) of the View:

theMapExtent = theView.GetDisplay.ReturnVisExtent

' Extract the dimensions of the View from the extent:
'+----------------------------+x2m,y2m
'|                            |
'|                            |
'|                            |
'|                            |Height
'|                            |
'|                            |
'|                            |
'+----------------------------+
'x1m,y1m        Width

theLowerLeft = theMapExtent.ReturnOrigin
x1m = theLowerLeft.Getx
y1m = theLowerLeft.Gety
Width = theMapExtent.GetWidth
Height = theMapExtent.GetHeight
x2m = x1m+Width
xmm = x1m+(Width/2)
y2m = y1m+Height
ymm = y1m+(Height*0.95)
xlm = x1m+(Width*0.05)
ylm = y1m+(Height/2)
theUpperRight = x2m@y2m
theUpperMiddle = xmm@ymm
theMiddleLeft = xlm@ylm
OriginX = x1m+(Width*0.05)
OriginY = y1m+(Height*0.15)
XaxisLength = Width*0.9
YAxisLength = Height*0.8
yTickLength = XaxisLength*0.01

' Fill the plotting area with a white patch:

aRect = Rect.MakeXY(x1m,y1m,x2m,y2m)
gRect = GraphicShape.Make(aRect)
aSymbol = RasterFill.Make
aSymbol.SetColor(Color.GetWhite)
aSymbol.SetOLColor(Color.GetBlack)
gRect.SetSymbol(aSymbol)
theGraphics.Add(gRect)
if (selBits=0) then
  av.ShowMsg(Station.AsString++Date1.AsString++Date2.AsString++"NO SELECTION POSSIBLE")
  return {}
end
av.ShowStopButton
av.ShowMsg("Seeking graph limits for"++PlotItem.AsString++"...")
FoundRecord = false
'MinX = 9999999999
'MaxX = -9999999999

MinY = 9999999999
MaxY = -9999999999
record = 0
if (selBits>0) then
  for each rec in theBitMap
    record = record+1
    av.SetStatus(100*(record/selBits))
    Variable = theVTab.ReturnValue(theVarField,rec)
    nDate = theVTab.ReturnValue(the_nDateField,rec)
    if (Variable>=0) then
      FoundRecord = true
      if (MaxY<Variable) then
        MaxY = Variable
      end
      if (MinY>Variable) then
        MinY = Variable
      end
      if (MaxX<nDate) then
        'MaxX = nDate

      end
      if (MinX>nDate) then
        'MinX = nDate

      end
    end
  end
end
if (FoundRecord.Not) then
  av.ShowMsg("MinX="++MinX.SetFormat("d").AsString++"MaxX="++MaxX.SetFormat("d").AsString++"MinY="++MinY.AsString++"MaxY="++MaxY.AsString++"Graph Limits Problem?")
  return {}
end

'Override calculated Y-range in certain cases:

MinY = 0
if (PlotItem.AsString="Ph") then
  PlotItem = "pH"
  MaxY = 14
end
if (Ymax_set.AsString<>"Auto") then
  MaxY = Ymax_set.AsNumber
  ' In the case of pH, we will allow values from 0 to 14:

  if (PlotItem.AsString="Ph") then
    PlotItem = "pH"
    MaxY = 14
  end
end
ScaleX = XaxisLength/(MaxX-MinX)
ScaleY = YaxisLength/(MaxY-MinY)
xTickLength = (MaxY-MinY)/25
xax1 = OriginX+((MinX-MinX)*ScaleX)
xax2 = OriginX+((MaxX-MinX)*ScaleX)
yax1 = OriginY+((MinY-MinY)*ScaleY)
yax2 = OriginY+((MaxY-MinY)*ScaleY)
xttl = xax1+(0.05*(xax2-xax1))
yttl = yax1+(0.95*(yax2-yax1))
av.Run("Y_Axis",{MinX,MaxX,MinY,MaxY,OriginX,OriginY,ScaleX,ScaleY,YTickLength,PlotItem})
av.Run("X_Axis",{MinX,MaxX,MinY,MaxY,OriginX,OriginY,ScaleX,ScaleY,XTickLength})
if (DrawArea) then
  av.Run("Plot_Area",{MinX,MaxX,MinY,MaxY,OriginX,OriginY,ScaleX,ScaleY,theVTab,theBitMap,PlotItem,Guidelines})
end
if (DrawRect) then
  av.Run("Plot_Bar",{MinX,MaxX,MinY,MaxY,OriginX,OriginY,ScaleX,ScaleY,theVTab,theBitMap,PlotItem,Guidelines})
end
if (DrawStick) then
  if (vType>1) then
    av.Run("Plot_Stick_Type"+vType.AsString,{MinX,MaxX,MinY,MaxY,OriginX,OriginY,ScaleX,ScaleY,theVTab,theBitMap,PlotItem,Guidelines,ColList})
  else
    av.Run("Plot_Stick",{MinX,MaxX,MinY,MaxY,OriginX,OriginY,ScaleX,ScaleY,theVTab,theBitMap,PlotItem,Guidelines,ColList})
  end
end
Date1.SetFormat("yyyy-MM-dd")
Date2.SetFormat("yyyy-MM-dd")
aTitle = GraphicText.Make(Station.AsString++PlotItem.AsString++Date1.AsString++"to"++Date2.AsString++_UseType,xttl@yttl)
aTitleSymbol = aTitle.ReturnSymbols.Get(0)
aTitleSymbol.SetSize(8)
theGraphics.Add(aTitle)
'yTitle = GraphicText.Make( PlotItem.AsString, xlm@ylm )
'yTitleSymbol = yTitle.ReturnSymbols.Get( 0 )
'yTitleSymbol.SetSize( 8 )
'theGraphics.AddBatch( yTitle )

av.ClearMsg
av.ClearStatus
theGraphics.EndBatch
theView.Invalidate
aWindow.Activate
return {}