|
1 | 1 | import {Element} from 'chart.js'; |
2 | 2 | import {toRadians, toPadding} from 'chart.js/helpers'; |
3 | | -import {clamp, scaleValue, rotated, drawBox, drawLabel, measureLabelSize, isLabelVisible, getRelativePosition} from '../helpers'; |
| 3 | +import {clamp, scaleValue, rotated, drawBox, drawLabel, measureLabelSize, getRelativePosition} from '../helpers'; |
4 | 4 |
|
5 | 5 | const PI = Math.PI; |
6 | 6 | const pointInLine = (p1, p2, t) => ({x: p1.x + t * (p2.x - p1.x), y: p1.y + t * (p2.y - p1.y)}); |
@@ -65,13 +65,16 @@ export default class LineAnnotation extends Element { |
65 | 65 | return (sqr(x - xx) + sqr(y - yy)) < epsilon; |
66 | 66 | } |
67 | 67 |
|
| 68 | + // TODO: make private in v2 |
68 | 69 | labelIsVisible(useFinalPosition, chartArea) { |
69 | | - if (!this.labelVisible) { |
| 70 | + const labelOpts = this.options.label; |
| 71 | + if (!chartArea || !labelOpts || !labelOpts.enabled) { |
70 | 72 | return false; |
71 | 73 | } |
72 | | - return !chartArea || isLineInArea(this.getProps(['x', 'y', 'x2', 'y2'], useFinalPosition), chartArea); |
| 74 | + return isLineInArea(this.getProps(['x', 'y', 'x2', 'y2'], useFinalPosition), chartArea); |
73 | 75 | } |
74 | 76 |
|
| 77 | + // TODO: make private in v2 |
75 | 78 | isOnLabel(mouseX, mouseY, useFinalPosition) { |
76 | 79 | if (!this.labelIsVisible(useFinalPosition)) { |
77 | 80 | return false; |
@@ -115,11 +118,31 @@ export default class LineAnnotation extends Element { |
115 | 118 | } |
116 | 119 |
|
117 | 120 | drawLabel(ctx, chartArea) { |
118 | | - if (this.labelIsVisible(false, chartArea)) { |
119 | | - ctx.save(); |
120 | | - applyLabel(ctx, this); |
121 | | - ctx.restore(); |
| 121 | + if (!this.labelIsVisible(false, chartArea)) { |
| 122 | + return; |
122 | 123 | } |
| 124 | + const {labelX, labelY, labelWidth, labelHeight, labelRotation, labelPadding, labelTextSize, options: {label}} = this; |
| 125 | + |
| 126 | + ctx.save(); |
| 127 | + ctx.translate(labelX, labelY); |
| 128 | + ctx.rotate(labelRotation); |
| 129 | + |
| 130 | + const boxRect = { |
| 131 | + x: -(labelWidth / 2), |
| 132 | + y: -(labelHeight / 2), |
| 133 | + width: labelWidth, |
| 134 | + height: labelHeight |
| 135 | + }; |
| 136 | + drawBox(ctx, boxRect, label); |
| 137 | + |
| 138 | + const labelTextRect = { |
| 139 | + x: -(labelWidth / 2) + labelPadding.left + label.borderWidth / 2, |
| 140 | + y: -(labelHeight / 2) + labelPadding.top + label.borderWidth / 2, |
| 141 | + width: labelTextSize.width, |
| 142 | + height: labelTextSize.height |
| 143 | + }; |
| 144 | + drawLabel(ctx, labelTextRect, label); |
| 145 | + ctx.restore(); |
123 | 146 | } |
124 | 147 |
|
125 | 148 | resolveElementProperties(chart, options) { |
@@ -155,9 +178,9 @@ export default class LineAnnotation extends Element { |
155 | 178 | const properties = inside |
156 | 179 | ? limitLineToArea({x, y}, {x: x2, y: y2}, chart.chartArea) |
157 | 180 | : {x, y, x2, y2, width: Math.abs(x2 - x), height: Math.abs(y2 - y)}; |
| 181 | + |
158 | 182 | const label = options.label; |
159 | | - properties.labelVisible = !!isLabelVisible(label); |
160 | | - if (properties.labelVisible) { |
| 183 | + if (label && label.content) { |
161 | 184 | return loadLabelRect(properties, chart, label); |
162 | 185 | } |
163 | 186 | return properties; |
@@ -243,30 +266,6 @@ function calculateAutoRotation(line) { |
243 | 266 | return rotation > PI / 2 ? rotation - PI : rotation < PI / -2 ? rotation + PI : rotation; |
244 | 267 | } |
245 | 268 |
|
246 | | -function applyLabel(ctx, line) { |
247 | | - const {labelX, labelY, labelWidth, labelHeight, labelRotation, labelPadding, labelTextSize, options} = line; |
248 | | - const label = options.label; |
249 | | - |
250 | | - ctx.translate(labelX, labelY); |
251 | | - ctx.rotate(labelRotation); |
252 | | - |
253 | | - const boxRect = { |
254 | | - x: -(labelWidth / 2), |
255 | | - y: -(labelHeight / 2), |
256 | | - width: labelWidth, |
257 | | - height: labelHeight |
258 | | - }; |
259 | | - drawBox(ctx, boxRect, label); |
260 | | - |
261 | | - const labelTextRect = { |
262 | | - x: -(labelWidth / 2) + labelPadding.left + label.borderWidth / 2, |
263 | | - y: -(labelHeight / 2) + labelPadding.top + label.borderWidth / 2, |
264 | | - width: labelTextSize.width, |
265 | | - height: labelTextSize.height |
266 | | - }; |
267 | | - drawLabel(ctx, labelTextRect, label); |
268 | | -} |
269 | | - |
270 | 269 | // TODO: v2 remove support for xPadding and yPadding |
271 | 270 | function getPadding(padding, xPadding, yPadding) { |
272 | 271 | let tempPadding = padding; |
|
0 commit comments