Skip to content

Commit 9f0d331

Browse files
Annotation tests work properly with log axes
We still have the problem that tests pass when run "individually" but not when all run.
1 parent 35ff5a7 commit 9f0d331

File tree

1 file changed

+26
-16
lines changed

1 file changed

+26
-16
lines changed

test/domain_ref_shapes_test.js

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -136,14 +136,9 @@ function setAxType(layout, axref, axtype) {
136136
// Calculate the ax value of an annotation given a particular desired scaling K
137137
// This also works with log axes by taking logs of each part of the sum, so that
138138
// the length in pixels is multiplied by the scalar
139-
function annaxscale(ac, axistype, c0, K) {
139+
function annaxscale(ac, c0, K) {
140140
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);
147142
return ret;
148143
}
149144

@@ -157,6 +152,14 @@ function annaxscale(ac, axistype, c0, K) {
157152
// same for yaxistype and yref
158153
function annotationTest(gd, layout, x0, y0, ax, ay, xref, yref, axref, ayref,
159154
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;
160163
// if xref != axref or axref === 'pixel' then ax is a value relative to
161164
// x0 but in pixels. Same for yref
162165
var xreftype = Axes.getRefType(xref);
@@ -182,8 +185,8 @@ function annotationTest(gd, layout, x0, y0, ax, ay, xref, yref, axref, ayref,
182185
ayref: ayref,
183186
};
184187
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),
187190
axref: axref,
188191
ayref: ayref,
189192
};
@@ -195,22 +198,22 @@ function annotationTest(gd, layout, x0, y0, ax, ay, xref, yref, axref, ayref,
195198
layout.annotations = [anno0, anno1];
196199
return Plotly.relayout(gd, layout).then(function (gd) {
197200
// 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);
200203
if (axpixels) {
201204
// no need to map the specified values to pixels (because that's what
202205
// they are already)
203206
xpixels = ax;
204207
} else {
205-
xpixels = mapAROCoordToPixel(gd.layout, 'xref', anno0, 'ax') -
208+
xpixels = mapAROCoordToPixel(gd.layout, 'xref', anno0, 'ax', 0, true) -
206209
xabspixels;
207210
}
208211
if (aypixels) {
209212
// no need to map the specified values to pixels (because that's what
210213
// they are already)
211214
ypixels = ay;
212215
} else {
213-
ypixels = mapAROCoordToPixel(gd.layout, 'yref', anno0, 'ay') -
216+
ypixels = mapAROCoordToPixel(gd.layout, 'yref', anno0, 'ay', 0, true) -
214217
yabspixels;
215218
}
216219
var annobbox0 = getSVGElemScreenBBox(findAROByColor(color0,"#"+gd.id));
@@ -252,10 +255,17 @@ function logAxisIfAxType(layoutIn, layoutOut, axid, axtype) {
252255
}
253256
}
254257

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
258263
// 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 :').
259269
function mapAROCoordToPixel(layout, axref, aro, c, offset, nolog) {
260270
var reftype = Axes.getRefType(aro[axref]);
261271
var axletter = axref[0];

0 commit comments

Comments
 (0)