' Name: Y_Axis
'
' Title: Draw y-axis (select "nice" maximum)
'
' Topics: Views
'
' Description: Draw y-axis based on the maximum y data value
'              
' Author: Michael Silberbauer 2001
'
' Institute for Water Quality Studies, Department of Water and Sanitation
' Private Bag X313 PRETORIA South Africa 0001
' Requires: date2day
'
' Self:av.Run( "Y_Axis", { MinX, MaxX, MinY, MaxY, OriginX, OriginY, ScaleX, ScaleY, YTickLength, PlotItem } )
'
' 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)
YTickLength = Self.Get(8)
PlotItem = Self.Get(9)
xax1 = OriginX+((MinX-MinX)*ScaleX)
xax2 = OriginX+((MaxX-MinX)*ScaleX)
theProject = av.GetProject
theView = av.GetActiveDoc
theDisplay = theView.GetDisplay
theGraphics = theView.GetGraphics
theMapExtent = theView.GetDisplay.ReturnVisExtent
theLowerLeft = theMapExtent.ReturnOrigin
x1m = theLowerLeft.Getx
y1m = theLowerLeft.Gety
Width = theMapExtent.GetWidth
Height = theMapExtent.GetHeight
x2m = x1m+Width
y2m = y1m+Height

' For now, assume a zero minimum:

YaxMin = 0

'Using logs, find which power-of-ten range contains the data:

YmaxLog = MaxY.log(10)
YmaxCharacteristic = Ymaxlog.truncate
if (YmaxCharacteristic<0) then
  YmaxCharacteristic = YmaxCharacteristic-1
end
YmaxMantissa = (YmaxLog-YmaxCharacteristic).Abs

'MsgBox.Info( "YmaxCharacteristic"++YmaxCharacteristic.AsString++"YmaxMantissa"++YmaxMantissa.AsString, "Y AXIS" )

Ystep = 2.log(10)
nTics = 0
if ((YmaxMantissa>=0) and (YmaxMantissa<=Ystep)) then
  YaxMax = 2
  nTics = 5
end
if ((YmaxMantissa>Ystep) and (YmaxMantissa<=(2*Ystep))) then
  YaxMax = 4
  nTics = 4
end
if ((YmaxMantissa>(2*Ystep)) and (YmaxMantissa<=(3*Ystep))) then
  YaxMax = 8
  nTics = 4
end
if ((YmaxMantissa>(3*Ystep)) and (YmaxMantissa<1)) then
  YaxMax = 10
  nTics = 5
end
YaxMax = YaxMax*(10^YmaxCharacteristic)
if (PlotItem.AsString="pH") then
  YaxMax = 14
  nTics = 7
end
yax1 = OriginY
yax2 = OriginY+((YaxMax-MinY)*ScaleY)
yax7 = OriginY+((7-MinY)*ScaleY)
if (yax2>y2m) then
  yax2 = y2m
end
aLine = Line.Make(xax1@yax1,xax1@yax2)
gLine = GraphicShape.Make(aLine)
theGraphics.Add(gLine)
if (PlotItem.AsString="pH") then
  aLine = Line.Make(xax1@yax7,xax2@yax7)
  gLine = GraphicShape.Make(aLine)
  aSymbol = gLine.GetSymbol.SetColor(Color.GetGreen)
  theGraphics.Add(gLine)
end
TicX0 = OriginX
TicX1 = OriginX-yTickLength
TxtX = OriginX-(2*yTickLength)
av.ShowStopButton
av.ShowMsg("Plotting y-axis...")
for each nTic in 0..nTics
  av.SetStatus(100*(nTic/nTics))
  TicYv = YaxMax*nTic/nTics
  TicY = OriginY+((TicYv-MinY)*ScaleY)
  if (TicY<=y2m) then
    TicLine = Line.Make(TicX0@TicY,TicX1@TicY)
    gLine = GraphicShape.Make(TicLine)
    theGraphics.Add(gLine)
    TicText = GraphicText.Make(TicYv.AsString,TxtX@TicY)
    TicTextSymbol = TicText.ReturnSymbols.Get(0)
    TicTextSymbol.SetSize(6)
    theGraphics.AddBatch(TicText)
  end
end
theGraphics.EndBatch
av.ClearMsg
av.ClearStatus
return {}