@@ -136,14 +136,9 @@ function setAxType(layout, axref, axtype) {
136
136
// Calculate the ax value of an annotation given a particular desired scaling K
137
137
// This also works with log axes by taking logs of each part of the sum, so that
138
138
// the length in pixels is multiplied by the scalar
139
- function annaxscale ( ac , axistype , c0 , K ) {
139
+ function annaxscale ( ac , c0 , K ) {
140
140
var ret ;
141
- if ( axistype === 'log' ) {
142
- ret = Math . pow ( 10 , Math . log10 ( c0 ) + 2 * ( Math . log10 ( ac ) - Math . log10 (
143
- c0 ) ) ) ;
144
- } else {
145
- ret = c0 + 2 * ( ac - c0 ) ;
146
- }
141
+ ret = c0 + 2 * ( ac - c0 ) ;
147
142
return ret ;
148
143
}
149
144
@@ -157,6 +152,14 @@ function annaxscale(ac, axistype, c0, K) {
157
152
// same for yaxistype and yref
158
153
function annotationTest ( gd , layout , x0 , y0 , ax , ay , xref , yref , axref , ayref ,
159
154
xaxistype , yaxistype , xid , yid ) {
155
+ // Take the log of values corresponding to log axes. This is because the
156
+ // test is designed to make predicting the pixel positions easy, and it's
157
+ // easiest when we work with the logarithm of values on log axes (doubling
158
+ // the log value doubles the pixel value, etc.).
159
+ x0 = xaxistype === 'log' ? Math . log10 ( x0 ) : x0 ;
160
+ y0 = yaxistype === 'log' ? Math . log10 ( y0 ) : y0 ;
161
+ ax = xaxistype === 'log' ? Math . log10 ( ax ) : ax ;
162
+ ay = yaxistype === 'log' ? Math . log10 ( ay ) : ay ;
160
163
// if xref != axref or axref === 'pixel' then ax is a value relative to
161
164
// x0 but in pixels. Same for yref
162
165
var xreftype = Axes . getRefType ( xref ) ;
@@ -182,8 +185,8 @@ function annotationTest(gd, layout, x0, y0, ax, ay, xref, yref, axref, ayref,
182
185
ayref : ayref ,
183
186
} ;
184
187
var opts1 = {
185
- ax : axpixels ? 2 * ax : annaxscale ( ax , xaxistype , x0 , 2 ) ,
186
- ay : aypixels ? 2 * ay : annaxscale ( ay , yaxistype , y0 , 2 ) ,
188
+ ax : axpixels ? 2 * ax : annaxscale ( ax , x0 , 2 ) ,
189
+ ay : aypixels ? 2 * ay : annaxscale ( ay , y0 , 2 ) ,
187
190
axref : axref ,
188
191
ayref : ayref ,
189
192
} ;
@@ -195,22 +198,22 @@ function annotationTest(gd, layout, x0, y0, ax, ay, xref, yref, axref, ayref,
195
198
layout . annotations = [ anno0 , anno1 ] ;
196
199
return Plotly . relayout ( gd , layout ) . then ( function ( gd ) {
197
200
// the choice of anno1 or anno0 is arbitrary
198
- var xabspixels = mapAROCoordToPixel ( gd . layout , 'xref' , anno1 , 'x' ) ;
199
- var yabspixels = mapAROCoordToPixel ( gd . layout , 'yref' , anno1 , 'y' ) ;
201
+ var xabspixels = mapAROCoordToPixel ( gd . layout , 'xref' , anno1 , 'x' , 0 , true ) ;
202
+ var yabspixels = mapAROCoordToPixel ( gd . layout , 'yref' , anno1 , 'y' , 0 , true ) ;
200
203
if ( axpixels ) {
201
204
// no need to map the specified values to pixels (because that's what
202
205
// they are already)
203
206
xpixels = ax ;
204
207
} else {
205
- xpixels = mapAROCoordToPixel ( gd . layout , 'xref' , anno0 , 'ax' ) -
208
+ xpixels = mapAROCoordToPixel ( gd . layout , 'xref' , anno0 , 'ax' , 0 , true ) -
206
209
xabspixels ;
207
210
}
208
211
if ( aypixels ) {
209
212
// no need to map the specified values to pixels (because that's what
210
213
// they are already)
211
214
ypixels = ay ;
212
215
} else {
213
- ypixels = mapAROCoordToPixel ( gd . layout , 'yref' , anno0 , 'ay' ) -
216
+ ypixels = mapAROCoordToPixel ( gd . layout , 'yref' , anno0 , 'ay' , 0 , true ) -
214
217
yabspixels ;
215
218
}
216
219
var annobbox0 = getSVGElemScreenBBox ( findAROByColor ( color0 , "#" + gd . id ) ) ;
@@ -252,10 +255,17 @@ function logAxisIfAxType(layoutIn, layoutOut, axid, axtype) {
252
255
}
253
256
}
254
257
255
- // axref can be xref or yref
256
- // c can be x0, x1, y0, y1
257
- // offset allows adding something to the coordinate before converting, say if
258
+ // {layout} is required to map to pixels using its domain, range and size
259
+ // {axref} can be xref or yref
260
+ // {aro} is the components object where c and axref will be looked up
261
+ // {c} can be x0, x1, y0, y1
262
+ // {offset} allows adding something to the coordinate before converting, say if
258
263
// you want to map the point on the other side of a square
264
+ // {nolog} if set to true, the log of a range value will not be taken before
265
+ // computing its pixel position. This is useful for components whose positions
266
+ // are specified in log coordinates (i.e., images and annotations).
267
+ // You can tell I first wrote this function for shapes only and then learned
268
+ // later this was the case for images and annotations :').
259
269
function mapAROCoordToPixel ( layout , axref , aro , c , offset , nolog ) {
260
270
var reftype = Axes . getRefType ( aro [ axref ] ) ;
261
271
var axletter = axref [ 0 ] ;
0 commit comments