Skip to content

Commit de84518

Browse files
committed
bar: update text rotation algorithm
* If rotation is needed: - inside texts are rotated counter-clockwise. - outside texts are rotated clockwise or counter-clockwise, so that the text is laid along the bar end.
1 parent efd6abd commit de84518

File tree

1 file changed

+23
-17
lines changed

1 file changed

+23
-17
lines changed

src/traces/bar/plot.js

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -303,32 +303,34 @@ function getTransformToMoveInsideBar(x0, x1, y0, y1, textBB, orientation) {
303303
else textpad = 0;
304304

305305
// compute rotation and scale
306-
var needsRotating,
306+
var rotate,
307307
scale;
308308

309309
if(textWidth <= barWidth && textHeight <= barHeight) {
310310
// no scale or rotation is required
311-
needsRotating = false;
311+
rotate = false;
312312
scale = 1;
313313
}
314314
else if(textWidth <= barHeight && textHeight <= barWidth) {
315315
// only rotation is required
316-
needsRotating = true;
316+
rotate = true;
317317
scale = 1;
318318
}
319319
else if((textWidth < textHeight) === (barWidth < barHeight)) {
320320
// only scale is required
321-
needsRotating = false;
321+
rotate = false;
322322
scale = Math.min(barWidth / textWidth, barHeight / textHeight);
323323
}
324324
else {
325325
// both scale and rotation are required
326-
needsRotating = true;
326+
rotate = true;
327327
scale = Math.min(barHeight / textWidth, barWidth / textHeight);
328328
}
329329

330+
if(rotate) rotate = -90; // rotate counter-clockwise
331+
330332
// compute text and target positions
331-
if(needsRotating) {
333+
if(rotate) {
332334
targetWidth = scale * textHeight;
333335
targetHeight = scale * textWidth;
334336
}
@@ -360,7 +362,7 @@ function getTransformToMoveInsideBar(x0, x1, y0, y1, textBB, orientation) {
360362
}
361363
}
362364

363-
return getTransform(textX, textY, targetX, targetY, scale, needsRotating);
365+
return getTransform(textX, textY, targetX, targetY, scale, rotate);
364366
}
365367

366368
function getTransformToMoveOutsideBar(x0, x1, y0, y1, textBB, orientation) {
@@ -388,28 +390,28 @@ function getTransformToMoveOutsideBar(x0, x1, y0, y1, textBB, orientation) {
388390
}
389391

390392
// compute rotation and scale
391-
var needsRotating,
393+
var rotate,
392394
scale;
393395
if(textWidth <= barWidth) {
394396
// no scale or rotation
395-
needsRotating = false;
397+
rotate = false;
396398
scale = 1;
397399
}
398400
else if(textHeight <= textWidth) {
399401
// only scale
400402
// (don't rotate to prevent having text perpendicular to the bar)
401-
needsRotating = false;
403+
rotate = false;
402404
scale = barWidth / textWidth;
403405
}
404406
else if(textHeight <= barWidth) {
405407
// only rotation
406-
needsRotating = true;
408+
rotate = true;
407409
scale = 1;
408410
}
409411
else {
410412
// both scale and rotation
411413
// (rotation prevents having text perpendicular to the bar)
412-
needsRotating = true;
414+
rotate = true;
413415
scale = barWidth / textHeight;
414416
}
415417

@@ -420,7 +422,7 @@ function getTransformToMoveOutsideBar(x0, x1, y0, y1, textBB, orientation) {
420422
targetHeight,
421423
targetX,
422424
targetY;
423-
if(needsRotating) {
425+
if(rotate) {
424426
targetWidth = scale * textBB.height;
425427
targetHeight = scale * textBB.width;
426428
}
@@ -434,28 +436,32 @@ function getTransformToMoveOutsideBar(x0, x1, y0, y1, textBB, orientation) {
434436
// bar end is on the left hand side
435437
targetX = x1 - textpad - targetWidth / 2;
436438
targetY = (y0 + y1) / 2;
439+
if(rotate) rotate = -90; // rotate counter-clockwise
437440
}
438441
else {
439442
targetX = x1 + textpad + targetWidth / 2;
440443
targetY = (y0 + y1) / 2;
444+
if(rotate) rotate = 90; // rotate clockwise
441445
}
442446
}
443447
else {
444448
if(y1 > y0) {
445449
// bar end is on the bottom
446450
targetX = (x0 + x1) / 2;
447451
targetY = y1 + textpad + targetHeight / 2;
452+
if(rotate) rotate = -90; // rotate counter-clockwise
448453
}
449454
else {
450455
targetX = (x0 + x1) / 2;
451456
targetY = y1 - textpad - targetHeight / 2;
457+
if(rotate) rotate = 90; // rotate clockwise
452458
}
453459
}
454460

455-
return getTransform(textX, textY, targetX, targetY, scale, needsRotating);
461+
return getTransform(textX, textY, targetX, targetY, scale, rotate);
456462
}
457463

458-
function getTransform(textX, textY, targetX, targetY, scale, needsRotating) {
464+
function getTransform(textX, textY, targetX, targetY, scale, rotate) {
459465
var transformScale,
460466
transformRotate,
461467
transformTranslate;
@@ -466,8 +472,8 @@ function getTransform(textX, textY, targetX, targetY, scale, needsRotating) {
466472
transformScale = '';
467473
}
468474

469-
transformRotate = (needsRotating) ?
470-
'rotate(-90 ' + textX + ' ' + textY + ') ' : '';
475+
transformRotate = (rotate) ?
476+
'rotate(' + rotate + ' ' + textX + ' ' + textY + ') ' : '';
471477

472478
// Note that scaling also affects the center of the text box
473479
var translateX = (targetX - scale * textX),

0 commit comments

Comments
 (0)