Syntax Highlighing:
comments, key words, predefined symbols, class members & methods, functions & classes
### AgePieChartTip.sml
### Display Control Script that shows a pie chart in the DataTip and allows
### information from other layers to be shown as well.
### Example shows percentage population in different age groups
### for census blocks in Lancaster County, NE.
### Requires TNTmips version 2007:73.
class GRDEVICE_MEM_RGBA dev;
class GRE_LAYER_VECTOR blkLayer;
class VECTOR BlkVec;
class GEOREF vecGeoref;
class TRANSPARM trans;
class POINT2D ptLayer;
class POINT2D offset;
numeric start;
numeric width, height, xCent, yCent;
## procedure called when group or layout is initialized
proc OnInitialize () {
# create RGBA graphics device w/ specified height, width to be used to draw
# graph and its legend
width = 220; height = 120;
dev.Create(height, width);
offset.x = 60; ## set offset of datatip 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
) {
# get layer containing the census blocks by name from the group
# and get the vector from this layer
blkLayer = (class GRE_LAYER_VECTOR) group.GetLayerByName("Census Blocks");
DispGetVectorFromLayer(BlkVec, blkLayer);
vecGeoref = GetLastUsedGeorefObject(BlkVec);
}
### procedure called when DataTip event is triggered
func OnViewDataTipShowRequest (
class GRE_VIEW view,
class POINT2D point,
class TOOLTIP datatip
) {
local numeric polyNum;
local numeric pctU18, pct18to29, pct30to44, pct45to64, pct65over;
local numeric sum = 0; # running sum of angles for setting start angle for next arc wedge
local numeric rectwidth; # width of rectangle for labels
local numeric hcentRect; # horizontal center coordinate of rectangle
local string title$, percent$;
local numeric fontsize, linespace;
local numeric texty = 0; # y-position of text string
local class GC gc;
local class COLOR color;
# set the size of the margin between DataTip contents and border in screen pixels
datatip.MarginHeight = 5;
datatip.MarginWidth = 5;
# clear graphics device to 100% transparent before drawing
local class COLOR transparent(0, 0, 0, 0); # transparent color
dev.Clear(transparent);
trans = view.GetTransLayerToScreen(blkLayer, 1); ## get transform from screen to layer
ptLayer = trans.ConvertPoint2DFwd(point); ## cursor position in layer coordinates
# get number of parcel polygon under cursor
polyNum = FindClosestPoly(BlkVec, ptLayer.x, ptLayer.y, vecGeoref, 0);
# get age group population percentages from database table for current polygon
# and convert to degrees of arc out of 360
pctU18 = 3.6 * BlkVec.poly[polyNum].AgeGroups.PctUnder18;
pct18to29 = 3.6 * BlkVec.poly[polyNum].AgeGroups.PctFrm18to29;
pct30to44 = 3.6 * BlkVec.poly[polyNum].AgeGroups.PctFrm30to44;
pct45to64 = 3.6 * BlkVec.poly[polyNum].AgeGroups.PctFrm45to64;
pct65over = 3.6 * BlkVec.poly[polyNum].AgeGroups.PctOver64;
# set variables for positioning drawing elements (in pixels from upper left corner)
rectwidth = 120; # width of rectangle for labels
hcentRect = rectwidth / 2;
xCent = rectwidth + (width - rectwidth) / 2; # location of pie chart center
yCent = height / 2;
r = 40; # radius of pie chart
boxtop = 1; # y-coord of top of rectangle for labels
fontsize = 12;
linespace = fontsize + 3;
gc = dev.CreateGC(); ## create graphics context for graph and text
## fill white rectangle with black border for label background
gc.SetColorName("white");
gc.SetLineWidth(1, "pixels");
gc.FillRect(1, boxtop, rectwidth, 110); ## fill rectangle upleft x, upleft y, width, height
gc.SetColorName("black");
gc.DrawRect(1, boxtop, rectwidth-2, 109); ## draw black border
gc.DrawTextSetFont("ARIALBD.TTF"); # set text parameters
gc.DrawTextSetHeightPixels(fontsize);
gc.TextStyle.RoundWidth = 1;
texty = boxtop + linespace; ## set vertical position of text
color.Name = "black"; ## draw title for label area
gc.SetColor(color);
gc.DrawTextSetColors(color);
title$ = "Census Block";
start = hcentRect - ( gc.TextGetWidth(title$) / 2);
gc.DrawTextSimple(title$, start, texty);
texty += linespace; ## increment vertical position of text
title$ = "Age Breakdown:"; # next line of title
start = hcentRect - ( gc.TextGetWidth(title$) / 2)
gc.DrawTextSimple(title$, start, texty);
gc.DrawTextSetHeightPixels(fontsize); # reset font parameters
gc.DrawTextSetFont("ARIAL.TTF");
## draw pie slice for under 18 group in red
color.Name = "red";
gc.SetColor(color);
texty += linespace; ## increment vertical position of text
if (pctU18 > 0) {
gc.FillArcWedge(xCent, yCent, r, r, 0, pctU18); # draw wedge w/ centerX, centerY, radiusX, radiusY, startangle, sweepangle
sum += pctU18; ## increment sum of wedge angles
}
gc.DrawTextSetColors(color); ## draw label and percentage in same color
gc.DrawTextSimple("Under 18:", 5, texty);
percent$ = sprintf("%.1f\%", pctU18 / 3.6);
start = rectwidth-4 - gc.TextGetWidth(percent$); ## compute start position for percent to right-align
gc.DrawTextSimple(percent$, start, texty);
## draw pie slice for 18 to 29 group in cyan
color.Name = "cyan3";
gc.SetColor(color);
texty += linespace;
if (pct18to29 > 0) {
gc.FillArcWedge(xCent, yCent, r, r, sum, pct18to29);
sum += pct18to29;
}
gc.DrawTextSetColors(color); ## draw label and percentage in same color
gc.DrawTextSimple("18 to 29:", 5, texty);
percent$ = sprintf("%.1f\%", pct18to29 / 3.6);
start = rectwidth-4 - gc.TextGetWidth(percent$); ## compute start position for percent to right-align
gc.DrawTextSimple(percent$, start, texty);
## draw pie slice for 30 to 44 group in blue
color.Name = "blue";
gc.SetColor(color);
texty += linespace;
if (pct30to44 > 0) {
gc.FillArcWedge(xCent, yCent, r, r, sum, pct30to44);
sum += pct30to44;
}
gc.DrawTextSetColors(color); ## draw label and percentage in same color
gc.DrawTextSimple("30 to 44:", 5, texty);
percent$ = sprintf("%.1f\%", pct30to44 / 3.6);
start = rectwidth-4 - gc.TextGetWidth(percent$); ## compute start position for percent to right-align
gc.DrawTextSimple(percent$, start, texty);
## draw pie slice for 45 to 64 group in green
color.Name = "purple";
gc.SetColor(color);
texty += linespace;
if (pct45to64 > 0) {
gc.FillArcWedge(xCent, yCent, r, r, sum, pct45to64);
sum += pct45to64;
}
gc.DrawTextSetColors(color); ## draw label and percentage in same color
gc.DrawTextSimple("45 to 64:", 5, texty);
percent$ = sprintf("%.1f\%", pct45to64 / 3.6);
start = rectwidth-4 - gc.TextGetWidth(percent$); ## compute start position for percent to right-align
gc.DrawTextSimple(percent$, start, texty);
## draw pie slice for over 65 group in purple
color.Name = "forest green";
gc.SetColor(color);
texty += linespace;
if (pct65over > 0) {
gc.FillArcWedge(xCent, yCent, r, r, sum, pct65over);
}
gc.DrawTextSetColors(color); ## draw label and percentage in same color
gc.DrawTextSimple("65 & over:", 5, texty);
percent$ = sprintf("%.1f\%", pct65over / 3.6);
start = rectwidth-4 - gc.TextGetWidth(percent$); ## compute start position for percent to right-align
gc.DrawTextSimple(percent$, start, texty);
## draw black outline around pie
color.Name = "black";
gc.SetColor(color);
gc.DrawCircle(xCent, yCent, r);
datatip.AppendImage(dev); # add graphic to standard DataTip
string BlockNum$ = "\n{~FARIALBD.TTF}Block Number:{~FARIAL.TTF~TABS 15L}\t" + BlkVec.poly[polyNum].blocks.BLOCK$ + "\n\n";
datatip.AppendText(BlockNum$); # add carriage return after graphic
return 0; # return 0 to render image in normal datatip frame along with info from other layers
}