' Name: JuanitaPie
'
' Title: Draw sector-pie symbols
'
' Topics: Views
'
' Description: plots radial symbols (Juanita pies)
'
' Originally an AML written by Juanita Moolman
' Institute for Water Quality Studies, Department of Water and Sanitation
' Private Bag X313 PRETORIA South Africa 0001
' ech@dwaf-hri.pwv.gov.za
' Reworked for Avenue by Michael Silberbauer 2001-03-12
'
' Requires: Called by PiePlot
'
' Self:
'
' Results:
' -----------------------------------------------------------------------------
' Calculate the dimensions of the Juanita pie diagram:

theView = av.GetActiveDoc
theGraphics = theView.GetGraphics
theGraphicGroup = GraphicGroup.Make
pi = Number.GetPi
angrad = pi/180
XCentre = 0.0
YCentre = 0.0
Radius = 1
PetAngle = 360/_nPetals
TotalArea = pi*Radius^2
SegmentArea = TotalArea/_nPetals
'Calculate a constant base area:

BaseArea = SegmentArea/4
BaseRadius = (BaseArea/pi).sqrt
xcirc = (XCentre*_Scale)+_xSymbol
ycirc = (YCentre*_Scale)+_ySymbol
RadiusCirc = BaseRadius*_Scale
aCircle = Circle.Make(xcirc@ycirc,RadiusCirc)
gCircle = GraphicShape.Make(aCircle)
aSymbol = RasterFill.Make
aSymbol.SetColor(Color.GetWhite)
aSymbol.SetOLColor(Color.GetBlack)
gCircle.SetSymbol(aSymbol)
gCircle.SetObjectTag("Juanitapie")
theGraphicGroup.Add(gCircle)
'set file for debug data

aFileName = FN.make("Temp.txt")
linFile = LineFile.Make(aFileName,#FILE_PERM_MODIFY)
Petal = 0
for each ClassValue in _ClassList
  colour = _ColourList.Get(Petal)
  Petal = Petal+1
  if (ClassValue>=0) then
    Item = _ItemsList.Get(Petal-1)
    Value_item = _ValueList.Get(Petal-1)
    AngNum = _SeqnsList.Get(Petal-1)
    NumClasses = _UseVariables.Get(Item).Get(2)
    Type = _UseVariables.Get(Item).Get(1)
    SeqNum = _UseVariables.Get(Item).Get(0)
    if (ClassValue=0) then ' radius = circle radius
      ItmArea = BaseArea
      ItmRadius = BaseRadius
    else
      ItmArea = (SegmentArea-BaseArea)*((ClassValue+1)/(NumClasses/Type))
      ItmArea = BaseArea+ItmArea
      ItmRadius = (ItmArea/pi).sqrt
      'ItmRadius   = ( ItmArea / pi ).log( 10 )

    end
    StartAngle = SeqNum*PetAngle
    AngList = List.Make
    nvs = 4
    for each nv in 0..nvs
      AngList.Add(StartAngle+(PetAngle*(nv/nvs)))
    end
    theVtxList = List.Make
    theVtxList.Add(xcirc@ycirc)
    for each angvalue in AngList
      theVtxX = ItmRadius*(angrad*angvalue).cos
      xv = (theVtxX*_Scale)+_xSymbol
      theVtxY = ItmRadius*(angrad*angvalue).sin
      yv = (theVtxY*_Scale)+_ySymbol
      theVtxList.Add(xv@yv)
    end
    theVtxList.Add(xcirc@ycirc)
    mPol = polygon.Make({theVtxList})
    gmPol = GraphicShape.Make(mPol)
    aSym = RasterFill.Make
    ColIndx1 = (colour+1)*3
    ColIndx2 = ColIndx1+1
    ColIndx3 = ColIndx1+2
    Rval1 = _theColourDict.Get(Item).Get(ColIndx1)
    Rval2 = _theColourDict.Get(Item).Get(ColIndx2)
    Rval3 = _theColourDict.Get(Item).Get(ColIndx3)
    aCol = Color.Make
    aCol.SetRGBList({Rval1,Rval2,Rval3})
    aSym.SetColor(aCol)
    gmPol.SetSymbol(aSym)
    theGraphicGroup.Add(gmPol)
    textstring = "CLASSVALUE"++ClassValue.AsString++Item.AsString++Value_item.AsString++AngNum.AsString++NumClasses.AsString++Type.AsString++SeqNum.AsString++"colour="+colour.AsString++"RGB="+RVal1.AsString++RVal2.AsString++RVal3.AsString
    linFile.GotoEnd
    linFile.WriteElt(textstring)
    if (_LblSymbol) then
      ItemText = GraphicText.Make(Item,theVtxList.Get((theVtxList.Count/2).round-1))
      ItemAngle = StartAngle+(PetAngle*0.5)
      if ((ItemAngle<270) and (ItemAngle>90)) then
        ItemAngle = ItemAngle+180
      end
      if (ItemAngle>360) then
        ItemAngle = ItemAngle-360
      end
      ItemText.SetAngle(ItemAngle)
      ItemText.SetObjectTag("Juanitapie")
      ItemSymbol = ItemText.ReturnSymbols.Get(0)
      ItemSymbol.SetSize(10)
      theGraphicGroup.Add(ItemText)
    end
    'theGraphics.Invalidate

  end
end
if (_IDSymbol) then
  nTs = 6 '(number of halo text copies)
  HaloColour = Color.GetWhite
  TextColour = Color.GetBlack
  FontSize = 8
  pi = Number.GetPi
  angrad = pi/180
  hAngle = 360/nTs
  Tx = xcirc-(RadiusCirc*0.95)
  Ty = ycirc
  IDText = GraphicText.Make(_station.AsString.Left(6),Tx@Ty)
  IDTextSymbol = IDText.ReturnSymbols.Get(0)
  IDTextSymbol.SetSize(FontSize)
  IDText.GetSymbol.SetColor(TextColour)
  HaloDist = RadiusCirc/12
  for each nT in 0..nTs ' draw text several times, slightly offset, for "halo"
    HaloText = GraphicText.Make(_station.AsString.Left(6),Tx@Ty)
    angvalue = hAngle*nT
    theOffX = HaloDist*(angrad*angvalue).cos
    theOffY = HaloDist*(angrad*angvalue).sin
    HaloText.Offset(Point.Make(theOffX,theOffY))
    HaloTextSymbol = HaloText.ReturnSymbols.Get(0)
    HaloTextSymbol.SetSize(FontSize)
    HaloText.GetSymbol.SetColor(HaloColour)
    theView.GetDisplay.HookUpSymbol(HaloText.GetSymbol)
    theGraphicGroup.Add(HaloText)
  end
  theView.GetDisplay.HookUpSymbol(IDText.GetSymbol)
  theGraphicGroup.Add(IDText)
  end
theGraphicGroup.SetObjectTag("Juanitapie")
theGraphics.Add(theGraphicGroup)
theGraphics.Invalidate
linFile.close 'close file for debug data