' Name: PieLegend
'
' Title: Create legend for pie symbols
'
' Topics: Views
'
' Description: Reads water quality guidelines from file and generates a legend for Juanita Pies
'
' Author: Michael Silberbauer 2001
' Institute for Water Quality Studies, Department of Water and Sanitation
' Private Bag X313 PRETORIA South Africa 0001
' SilberbauerM@dwaf.gov.za
'
' Requires: A guideline data file
'_theColourDict = Dictionary of colours
'_UseVariables  = Dictionary of variables defined for each use type
'_theChemSym    = Dictionary of variable abbreviations
'_theNarrative  = Dictionary of guideline descriptions
'
' Self:
'
' Results:
' -----------------------------------------------------------------------------

theProject = av.getProject
theView = View.Make
theView.SetTOCWidth(0)
ScreenWidth = System.ReturnScreenSizePixels.GetX
ScreenHeight = System.ReturnScreenSizePixels.GetY
WinWidth = 0.25*ScreenWidth
WinHeight = 1.00*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
'| |  | yk                    |
'| +--+                       |
'|  xk                        |
'|                            |Height
'|                            |
'|                            |
'|                            |
'+----------------------------+
'x1m,y1m        Width

theLowerLeft = theMapExtent.ReturnOrigin
x1m = theLowerLeft.Getx
y1m = theLowerLeft.Gety
xwm = theMapExtent.GetWidth
ywm = theMapExtent.GetHeight
x2m = x1m+xwm
y2m = y1m+ywm
theUpperRight = x2m@y2m
xm = xwm/33
ym = ywm/33
xk = (xwm-xm-xm)/10
xt = xm+xk+xm
y = 1
x = y*(xwm/ywm)
UseTypeList = ({"Domestic_Health","Domestic_Salts","Irrigation"})
_UseType = MsgBox.ListAsString(UseTypeList,"Please choose a use type","Juanita Pie: Choose use type")
_ValType = "Val_info_med" ' Code for looking up median cutpoint data in classcodes.txt
ViewName = "Key_for_"+_UseType
theView.SetName(ViewName)
theTable = theProject.FindDoc("classcodes.txt")
theVTab = theTable.getVTab
numrecs = theVTab.GetNumRecords
FieldList = theVTab.GetFields
theVTab.GetSelection.ClearAll
theVTab.UpdateSelection
theBitMap = theVTab.GetSelection
expr = "( [Group] ="++_UseType.AsString.Quote++" )"
theVTab.Query(expr,theBitMap,#VTAB_SELTYPE_NEW)
theVTab.UpdateSelection
totalBits = theBitMap.GetSize
selBits = theBitMap.Count
F_Val_Info = theVTab.FindField("Val_Info")
F_N_Cuts = theVTab.FindField("Number")
F_Red = List.Make
F_Green = List.Make
F_Blue = List.make
F_Text = List.Make
for each nCut in 0..8
  F_Red.Add(theVTab.FindField("R"+nCut.AsString))
  F_Green.Add(theVTab.FindField("G"+nCut.AsString))
  F_Blue.Add(theVTab.FindField("B"+nCut.AsString))
  F_Text.Add(theVTab.FindField("Text"+nCut.AsString))
end
F_Symbol = theVTab.FindField("Symbol")
F_Units = theVTab.FindField("Units")
nKeys = 0
for each rec in theBitMap
  nCuts = theVTab.ReturnValue(F_N_Cuts,rec)
  nKeys = nKeys+nCuts
  nKeys = nKeys+1
end
yk = (ywm-ym-ym)/(nKeys+2)
yk1 = y2m-ym
xk1 = x1m+xm
ykll = yk1
yt = ykll
labeltext = Viewname.Substitute("_"," ")
IDText = GraphicText.Make(labeltext,xt@yt)
IDTextSymbol = IDText.ReturnSymbols.Get(0)
IDTextSymbol.SetSize(10)
theGraphics.Add(IDText)
ykll = yk1-yk
ykll = yk1-yk
lastcut = " "
for each rec in theBitMap
  ykll = ykll-yk
  yt = ykll+(0.5*yk)
  Symbl = theVTab.ReturnValue(F_Symbol,rec)
  Unitz = theVTab.ReturnValue(F_Units,rec)
  labeltext = Symbl++"("+Unitz+")"
  IDText = GraphicText.Make(labeltext,xt@yt)
  IDTextSymbol = IDText.ReturnSymbols.Get(0)
  IDTextSymbol.SetSize(9)
  theGraphics.Add(IDText)
  nCuts = theVTab.ReturnValue(F_N_Cuts,rec)
  for each nCut in 0..nCuts
    ykll = ykll-yk
    yt = ykll+(0.25*yk)
    if (nCut=0) then
      F_Cut = theVTab.FindField("Cut"+(nCut+1).AsString)
      Range = "<"++theVTab.ReturnValue(F_Cut,rec).AsString
    end
    if ((nCut>0) and (nCut<nCuts)) then
      F_Cut = theVTab.FindField("Cut"+(nCut).AsString)
      F_Cut1 = theVTab.FindField("Cut"+(nCut+1).AsString)
      Range = theVTab.ReturnValue(F_Cut,rec).AsString++"-"++theVTab.ReturnValue(F_Cut1,rec).AsString
    end
    if (nCut=nCuts) then
      F_Cut = theVTab.FindField("Cut"+(nCut).AsString)
      Range = ">"++theVTab.ReturnValue(F_Cut,rec).AsString
    end
    Red = theVTab.ReturnValue(F_Red.Get(nCut),rec)
    Green = theVTab.ReturnValue(F_Green.Get(nCut),rec)
    Blue = theVTab.ReturnValue(F_Blue.Get(nCut),rec)
    mRect = Rect.Make(xk1@ykll,xk@yk)
    gmRec = GraphicShape.Make(mRect)
    aSym = RasterFill.Make
    aCol = Color.Make
    aCol.SetRGBList({Red,Green,Blue})
    aSym.SetColor(aCol)
    aSym.SetOLColor(Color.GetBlack)
    gmRec.SetSymbol(aSym)
    theGraphics.Add(gmRec)
    Text = theVTab.ReturnValue(F_Text.Get(nCut),rec)
    labeltext = Range++Text
    IDText = GraphicText.Make(labeltext,xt@yt)
    IDTextSymbol = IDText.ReturnSymbols.Get(0)
    IDTextSymbol.SetSize(7)
    theGraphics.Add(IDText)
  end
  ykll = ykll-yk
end
nowDate = Date.Now
nowDate.SetFormat("yyyy-MM-dd")
nowTime = Date.Now
nowTime.SetFormat("hhhh:m:s")
dtStamp = nowDate.asString++" "++nowTime.AsString

'av.ClearMsg
'av.ClearStatus

theGraphics.Invalidate