Legacy Layout v Layout Designer code
Another issue concerning coding is thematic and cartographic legend layer support for legacy code. If you look up cartographic legend in MapBasic Help you will find code which was developed for the legacy layout format. Below is a section from the Create Cartographic Legend section.
S_title="Theme Legend of " + WindowInfo (I_map_id, WW_INFO_NAME)
By using a note statement you can ascertain the legend name using the first section of the code snippet as shown in the screen below.
If we take another look at this line of code:-
S_title="Theme Legend of " + WindowInfo (I_map_id, WW_INFO_NAME)
The next thing to note is that WW_INFO_NAME must be a typing error. This should read WIN_INFO_NAME.
Even so when these corrections are made the code still doesn’t get beyond a note statement. As shown in the next image.
The Help section spells out the demise of the classic layout window and any related code as the following graphic from Help shows. The relevant statement has been highlighted.
- Maps can be brought into a layout, the classic layout Title clause now appears to be optional.
- Browsers, text and eclipse statements all work without an issue with legacy code.
Layout Designer window
Here is a screen shot from MapBasic Help which also includes some useful code:-
However, this code produces a legend window in the map section rather than in a layout window. We need to modify this code so that it can be used directly in the layout window.
Creating MapBasic code for a Layout Designer window
Declare Sub Main
Sub Main
Dim nMapWinid As Integer
Dim nLayoutWinid As Integer
Dim nBrowseWinid As Integer
Dim sString As String
' Open three tables
Open Table "C:\MapInfo Trial Data\USA\States.tab" As states
Open Table "C:\MapInfo Trial Data\USA\City_125.tab" As cities
Open Table "C:\MapInfo Trial Data\USA\Us_hiway.tab" As hiway
' Create map layers for these tables
Map From cities, hiway, states
Set Map Zoom 6370
' Save the current map window's ID
nMapWinid = Frontwindow()
' Create a browser window for columns State_Name and Pop_1990
Browse State_Name, Pop_1990 from STATES
' Save the current browser window's ID
nBrowseWinid = FrontWindow()
' Create a new layout designer window
Layout Designer Width 4 Height 6
' Save the layout window's ID
nLayoutWinid = FrontWindow()
Set CoordSys Layout Units "in"
' Add the map window to the layout window.
Create Frame Into Window nLayoutWinid
(0.235,1) (6,6)
From Window nMapWinid
' The following code is modified from the help section
' This code creates the cartographic legend for the three map layers
Create Designer Legend
Position (0,0) Units "in"
Width 0 Units "in" Height 0 Units "in"
Custom
Default Frame Title "# Legend" Font ("Calibri",1,12,16711680)
Default Frame Subtitle "#" Font ("Arial",2,10,255)
Default Frame Style "%" Font ("Lucida Calligraphy",0,8,16732240)
Default Frame Line Width 36 Units "pt"
Default Frame Region Width 32 Units "pt"
Default Frame Region Height 14 Units "pt"
Default Frame Auto Font Size ON
' Note that we here we can use the map layer names
Frame From Layer states
Using column object
label default
Position (4.7,1.14) Units "in"
Title "United States"
Subtitle "State Boundaries"
Frame From Layer cities
Using column object
label default
Position (4.7,1.85) Units "in"
Title "125 US Cities"
Subtitle "US Cities"
Frame From Layer hiway
Using column object
label default
Position (4.7,2.5) Units "in"
Title "US Highways"
Subtitle "Main Highways"
' Add the browser window to the layout window
Create Frame Into Window nLayoutWinid
(5.985, 0.985) (7.9, 10.9)
From Window nBrowseWinid
' Add a title to the layout
Create Text Into Window nLayoutWinid
"1990 Population Study"
' (2.5 Across.5 Down) (8 Across .9 Down)
(2.5,.5) (8,.9)
Font MakeFont ("Arial", 1, 24, BLACK, WHITE)
' Add company name and today's date to the layout.
Create Ellipse Into Window nLayoutWinid
(2.1, 6.75) (4.6, 7.55)
Pen (3,2, BLUE)
Create Text Into Window nLayoutWinid
"JPS Services"
(2.1, 7.03) (4.6, 7.3)
Font MakeFont("Arial", 0, 16, BLUE, WHITE)
Justify Center
sString = Str$(CurDate())
Create Text Into Window nLayoutWinid
sString
(3,8.6) (4, 8.9)
Font MakeFont("Arial", 0, 8, BLACK, WHITE)
Justify Left
End Sub
The following code should be sufficient to create a thematic legend:-
Position (3.7,1.16) Units "in"
Title "US by 1990 Population"
Priority 10
When run from the map window the following code reveals the names and types of all of the open layers.
Declare sub main
Sub Main
dim i,j as integer
j = frontwindow()
for i = 1 to mapperinfo(j,MAPPER_INFO_LAYERS)
print "No: " + i +
", Name: " + layerinfo (j,i,LAYER_INFO_NAME) +
", Type: " + layerinfo (j,i,LAYER_INFO_TYPE)
next
end sub
Position (3.7,1.16) Units "in"
Title "US by 1990 Population"
Priority 10
for i = 1 to mapperinfo(j,MAPPER_INFO_LAYERS)
if layerinfo(j,i,LAYER_INFO_TYPE) = 3 Then
sThematicLayer = layerinfo(j,i,LAYER_INFO_NAME)
End If
next
The following code creates a map, browser and thematic layer. Then a layout is created with a thematic legend included.
Declare Sub Main
Sub Main
Dim nMapWinid , i, j As Integer
Dim nLayoutWinid As Integer
Dim nBrowseWinid As Integer
Dim sString, sThematicLayer As String
' Open three tables
Open Table "C:\MapInfo Trial Data\USA\States.tab" As states
Open Table "C:\MapInfo Trial Data\USA\City_125.tab" As cities
Open Table "C:\MapInfo Trial Data\USA\Us_hiway.tab" As hiway
' Create map layers for these tables
Map From cities, hiway, states
Set Map Zoom 6370
' Create a thematic map of the States layer
Shade states With Pop_1990
Ranges 400000: 1500000,
1500000: 3500000,
3500000: 5500000,
5500000: 29800000
nMapWinid = Frontwindow()
' This code obtains the thematic layer name
j = nMapWinid
for i = 1 to mapperinfo(j,MAPPER_INFO_LAYERS)
if layerinfo(j,i,LAYER_INFO_TYPE) = 3 Then
sThematicLayer = layerinfo(j,i,LAYER_INFO_NAME)
End If
next
' Create a browser window for columns State_Name and Pop_1990
Browse State_Name, Pop_1990 from STATES
nBrowseWinid = FrontWindow()
' Create a new layout window
Layout Designer Width 4 Height 6
nLayoutWinid = FrontWindow()
Set CoordSys Layout Units "in"
' Add the map window to the layout window.
Create Frame Into Window nLayoutWinid
(0.235,1) (6,6)
From Window nMapWinid
' The following code is modified from MapBasic Help section
Create Designer Legend
Position (0,0) Units "in"
Width 0 Units "in" Height 0 Units "in"
Custom
Default Frame Title "# Legend" Font ("Calibri",1,12,16711680)
Default Frame Subtitle "#" Font ("Arial",2,10,255)
Default Frame Style "%" Font ("Lucida Calligraphy",0,8,16732240)
Default Frame Line Width 36 Units "pt"
Default Frame Region Width 32 Units "pt"
Default Frame Region Height 14 Units "pt"
Default Frame Auto Font Size ON
' Note that here we need to use
' the thematic layer name from
' code provided earlier in the program
Frame From Layer sThematicLayer
Position (3.7,1.16) Units "in"
Title "US by 1990 Population"
Priority 10
' Add the browser window to the layout window
Create Frame Into Window nLayoutWinid
(5.985, 0.985) (7.9, 10.9)
From Window nBrowseWinid
' Add a title to the layout
Create Text Into Window nLayoutWinid
"1990 Population Study"
' (2.5 Across .5 Down) (8 Across .9 Down
(2.5, .5) (8, .9)
Font MakeFont ("Arial", 1, 24, BLACK, WHITE)
' Add company name and today's date to the layout.
Create Ellipse Into Window nLayoutWinid
(2.1, 6.75) (4.6, 7.55)
Pen (3,2, BLUE)
Create Text Into Window nLayoutWinid
"JPS Services"
(2.1, 7.03) (4.6, 7.3)
Font MakeFont("Arial", 0, 16, BLUE, WHITE)
Justify Center
sString = Str$(CurDate())
Create Text Into Window nLayoutWinid
sString
(3,8.6) (4, 8.9)
Font MakeFont("Arial", 0, 8, BLACK, WHITE)
Justify Left
End Sub
When looking at the Help section in MapBasic be aware that some of the sections are referring to legacy code although this is not always explicitly stated.
Moving forward any layout code should be developed using the Layout Designer rather the classic Layout code to ensure that all elements of your layout are displayed correctly.