' Name: X_Axis
'
' Title: Draw x-axis (time)
'
' Topics: Views
'
' Description: Draw an x-axis based on the Avenue date system.
'              
' 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( "X_Axis", { MinX, MaxX, MinY, MaxY, OriginX, OriginY, ScaleX, ScaleY, XTickLength } )
'
' 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)
XTickLength = Self.Get(8)
XTic1Length = 0.5*XTickLength
XTic2Length = 1.5*XTickLength
XTic3Length = 2.0*XTickLength
XTic4Length = 2.5*XTickLength
XTic5Length = 3.5*XTickLength
XTic6Length = 4.0*XTickLength
theProject = av.GetProject
theView = av.GetActiveDoc
theDisplay = theView.GetDisplay
theGraphics = theView.GetGraphics
nDays = MaxX-MinX
xax1 = OriginX
xax2 = OriginX+(nDays*ScaleX)
yax1 = OriginY
yax2 = OriginY+((MaxY-MinY)*ScaleY)
aLine = Line.Make(xax1@yax1,xax2@yax1)
gLine = GraphicShape.Make(aLine)
theGraphics.Add(gLine)

' Use MinX and MaxX to decide on month and day tic type:

nDays = nDays
yt0 = OriginY
yt1 = OriginY+((MinY-XTic1Length)*ScaleY)
yt2 = OriginY+((MinY-XTic2Length)*ScaleY)
yt3 = OriginY+((MinY-XTic3Length)*ScaleY)
yt4 = OriginY+((MinY-XTic4Length)*ScaleY)
yt5 = OriginY+((MinY-XTic5Length)*ScaleY)
yt6 = OriginY+((MinY-XTic6Length)*ScaleY)

' Always plot year tics:

av.ShowStopButton
av.ShowMsg("Plotting x-axis year tics...")
daycount = 0
for each nDay in MinX..MaxX
  jYear = Date.MakeFromJulianDay(nDay,0).SetFormat("yyyy")
  jDate = Date.MakeFromJulianDay(nDay,0).SetFormat("yyyy-MM-dd")
  daycount = daycount+1
  av.SetStatus(100*(daycount/nDays))
  av.ShowMsg("Plotting x-axis"++jDate.AsString)
  if (jDate.AsString.Right(5)="01-01") then
    xt1 = OriginX+((nDay-MinX)*ScaleX)
    aLine = Line.Make(xt1@yt0,xt1@yt5)
    gLine = GraphicShape.Make(aLine)
    theGraphics.AddBatch(gLine)
  end
  'Plot year-month annotation at either the xMin or 1-Jan position for <= 1 year (not ideal...):

  if (((nDay=MinX) or (jDate.AsString.Right(5)="01-01")) and (nDays<=366)) then
    xt1 = OriginX+((nDay-MinX)*ScaleX)
    jMonth = Date.MakeFromJulianDay(nDay,0).SetFormat("MM").AsString
    TicText = GraphicText.Make(jYear.AsString+"-"+jMonth,xt1@yt6)
    TicTextSymbol = TicText.ReturnSymbols.Get(0)
    TicTextSymbol.SetSize(9)
    theGraphics.AddBatch(TicText)
  end
  ' Plot year annotation at June-01 position for > 1 year:

  if ((jDate.AsString.Right(5)="06-01") and (nDays>366)) then
    xt1 = OriginX+((nDay-MinX)*ScaleX)
    TicText = GraphicText.Make(jYear.AsString,xt1@yt6)
    TicTextSymbol = TicText.ReturnSymbols.Get(0)
    TicTextSymbol.SetSize(9)
    theGraphics.AddBatch(TicText)
  end
  ' < about 20 years: plot month tics:

  if ((jDate.AsString.Right(2)="01") and (nDays<=7500)) then
    xt1 = OriginX+((nDay-MinX)*ScaleX)
    aLine = Line.Make(xt1@yt0,xt1@yt3)
    gLine = GraphicShape.Make(aLine)
    theGraphics.AddBatch(gLine)
  end
  ' < about 2 years: annotate months halfway between tics with single letter:

  if ((jDate.AsString.Right(2)="15") and ((nDays<=750) and (nDays>366))) then
    xt1 = OriginX+((nDay-MinX)*ScaleX)
    jMonth = Date.MakeFromJulianDay(nDay,0).SetFormat("MMM").AsString.Left(1)
    TicText = GraphicText.Make(jMonth.AsString,xt1@yt4)
    TicTextSymbol = TicText.ReturnSymbols.Get(0)
    TicTextSymbol.SetSize(6)
    theGraphics.AddBatch(TicText)
  end
  ' < about 1 year: annotate months halfway between tics with three letters:

  if ((jDate.AsString.Right(2)="15") and (nDays<=366)) then
    xt1 = OriginX+((nDay-MinX)*ScaleX)
    jMonth = Date.MakeFromJulianDay(nDay,0).SetFormat("MMM")
    TicText = GraphicText.Make(jMonth.AsString,xt1@yt4)
    TicTextSymbol = TicText.ReturnSymbols.Get(0)
    TicTextSymbol.SetSize(6)
    theGraphics.AddBatch(TicText)
  end
  ' < about 6 months: plot day tics:

  if (nDays<=182) then
    xt1 = OriginX+((nDay-MinX)*ScaleX)
    aLine = Line.Make(xt1@yt0,xt1@yt1)
    gLine = GraphicShape.Make(aLine)
    theGraphics.AddBatch(gLine)
  end
  ' < about 2 months: annotate days halfway between tics:

  if (nDays<=62) then
    xt1 = OriginX+(((nDay+0.5)-MinX)*ScaleX)
    nDayTxt = Date.MakeFromJulianDay(nDay,0).SetFormat("dd")
    TicText = GraphicText.Make(nDayTxt.AsString,xt1@yt2)
    TicTextSymbol = TicText.ReturnSymbols.Get(0)
    TicTextSymbol.SetSize(6)
    theGraphics.AddBatch(TicText)
  end
end
av.ClearMsg
av.ClearStatus
theGraphics.EndBatch
return {}