# legend position

I try to calculate the legend/colorscale position in percent of the plot area but it is not working using the ABSX and ABSY.

Function PrintLegendPosition(graph, name)
string graph, name

string anchorCode, exterior, Xpos, Ypos
variable absX, absY, fracx, fracy

string info = annotationinfo(graph, name, 1)
string flags = StringByKey("FLAGS", info, ":", ";", 1)

GetWindow \$graph, gsize // Look up the size of the graph window, in points
variable g_left = V_left
variable g_right = V_right
variable g_top = V_top
variable g_bottom = V_bottom

GetWindow \$graph, psize // look up the plot size in points
variable p_left = V_left
variable p_right = V_right
variable p_top = V_top
variable p_bottom = V_bottom

anchorcode = StringByKey("A", flags, "=", "/", 1)
exterior = StringByKey("E", flags, "=", "/", 1)

xPos = StringByKey("X", flags, "=", "/", 1)
yPos = StringByKey("Y", flags, "=", "/", 1)

absx = str2num(StringByKey("ABSX", info, ":", ";", 1))
absy = str2num(StringByKey("ABSY", info, ":", ";", 1))
fracx = (absx - p_left) / (p_right - p_left)
fracy = (absy - p_bottom) / (p_top - p_bottom)

print fracx, fracy
End

This is not getting anywhere. absx and absy sometimes are so wide out of range that the percentage gets 1,000% or so where it should rather be something like 0.5

Not sure though, if I should respect xPos or yPos and the exterior flag probably should trigger using gsize.

Can anyone help me out?

I tried your code and realized that I do not understand what you want to do at all. So you have a legend which is pinned to the exterior graph window and you want to extract the relative position in the complete window and not just the graph area?

Also, your code is incomplete in the sense that you nowhere use the information extracted from 'flags', which I believe is crucial when finding the legend position. For now fracx, fracy just give out the fractional position inside the graph area of the selected anchor point (not that I understand your formula, I just tested around a bit). This is more or less the same as the info provided by xpos, ypos (maybe inverted in some cases). So, first tell us a bit more about what you want to do.

Sorry for putting unused elements in my code here. It is not good for readability. I understand that. The unused parts are for WIP state only and are left overs from some experimenting with the "formula"

The current "formula" is not a fancy one and should be straight forward:

Consider to the top left corner of the graph window (0,0) and the bottom right corner of the graph window (1,1), this function gives out the position of the legend relative to this definition. It mostly works (I have attached my testing Experiment)

Sometimes, though, the absX and absY are rushung up to 4000 after the experiment start. Some runs later, everything is fine again. I guess it is a bug but after putting around 3h into it, i'm giving up.

I am specifically looking for a formula where absX and absY is avoided.

I could also give a lot more information if you want to, but I don't want to flood the forum here.

legendtest.pxp (4.01 MB)

I ended up with a workaround that feels a bit hacky:

xPos = NumberByKey("X", flags, "=", "/", 1)
yPos = NumberByKey("Y", flags, "=", "/", 1)

fracy = yPos / 100
if(strsearch(anchorcode, "C", 0) > -1)
fracy *= -1
endif
fracx = xPos / 100
if(strsearch(anchorcode, "R", 0) > -1)
fracx = 1 - fracx
endif
if(strsearch(anchorcode, "M", 0) > -1)
fracx += 0.5
endif

Thank you for clearing that up. If the problematic behavior only happens sometimes on start and you think it's a bug, then the Igor team may help out. But it seems you already settled for a workable solution. BTW, I do not find you code hacky at all. Basically, you are just using the most direct approach by tapping into the legend position information.

the center ("C") part is not right, btw. but I can live without using center anchors for now. I think the main problem in the IP implementation is that there is no differentiatiation between the anchor that lives on the scalebar/legend itself and the anchor that is attached on the window. Both anchors are basically set with the same flag. That may be convenient at first look but should probably be differntiated, e.g to have a RC anchor on the graph but a TL anchor on the scalebar.

@jjweimer that is a good solution to work around the (complicated) anchor to position calculation but I want to preserve the anchor of the legend. The anchor in the window then needs re-calculating to a "fixed anchor" at TL.

If I would change the anchor to TL, then I would need to calculate the offset to the "old anchor" that lives on the legend with the width and length of the legend which does not make it easier.

@ukos ... OK. I thought you might want to reposition the legend to a NEW position that is a percentage of the graph. I agree, when you want to preserve the current position but report it relative using a percent relative to the current anchor, you likely have to do the grunt work.

* Switch all of the anchors to LT (do NOT change any of the anchor values)
* Do a graph update
--> I *think* this will keep everything positioned on the graph but will reset the position values internally (i.e in the graph recreation macro)
* From here, redo the percentage calculations using the easier LT common

I someday hope that we will have a feature to be able to position anchors in percentages relative to internal graph (width,height) parameters rather than always in absolute pixels or points.

/X and /Y *are* values that are a percentage of the graph plot area's width and height. Not absolute pixels or points. So maybe use /X and /Y instead of ABSX and ABSY?

/X and /Y are reported in AnnotationInfo's FLAGS section.

Take a look at this help topic:

`DisplayHelpTopic "Textbox, Legend, and Color Scale Positioning in a Graph"`

@JimProuty If you have a look at the code, you will see that I already found these values.

The problem is that the anchor for these values seems to change together with the anchor of the legend which makes it necessary to do some calculations to get the position of the legend anchor with respect to of the top left corner of the graph window.