BarGraphTip.sml

  Download

More scripts: Enhanced Data Tip

Syntax Highlighing:

comments, key words, predefined symbols, class members & methods, functions & classes
            
### BarGraphTip.sml
### Display Control Script that shows a bar 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 pcUnder18, pc18to24, pc25to44, pc45to64, pc65andOver;		## variables for population values (%)
## procedure called when group or layout is initialized
proc OnInitialize () {
	imagedev.Create(76, 100);		## create GraphTip drawing area and mask with specified dimensions
	maskdev.Create(76, 100);		## mask not used in this example, but device needed in order to use offset parameter
	offset.x = 12;
	offset.y = 12;
	}
### procedure called when View is created for group; use to get layer information
proc OnGroupCreateView (
   class GRE_GROUP group,			## predefined class variables
   class GRE_VIEW view
   ) {
   cntyLayer = group.GetLayerByName("% population growth");
	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
	polyNum = 0;
	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);
	pcUnder18 = CntyVec.poly[polyNum].NEcntyPopAge.pc_under_18;		## read values from database
	pc18to24 = CntyVec.poly[polyNum].NEcntyPopAge.pc_18_to_24;		## for polygon under cursor
	pc25to44 = CntyVec.poly[polyNum].NEcntyPopAge.pc_25_to_44;
	pc45to64 = CntyVec.poly[polyNum].NEcntyPopAge.pc_45_to_64;
	pc65andOver = CntyVec.poly[polyNum].NEcntyPopAge.pc_65_and_over;
	gc = imagedev.CreateGC();		## create graphics context for graph tip
	gc.SetColorName("white");		## fill white rectangle with black border for label background
	gc.FillRect(0, 0, 100, 75);
	gc.SetColorName("black");
	gc.DrawRect(0, 0, 98, 75);
	# fill rectangles with different colors left to right to create vertical bars
	# using population category value as height
	color.red = 100;	color.green = 0;	color.blue = 0;
	gc.SetColor(color);
	gc.FillRect(15, 60 - pcUnder18, 14, pcUnder18);
	color.red = 0;	color.green = 67; color.blue = 0;
	gc.SetColor(color);
	gc.FillRect(30, 60 - pc18to24, 14, pc18to24);
	color.red = 0;		color.green = 33;	color.blue = 100;
	gc.SetColor(color);
	gc.FillRect(45, 60 - pc25to44, 14, pc25to44);
	color.red = 100;	color.green = 67; color.blue = 0;
	gc.SetColor(color);
	gc.FillRect(60, 60 - pc45to64, 14, pc45to64);
	color.red = 100;	color.green = 0;	color.blue = 100;
	gc.SetColor(color);
	gc.FillRect(75, 60 - pc65andOver, 14, pc65andOver);
	gc.DrawTextSetFont("ARIAL.TTF");		## set font attributes for labels
	gc.DrawTextSetHeightPixels(9);
	gc.TextStyle.RoundWidth = 1;
	color.Name = "black";
	gc.SetColor(color);
	gc.DrawTextSetColors(color);
	gc.DrawTextSimple("AGE GROUPS:", 22, 15);
	gc.DrawRect(15, 20, 75, 40);		## draw black frame and grid line for chart
	gc.MoveTo(15, 40);
	gc.DrawTo(90, 40);
	gc.DrawTextSimple("0", 12, 63, 90);		## vertical axis labels
	gc.DrawTextSimple("20", 12, 45, 90);
	gc.DrawTextSimple("40%", 12, 25, 90);
	gc.DrawTextSimple("0", 14, 70)			## draw labels for age group boundaries
	gc.DrawTextSimple("18", 26, 70);			## (boundaries of bars)
	gc.DrawTextSimple("25", 41, 70);
	gc.DrawTextSimple("45", 56, 70);
	gc.DrawTextSimple("65", 71, 70);
	### set the rendered image as source for the GraphTip
	datatip.SetImageTip(imagedev, maskdev, offset);
   return retval;
   }