|
1 | 1 | import {Element} from 'chart.js'; |
2 | 2 | import {toPadding} from 'chart.js/helpers'; |
3 | | -import {drawBox, drawLabel, measureLabelSize, isLabelVisible, getRectCenterPoint, getChartRect, toPosition, inBoxRange} from '../helpers'; |
| 3 | +import {drawBox, drawLabel, getRelativePosition, measureLabelSize, isLabelVisible, getRectCenterPoint, getChartRect, toPosition, inBoxRange} from '../helpers'; |
4 | 4 |
|
5 | 5 | export default class BoxAnnotation extends Element { |
6 | 6 | inRange(mouseX, mouseY, useFinalPosition) { |
@@ -94,24 +94,28 @@ BoxAnnotation.defaultRoutes = { |
94 | 94 | function calculateX(box, labelSize, position, padding) { |
95 | 95 | const {x: start, x2: end, width: size, options} = box; |
96 | 96 | const {xAdjust: adjust, borderWidth} = options.label; |
97 | | - return calculatePosition({start, end, size}, {position: position.x, padding: padding.left, adjust, borderWidth, size: labelSize.width}); |
| 97 | + return calculatePosition({start, end, size}, { |
| 98 | + position: position.x, |
| 99 | + padding: {start: padding.left, end: padding.right}, |
| 100 | + adjust, borderWidth, |
| 101 | + size: labelSize.width |
| 102 | + }); |
98 | 103 | } |
99 | 104 |
|
100 | 105 | function calculateY(box, labelSize, position, padding) { |
101 | 106 | const {y: start, y2: end, height: size, options} = box; |
102 | 107 | const {yAdjust: adjust, borderWidth} = options.label; |
103 | | - return calculatePosition({start, end, size}, {position: position.y, padding: padding.top, adjust, borderWidth, size: labelSize.height}); |
| 108 | + return calculatePosition({start, end, size}, { |
| 109 | + position: position.y, |
| 110 | + padding: {start: padding.top, end: padding.bottom}, |
| 111 | + adjust, borderWidth, |
| 112 | + size: labelSize.height |
| 113 | + }); |
104 | 114 | } |
105 | 115 |
|
106 | 116 | function calculatePosition(boxOpts, labelOpts) { |
107 | | - const {start, end, size} = boxOpts; |
108 | | - const {position, padding, adjust, borderWidth} = labelOpts; |
109 | | - const margin = padding + (borderWidth / 2) + adjust; |
110 | | - if (position === 'start') { |
111 | | - return start + margin; |
112 | | - } else if (position === 'end') { |
113 | | - return end - labelOpts.size - margin; |
114 | | - } |
115 | | - return start + (size - labelOpts.size) / 2; |
| 117 | + const {start, end} = boxOpts; |
| 118 | + const {position, padding: {start: padStart, end: padEnd}, adjust, borderWidth} = labelOpts; |
| 119 | + const availableSize = end - borderWidth - start - padStart - padEnd - labelOpts.size; |
| 120 | + return start + borderWidth / 2 + adjust + padStart + getRelativePosition(availableSize, position); |
116 | 121 | } |
117 | | - |
|
0 commit comments