Syntax Highlighing:
comments, key words, predefined symbols, class members & methods, functions & classes
### PieChartGraphTip.sml
### Display Control Script that shows a pie chart graph tip
### for county polygons using census attributes in the database.
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 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 onePerson, family, nonfamily; ## 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(110, 84); ## create GraphTip drawing area and mask with specified dimensions
maskdev.Create(110, 84);
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, 84, 50); ## 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
proc OnGroupCreateView (
class GRE_GROUP group,
class GRE_VIEW view
) {
cntyLayer = (class GRE_LAYER_VECTOR) group.GetLayerByName("Number of Households");
DispGetVectorFromLayer(CntyVec, cntyLayer);
vecGeoref = GetLastUsedGeorefObject(CntyVec);
}
### 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].Household.Total; ## get values for households for polygon
onePerson = 360 * CntyVec.poly[polyNum].Household._1_person_house / total;
family = 360 * CntyVec.poly[polyNum].Household.Family_househld / total;
nonfamily = 360 * CntyVec.poly[polyNum].Household.Nonfamily_house / total;
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,84,50);
gc.SetColorName("black");
gc.DrawRect(1,60,82,49);
gc.DrawTextSetFont("ARIAL.TTF");
gc.DrawTextSetHeightPixels(9);
gc.TextStyle.RoundWidth = 1;
color.Name = "black"; ## draw title for label area
gc.SetColor(color);
gc.DrawTextSetColors(color);
gc.DrawTextSimple("HOUSEHOLDS:", 8, 71);
gc.DrawTextSetHeightPixels(10);
## draw pie slice for one-person households in red
color.Name = "red";
gc.SetColor(color);
gc.FillArcWedge(42, 30, 25, 25, 0, onePerson);
gc.DrawTextSetColors(color); ## draw label and percentage in same color
gc.DrawTextSimple("1 person:", 5, 82);
percent$ = sprintf("%.1f\%", onePerson / 3.6);
start = 80 - gc.TextGetWidth(percent$); ## compute start position for percent to right-align
gc.DrawTextSimple(percent$, start, 82);
## draw pie slice for family households in dark green
color.red = 0; color.green = 60; color.blue = 0;
gc.SetColor(color);
gc.FillArcWedge(42, 30, 25, 25, onePerson, family);
gc.DrawTextSetColors(color); ## draw label and percentage in same color
gc.DrawTextSimple("Family:", 6, 94);
percent$ = sprintf("%.1f\%", family / 3.6);
start = 80 - gc.TextGetWidth(percent$);
gc.DrawTextSimple(percent$, start, 94);
## draw pie slice for non-family households in blue
color.Name = "blue";
gc.SetColor(color);
gc.FillArcWedge(42, 30, 25, 25, onePerson + family, nonfamily);
gc.DrawTextSetColors(color); ## draw label and percentage in same color
gc.DrawTextSimple("Nonfamily:", 6, 106);
percent$ = sprintf("%.1f\%", nonfamily / 3.6);
start = 80 - gc.TextGetWidth(percent$);
gc.DrawTextSimple(percent$, start, 106);
### set the rendered image and mask as source for the GraphTip
datatip.SetImageTip(imagedev, maskdev);
return retval;
}