Hi George
I needed to get my hands dirty to really understand what's going on and give you a solution.
I think I have found something that you can work with now.
Here's how the legend looks in the layout with my solution
I modified your code a bit to find the map from a specific frame in the layout. You can modify this if you already know which frame contains the map or if you have gotten the ID of the map in a different way.
I get the size and the position of the frame with map to place my legend frames inside the map frame. I chose the easy solution and used the top left corner. If you want the lower right corner, you'll have to create all the frames, figure out the maximum width and total height of the frames, and then calculate how to fit them into the corner and finally use the
Alter Designer Frame
statement to move all the legend frames.
For the first layer, you create a legend for, use the Create Designer Legend. Now get the legend window ID from this frame using this expression:
legend_windowid = LayoutItemInfo(layout_windowid, LayoutInfo(layout_windowid, LAYOUT_INFO_NUM_ITEMS), LAYOUT_ITEM_INFO_LEGEND_DESIGNER_WINDOW)
For the remaining layers, use Add Designer Frame and refer to the legend window ID retrieved above.
Using the maximum width and total height of the legend frames, you can finally create a new frame with a rectangle using the
Create Rect
statement. You can use the
Priority
keyword to control the draw order.
I have included my working solution below.
Let me know how this works out for you
------------------------------
Peter Horsbøll Møller
Distinguished Engineer
Pitney Bowes Software & Data
------------------------------
Original Message:
Sent: 08-05-2020 08:17
From: George Corea
Subject: Mapbasic to add Thematic Legend in one frame
Thanks. It runs but it seems to just create some frames in the Legend Designer with no info in them.
and then being able to control where this is placed in the layout. So something like this with a rectangle behind the legend elements.
I've attached the wor and mb for this. The data is just the sample data from the python examples.
Regards,
------------------------------
George Corea
Mangoesmapping
Original Message:
Sent: 08-05-2020 03:47
From: Peter Horsbøll Møller
Subject: Mapbasic to add Thematic Legend in one frame
Hi George
I get a bit confused by your example as you have multiple For statements in the code. I guess most of them have been disabled, right?
Can you try something along these lines:
SUB Main
Dim num_windows as integer
num_windows = NumWindows()
print (num_windows)
Dim i,map_windowid,legend_windowid,map_layer_count as integer
Dim info_name, info_type, info_windowid as string
For i = 1 to num_windows
print ("............")
print (i)
info_name = (WindowInfo(i, WIN_INFO_NAME))
info_type = (WindowInfo(i, WIN_INFO_TYPE))
info_windowid = (WindowInfo(i, WIN_INFO_WINDOWID))
' print (info_name)
' print (info_type)
' print (info_windowid)
if info_type="35" then
print ("legend window")
legend_windowid=int(info_windowid)
elseif info_type="1" then
print ("map window")
map_windowid=int(info_windowid)
map_layer_count=MapperInfo(info_windowid, MAPPER_INFO_LAYERS)
' print (map_layer_count)
'LayerListInfo(map_window_id, numeric_counter, attribute )
'**This is where we build the legend in the layout
'**I'm just adding a static 2 inces to the position for each layer
'**- that needs to depend on the size of the previous frame
For nLayer = 1 to map_layer_count
Create Designer Legend Custom
'Position (2.0,2.0) Units "in"
'Width 2.8 Units "in" Height 5 Units "in"
Frame From Layer i Position (2.0 + ((i-1) * 2), 2.0) Units "in"
Next 'nLayer
end if
next
print ("Collected layer/map info")
End Sub
------------------------------
Peter Horsbøll Møller
Distinguished Engineer
Pitney Bowes Software & Data
Original Message:
Sent: 08-05-2020 02:34
From: George Corea
Subject: Mapbasic to add Thematic Legend in one frame
Thanks Peter,
That seems to work if we know the layers in the map but not if we don't and try to build the list.
For example in the following code I get
------------------------------
George Corea
Mangoesmapping
Original Message:
Sent: 08-03-2020 08:29
From: Peter Horsbøll Møller
Subject: Mapbasic to add Thematic Legend in one frame
Hi George
You can specify the position using the Create Designer Legend
statement but you need to do this for the individual frames, not for the overall Legend.
Here's an example:
Create Designer Legend Custom
Position (2.0,2.0) Units "in"
Width 2.8 Units "in" Height 5 Units "in"
Frame From Layer 2 Position (2.0, 2.0) Units "in"
Frame From Layer 1 Position (2.0, 3.0) Units "in"
The first Position
is ignored as that's used when you create it as a stand-alone window. It's the Positon
element for each of the Frame From Layer
parts that control where the frame will be placed.
Also, note that you need to include the Custom
keyword at the beginning for MapInfo Pro to use the positions of the individual frames.
I'd suggest that you only insert the legend from one layer at a time and then inspect the size of that frame. You can now calculate the location of the next frame to insert.
------------------------------
Peter Horsbøll Møller
Distinguished Engineer
Pitney Bowes Software & Data
Original Message:
Sent: 08-03-2020 07:28
From: Peter Horsbøll Møller
Subject: Mapbasic to add Thematic Legend in one frame
Hi George
The new layout works differently than the old one. Think of a legend window when dealing with legends.
Each legend needs to go into its own frame. You can insert the first legend into an existing frame and then add additional frames below this one. So it's a bit more cumbersome than you might think. Let me try to create an example for you. I'll be back
------------------------------
Peter Horsbøll Møller
Distinguished Engineer
Pitney Bowes Software & Data
Original Message:
Sent: 07-27-2020 08:32
From: George Corea
Subject: Mapbasic to add Thematic Legend in one frame
I am trying to work out how to add multiple thematic legends into a frame using mapbasic.
Create Designer Legend From Window 3015 Portrait Default Frame Style "#" Font ("Arial",0,8,0) Frame From Layer 1 Frame From Layer 4
Puts all the listed layers in but to the side of the map and I can't control where this should be and what the background should be as each theme is a separate element
.
If I add a frame first then just the first layer in the list is in the frame but the others aren't.
So
Create Frame (8.9717,0.5885) (11.2499, 3.6736) Name "No Content"
Add Designer Frame Window 3021 Frame From Layer 13 Title "" Into Id 11 Frame From Layer 32 Title "Water Mains Status" Frame From Layer 35 Title "Water Mains Status"
What is the 64 bit mapbasic to get every layer into the same frame? It is possible in 32bit but the way the print layouts are handled is different so that code doesn't work.
Thanks
------------------------------
George Corea
Mangoesmapping
------------------------------