Syntax Highlighing:
comments, key words, predefined symbols, class members & methods, functions & classes
### Afghan Urbanization Pie Chart ###
class GRDEVICE_MEM_RGB24 imagedev; ## color rendering device in memory for drawing GraphTip
class GRDEVICE_MEM_BINARY maskdev; ## transparency mask in memory for GraphTip
class GC gc; ## graphics context for GraphTip
class GRE_LAYER_VECTOR cntyLayer;
class GRE_GROUP group;
class VECTOR CntyVec;
class GEOREF vecGeoref;
class TRANSPARM trans;
class POINT2D ptLayer;
class POINT2D offset;
class COLOR color;
numeric polyNum; ## element number of polygon under cursor
numeric total; ## variable for total number of households in county
numeric urban, rural ; ## variables for different household type values
string percent$; ## string for percemtage labels
numeric start; ## starting x-ccordinate for label, computed from rendered length
## procedure called when group or layout is initialized
proc OnInitialize () {
imagedev.Create(100, 85); ## create GraphTip drawing area and mask with specified dimensions
maskdev.Create(100, 85);
gc = maskdev.CreateGC(); ## create graphics context for mask
gc.SetColorPixel(1);
gc.FillArcWedge(42, 30, 25, 25, 0, 360); ## set mask values for pie circle and label rectangle
gc.FillRect(1, 60, 85, 40); ## areas to 1; other graph tip areas transparent
offset.x = 10; ## set offset of graphtip from cursor position (right and down)
offset.y = 10;
}
### procedure called when View is created for group; use to get layer information
proc OnLayoutDrawEnd (
class GRE_LAYOUT layout,
class GRE_VIEW view
) {
group = layout.GetGroupByName("Population");
cntyLayer = group.ActiveLayer;
DispGetVectorFromLayer(CntyVec, cntyLayer);
vecGeoref = GetLastUsedGeorefObject(CntyVec);
# PopupMessage("Script Activated")
}
### function called when datatip event is triggered
func OnViewDataTipShowRequest (
class GRE_VIEW view, ## predefined class variables
class POINT2D point,
class TOOLTIP datatip
) {
numeric retval = 1; ## in GraphTip use only what is created by script
trans = view.GetTransLayerToScreen(cntyLayer, 1); ## get transform from screen to layer
ptLayer = trans.ConvertPoint2DFwd(point); ## cursor position in layer coordinates
polyNum = FindClosestPoly(CntyVec, ptLayer.x, ptLayer.y, vecGeoref, 0);
total = CntyVec.poly[polyNum].ProvStats.TOTAL; ## get values for households for polygon
rural = 360 * CntyVec.poly[polyNum].ProvStats.RURAL / total;
urban = 360 * CntyVec.poly[polyNum].ProvStats.URBAN / total;
string name$ = CntyVec.poly[polyNum].ProvStats.PROVINCE$;
numeric pop = CntyVec.poly[polyNum].Population_PRV.Population;
gc = imagedev.CreateGC(); ## create graphics context for graph tip
gc.SetColorName("white"); ## fill white rectangle with black border for label background
gc.SetLineWidth(1, "pixels");
gc.FillRect(1,60,83,50);# 1 60 84 50
gc.SetColorName("black");
gc.DrawRect(1,60,83,39);# 1 60 82 39
gc.DrawTextSetFont("ARIAL.TTF");
gc.DrawTextSetHeightPixels(13);
gc.TextStyle.RoundWidth = 1;
color.Name = "black"; ## draw title for label area
gc.SetColor(color);
gc.DrawTextSetColors(color);
numeric textsize = gc.TextGetWidth(name$); ## demir
gc.DrawTextSimple(name$, (85-textsize)/2, 72);
gc.DrawTextSetHeightPixels(11);
## draw pie slice for urbans
color.Name = "red";
gc.SetColor(color);
gc.FillArcWedge(42, 30, 25, 25, 0, urban);
gc.DrawTextSetColors(color); ## draw label and percentage in same color
gc.DrawTextSimple("Urban:", 5, 83);
percent$ = sprintf("%.1f\%", urban / 3.6);
start = 80 - gc.TextGetWidth(percent$); ## compute start position for percent to right-align
gc.DrawTextSimple(percent$, start, 83);
## draw pie slice for rurals
color.red =0; color.green = 0; color.blue = 255;
gc.SetColor(color);
gc.FillArcWedge(42, 30, 25, 25, urban, rural);
gc.DrawTextSetColors(color); ## draw label and percentage in same color
gc.DrawTextSimple("Pop:", 6, 95);
string pop$ = sprintf("%d", pop);
start = 80 - gc.TextGetWidth(pop$);
gc.DrawTextSimple(pop$, start, 95);
if (name$ <> ""){ # the data is available or not?
### set the rendered image and mask as source for the GraphTip
datatip.SetImageTip(imagedev, maskdev);
return retval;
}
}