Skip to content

Commit fd6a34c

Browse files
authored
Fix #556 (#558)
1 parent 7f31cd6 commit fd6a34c

20 files changed

+281
-276
lines changed

docs/guide/types/label.md

Lines changed: 2 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ The following options are available for label annotations.
7070
| [`font`](#styling) | [`Font`](../options#font) | Yes | `{}`
7171
| [`height`](#general) | `number`\|`string` | Yes | `undefined`
7272
| [`padding`](#general) | [`Padding`](../options#padding) | Yes | `6`
73-
| [`point`](#point) | `object` | Yes |
7473
| [`position`](#position) | `string`\|`{x: string, y: string}` | Yes | `'center'`
7574
| [`textAlign`](#general) | `string` | Yes | `'center'`
7675
| [`width`](#general) | `number`\|`string` | Yes | `undefined`
@@ -137,83 +136,9 @@ If this value is an object, the `x` property defines the horizontal alignment of
137136

138137
If this value is a number, it is applied to all corners of the rectangle (topLeft, topRight, bottomLeft, bottomRight). If this value is an object, the `topLeft` property defines the top-left corners border radius. Similarly, the `topRight`, `bottomLeft`, and `bottomRight` properties can also be specified. Omitted corners have radius of 0.
139138

140-
### Point
139+
## Callout
141140

142-
A point options can enable the selected point drawing.
143-
144-
Namespace: `options.annotations[annotationID].label.point`, it defines options for the point on the annotation label.
145-
146-
```js chart-editor
147-
/* <block:options:0> */
148-
const options = {
149-
plugins: {
150-
autocolors: false,
151-
annotation: {
152-
annotations: {
153-
label1: {
154-
type: 'label',
155-
xValue: 2.5,
156-
yValue: 60,
157-
backgroundColor: 'rgba(245,245,245)',
158-
content: ['In this point of time,', 'something happened'],
159-
textAlign: 'start',
160-
position: {
161-
y: 'start'
162-
},
163-
font: {
164-
size: 18
165-
},
166-
point: {
167-
enabled: true,
168-
backgroundColor: 'red',
169-
radius: 5
170-
}
171-
}
172-
}
173-
}
174-
}
175-
};
176-
/* </block:options> */
177-
178-
/* <block:config:1> */
179-
const config = {
180-
type: 'line',
181-
data: {
182-
labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'],
183-
datasets: [{
184-
label: 'My First Dataset',
185-
data: [65, 59, 80, 81, 56, 55, 40],
186-
fill: false,
187-
borderColor: 'rgb(75, 192, 192)',
188-
tension: 0.1
189-
}]
190-
},
191-
options
192-
};
193-
/* </block:config> */
194-
195-
module.exports = {
196-
config
197-
};
198-
```
199-
200-
All of these options can be [Scriptable](../options#scriptable-options).
201-
202-
| Name | Type | Default | Notes
203-
| ---- | ---- | :----: | ---- | ----
204-
| `enabled` | `boolean` | `false` | If true, the point is drawn.
205-
| `backgroundColor` | `Color` | `undefined` | Fill color of the selected point.
206-
| `borderColor` | [`Color`](../options#color) | `undefined` | Stroke color of the selected point.
207-
| `borderDash` | `number[]` | `[]` | Length and spacing of dashes of the border of the selected point. See [MDN](https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/setLineDash).
208-
| `borderDashOffset` | `number` | `0` | Offset for line dashes of the border of the selected point. See [MDN](https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/lineDashOffset).
209-
| `borderWidth` | `number` | `1` | Stroke width of the selected point.
210-
| `pointStyle` | [`PointStyle`](../options#point-style) | `'circle'` | Style of the point.
211-
| `radius` | `number` | `3` | Radius of the selected point.
212-
| `rotation` | `number` | `0` | Rotation of point, in degrees.
213-
214-
### Callout
215-
216-
A callout connects the label by a line to a point.
141+
A callout connects the label by a line to the selected point.
217142

218143
Namespace: `options.annotations[annotationID].label.callout`, it defines options for the callout on the annotation label.
219144

@@ -239,10 +164,6 @@ const options = {
239164
callout: {
240165
enabled: true,
241166
side: 10
242-
},
243-
point: {
244-
enabled: true,
245-
backgroundColor: 'rgba(245,245,245)'
246167
}
247168
}
248169
}

docs/samples/label/point.md

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Point
22

33
```js chart-editor
4-
// <block:setup:2>
4+
// <block:setup:3>
55
const DATA_COUNT = 12;
66
const MIN = 0;
77
const MAX = 100;
@@ -16,8 +16,8 @@ const data = {
1616
};
1717
// </block:setup>
1818

19-
// <block:annotation:1>
20-
const annotation = {
19+
// <block:annotation1:1>
20+
const annotation1 = {
2121
type: 'label',
2222
backgroundColor: 'rgba(245,245,245, 0.5)',
2323
content: (ctx) => 'Maximum value is ' + maxValue(ctx).toFixed(2),
@@ -45,7 +45,19 @@ const annotation = {
4545
yAdjust: -6,
4646
yValue: (ctx) => maxValue(ctx)
4747
};
48-
// </block:annotation>
48+
// </block:annotation1>
49+
50+
// <block:annotation2:2>
51+
const annotation2 = {
52+
type: 'point',
53+
backgroundColor: 'transparent',
54+
borderColor: (ctx) => ctx.chart.data.datasets[0].borderColor,
55+
pointStyle: 'rectRounded',
56+
radius: 10,
57+
xValue: (ctx) => maxLabel(ctx),
58+
yValue: (ctx) => maxValue(ctx)
59+
};
60+
// </block:annotation2>
4961

5062
/* <block:config:0> */
5163
const config = {
@@ -68,15 +80,16 @@ const config = {
6880
annotation: {
6981
clip: false,
7082
annotations: {
71-
annotation
83+
annotation1,
84+
annotation2
7285
}
7386
}
7487
}
7588
}
7689
};
7790
/* </block:config> */
7891

79-
// <block:utils:3>
92+
// <block:utils:4>
8093
function maxValue(ctx) {
8194
let max = 0;
8295
const dataset = ctx.chart.data.datasets[0];

src/types/label.js

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,11 @@
1-
import {drawBox, drawLabel, drawPoint, measureLabelSize, isLabelVisible, getChartPoint, getRectCenterPoint, toPosition, setBorderStyle, getSize, inBoxRange, inPointRange, isBoundToPoint, getChartRect, isPointVisible} from '../helpers';
1+
import {drawBox, drawLabel, measureLabelSize, isLabelVisible, getChartPoint, getRectCenterPoint, toPosition, setBorderStyle, getSize, inBoxRange, isBoundToPoint, getChartRect} from '../helpers';
22
import {color, toPadding} from 'chart.js/helpers';
33
import {Element} from 'chart.js';
44

55
export default class LabelAnnotation extends Element {
66

77
inRange(mouseX, mouseY, useFinalPosition) {
8-
if (!this.visible) {
9-
return false;
10-
}
11-
if (inBoxRange(mouseX, mouseY, this.getProps(['x', 'y', 'width', 'height'], useFinalPosition))) {
12-
return true;
13-
}
14-
const pointOpts = this.options.point;
15-
if (isPointVisible(pointOpts)) {
16-
const {pointX: x, pointY: y} = this.getProps(['pointX', 'pointY'], useFinalPosition);
17-
if (inPointRange({x: mouseX, y: mouseY}, {x, y}, pointOpts.radius)) {
18-
return true;
19-
}
20-
}
21-
return false;
8+
return this.visible && inBoxRange(mouseX, mouseY, this.getProps(['x', 'y', 'width', 'height'], useFinalPosition));
229
}
2310

2411
getCenterPoint(useFinalPosition) {
@@ -29,13 +16,12 @@ export default class LabelAnnotation extends Element {
2916
if (!this.visible) {
3017
return;
3118
}
32-
const {labelX, labelY, labelWidth, labelHeight, pointX, pointY, options} = this;
19+
const {labelX, labelY, labelWidth, labelHeight, options} = this;
3320
drawCallout(ctx, this);
3421
if (this.boxVisible) {
3522
drawBox(ctx, this, options);
3623
}
3724
drawLabel(ctx, {x: labelX, y: labelY, width: labelWidth, height: labelHeight}, options);
38-
drawPoint(ctx, {x: pointX, y: pointY}, options.point);
3925
}
4026

4127
resolveElementProperties(chart, options) {
@@ -87,17 +73,6 @@ LabelAnnotation.defaults = {
8773
side: 5,
8874
start: '50%',
8975
},
90-
point: {
91-
backgroundColor: undefined,
92-
borderColor: undefined,
93-
borderDash: [],
94-
borderDashOffset: 0,
95-
borderWidth: 1,
96-
enabled: false,
97-
pointStyle: 'circle',
98-
radius: 3,
99-
rotation: 0
100-
},
10176
color: 'black',
10277
content: null,
10378
display: true,

test/fixtures/label/boxLocation.js

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,21 @@ module.exports = {
4545
},
4646
position: {
4747
y: 'end'
48-
},
49-
point: {
50-
enabled: true,
51-
radius: 10,
52-
backgroundColor: 'red',
53-
borderColor: 'black',
54-
borderWidth: 1
5548
}
5649
},
50+
point1: {
51+
type: 'point',
52+
xScaleID: 'x',
53+
yScaleID: 'y',
54+
xMin: 1,
55+
yMin: 1,
56+
xMax: 8,
57+
yMax: 8,
58+
radius: 10,
59+
backgroundColor: 'red',
60+
borderColor: 'black',
61+
borderWidth: 1
62+
},
5763
box1: {
5864
type: 'box',
5965
xScaleID: 'x',
@@ -85,13 +91,19 @@ module.exports = {
8591
position: {
8692
y: 'end'
8793
},
88-
point: {
89-
enabled: true,
90-
radius: 10,
91-
backgroundColor: 'red',
92-
borderColor: 'black',
93-
borderWidth: 1,
94-
}
94+
},
95+
point2: {
96+
type: 'point',
97+
xScaleID: 'x',
98+
yScaleID: 'y',
99+
xMin: -8,
100+
yMin: -8,
101+
xMax: 1,
102+
yMax: 1,
103+
radius: 10,
104+
backgroundColor: 'red',
105+
borderColor: 'black',
106+
borderWidth: 1
95107
},
96108
box2: {
97109
type: 'box',

test/fixtures/label/calloutHorizontalPosition.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,13 @@ module.exports = {
5757
enabled: true,
5858
position: 'left',
5959
},
60-
point: {
61-
enabled: true
62-
}
60+
},
61+
point2: {
62+
type: 'point',
63+
xScaleID: 'x',
64+
yScaleID: 'y',
65+
xValue: 'May',
66+
yValue: 10
6367
}
6468
}
6569
}
241 Bytes
Loading

test/fixtures/label/calloutMargin.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,15 @@ module.exports = {
3636
enabled: true,
3737
margin: 10,
3838
position: 'right',
39-
},
40-
point: {
41-
enabled: true,
4239
}
4340
},
41+
point1: {
42+
type: 'point',
43+
xScaleID: 'x',
44+
yScaleID: 'y',
45+
xValue: 2.5,
46+
yValue: 20
47+
},
4448
text2: {
4549
type: 'label',
4650
xScaleID: 'x',
@@ -61,10 +65,14 @@ module.exports = {
6165
enabled: true,
6266
margin: 0,
6367
position: 'left',
64-
},
65-
point: {
66-
enabled: true,
6768
}
69+
},
70+
point2: {
71+
type: 'point',
72+
xScaleID: 'x',
73+
yScaleID: 'y',
74+
xValue: 'May',
75+
yValue: 10
6876
}
6977
}
7078
}
694 Bytes
Loading

test/fixtures/label/calloutPoint.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,15 @@ module.exports = {
3434
yAdjust: -100,
3535
callout: {
3636
enabled: true,
37-
},
38-
point: {
39-
enabled: true,
4037
}
38+
},
39+
point1: {
40+
type: 'point',
41+
xScaleID: 'x',
42+
yScaleID: 'y',
43+
xValue: 'February',
44+
yValue: 10,
45+
radius: 3
4146
}
4247
}
4348
}

0 commit comments

Comments
 (0)