Skip to content

Commit a359625

Browse files
committed
Merge remote-tracking branch 'origin/master' into innerLabelProto
Conflicts: src/helpers/helpers.options.js types/options.d.ts
2 parents b2cc907 + 84545be commit a359625

File tree

12 files changed

+1054
-856
lines changed

12 files changed

+1054
-856
lines changed

docs/guide/configuration.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ The `init` option is scriptable but it doesn't get the [options context](./optio
7575
This is the signature of the scriptable option:
7676

7777
```javascript
78-
({chart, properties, options}) => void | boolean | AnnotationBoxModel
78+
({chart, properties, options}) => void | boolean | AnnotationElement
7979
```
8080

8181
where the properties is the element model

docs/samples/point/initAnim.md

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,6 @@ const annotation1 = {
2424
borderRadius: 4,
2525
borderWidth: 1,
2626
init: true,
27-
label: {
28-
display: true,
29-
content: 'Fade',
30-
textAlign: 'center'
31-
},
3227
radius: 40,
3328
xMax: 6.5,
3429
xMin: 4.5,
@@ -54,11 +49,6 @@ const annotation2 = {
5449
borderRadius: 4,
5550
borderWidth: 1,
5651
init: () => ({centerY: 0}),
57-
label: {
58-
display: true,
59-
content: 'Flyin from top',
60-
textAlign: 'center'
61-
},
6252
radius: 40,
6353
xMax: 2.5,
6454
xMin: 0.5,
@@ -84,11 +74,6 @@ const annotation3 = {
8474
borderRadius: 4,
8575
borderWidth: 1,
8676
init: () => ({centerX: 0}),
87-
label: {
88-
display: true,
89-
content: 'Flyin from left',
90-
textAlign: 'center'
91-
},
9277
radius: 40,
9378
xMax: 10.5,
9479
xMin: 8.5,

package-lock.json

Lines changed: 1012 additions & 815 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "chartjs-plugin-annotation",
33
"homepage": "https://www.chartjs.org/chartjs-plugin-annotation/index",
44
"description": "Annotations for Chart.js",
5-
"version": "2.1.2",
5+
"version": "2.2.1",
66
"author": "Evert Timberg <[email protected]>",
77
"license": "MIT",
88
"main": "dist/chartjs-plugin-annotation.js",
@@ -47,7 +47,7 @@
4747
"@simonbrunel/vuepress-plugin-versions": "^0.2.0",
4848
"@typescript-eslint/eslint-plugin": "^5.51.0",
4949
"@typescript-eslint/parser": "^5.51.0",
50-
"chart.js": "^4.1.2",
50+
"chart.js": "^4.2.1",
5151
"chartjs-test-utils": "^0.5.0",
5252
"concurrently": "^7.6.0",
5353
"cross-env": "^7.0.3",

src/elements.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {Animations} from 'chart.js';
2-
import {isObject, defined} from 'chart.js/helpers';
2+
import {isObject, isArray, defined} from 'chart.js/helpers';
33
import {eventHooks} from './events';
44
import {elementHooks} from './hooks';
55
import {annotationTypes} from './types';
@@ -9,6 +9,8 @@ const directUpdater = {
99
};
1010

1111
const hooks = eventHooks.concat(elementHooks);
12+
const resolve = (value, optDefs) => isObject(optDefs) ? resolveObj(value, optDefs) : value;
13+
1214

1315
/**
1416
* @typedef { import("chart.js").Chart } Chart
@@ -131,7 +133,11 @@ function resolveObj(resolver, defs) {
131133
for (const prop of Object.keys(defs)) {
132134
const optDefs = defs[prop];
133135
const value = resolver[prop];
134-
result[prop] = isObject(optDefs) && !isIndexable(prop) ? resolveObj(value, optDefs) : value;
136+
if (isIndexable(prop) && isArray(value)) {
137+
result[prop] = value.map((item) => resolve(item, optDefs));
138+
} else {
139+
result[prop] = resolve(value, optDefs);
140+
}
135141
}
136142
return result;
137143
}

src/helpers/helpers.chart.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,9 @@ export function resolveLineProperties(chart, options) {
177177
* @param {boolean} [centerBased=false]
178178
* @returns {AnnotationBoxModel}
179179
*/
180-
export function resolveBoxAndLabelProperties(chart, options, centerBased) {
180+
export function resolveBoxAndLabelProperties(chart, options) {
181181
const properties = resolveBoxProperties(chart, options);
182-
properties.initProperties = initAnimationProperties(chart, properties, options, centerBased);
182+
properties.initProperties = initAnimationProperties(chart, properties, options);
183183
properties.elements = [{
184184
type: 'label',
185185
optionScope: 'label',

src/helpers/helpers.options.js

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,27 @@
1-
import {isObject, isFunction, isArray, toFont, valueOrDefault, defined, callback} from 'chart.js/helpers';
1+
import {isObject, isFunction, isArray, toFont, valueOrDefault, defined, callback as invoke} from 'chart.js/helpers';
22
import {clamp} from './helpers.core';
33

44
const isPercentString = (s) => typeof s === 'string' && s.endsWith('%');
55
const toPercent = (s) => parseFloat(s) / 100;
66
const toPositivePercent = (s) => clamp(toPercent(s), 0, 1);
77

8+
const boxAppering = (x, y) => ({x, y, x2: x, y2: y, width: 0, height: 0});
9+
const defaultInitAnimation = {
10+
box: (properties) => boxAppering(properties.centerX, properties.centerY),
11+
doughnutLabel: (properties) => boxAppering(properties.centerX, properties.centerY),
12+
ellipse: (properties) => ({centerX: properties.centerX, centerY: properties.centerX, radius: 0, width: 0, height: 0}),
13+
label: (properties) => boxAppering(properties.centerX, properties.centerY),
14+
line: (properties) => boxAppering(properties.x, properties.y),
15+
point: (properties) => ({centerX: properties.centerX, centerY: properties.centerY, radius: 0, width: 0, height: 0}),
16+
polygon: (properties) => boxAppering(properties.centerX, properties.centerY)
17+
};
18+
819
/**
920
* @typedef { import('chart.js').FontSpec } FontSpec
1021
* @typedef { import('chart.js').Point } Point
1122
* @typedef { import('chart.js').Padding } Padding
1223
* @typedef { import('../../types/element').AnnotationBoxModel } AnnotationBoxModel
24+
* @typedef { import('../../types/element').AnnotationElement } AnnotationElement
1325
* @typedef { import('../../types/options').AnnotationPointCoordinates } AnnotationPointCoordinates
1426
* @typedef { import('../../types/label').CoreLabelOptions } CoreLabelOptions
1527
* @typedef { import('../../types/label').LabelPositionObject } LabelPositionObject
@@ -151,17 +163,16 @@ function calculateLabelPosition(start, size, adjust = 0, position) {
151163
* @param {Chart} chart
152164
* @param {AnnotationBoxModel} properties
153165
* @param {CoreAnnotationOptions} options
154-
* @param {boolean} [centerBased=false]
155-
* @returns {AnnotationBoxModel}
166+
* @returns {AnnotationElement}
156167
*/
157-
export function initAnimationProperties(chart, properties, options, centerBased = false) {
168+
export function initAnimationProperties(chart, properties, options) {
158169
const initAnim = options.init;
159170
if (!initAnim) {
160171
return;
161172
} else if (initAnim === true) {
162-
return applyDefault(properties, centerBased);
173+
return applyDefault(properties, options);
163174
}
164-
return checkCallbackResult(properties, centerBased, callback(initAnim, [{chart, properties, options}]));
175+
return execCallback(chart, properties, options);
165176
}
166177

167178
/**
@@ -183,16 +194,15 @@ export function loadHooks(options, hooks, hooksContainer) {
183194
return activated;
184195
}
185196

186-
function applyDefault({centerX, centerY}, centerBased) {
187-
if (centerBased) {
188-
return {centerX, centerY, radius: 0, width: 0, height: 0};
189-
}
190-
return {x: centerX, y: centerY, x2: centerX, y2: centerY, width: 0, height: 0};
197+
function applyDefault(properties, options) {
198+
const type = options.type || 'line';
199+
return defaultInitAnimation[type](properties);
191200
}
192201

193-
function checkCallbackResult(properties, centerBased, result) {
202+
function execCallback(chart, properties, options) {
203+
const result = invoke(options.init, [{chart, properties, options}]);
194204
if (result === true) {
195-
return applyDefault(properties, centerBased);
205+
return applyDefault(properties, options);
196206
} else if (isObject(result)) {
197207
return result;
198208
}

src/types/ellipse.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export default class EllipseAnnotation extends Element {
4444
}
4545

4646
resolveElementProperties(chart, options) {
47-
return resolveBoxAndLabelProperties(chart, options, true);
47+
return resolveBoxAndLabelProperties(chart, options);
4848
}
4949

5050
}

src/types/point.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export default class PointAnnotation extends Element {
3939

4040
resolveElementProperties(chart, options) {
4141
const properties = resolvePointProperties(chart, options);
42-
properties.initProperties = initAnimationProperties(chart, properties, options, true);
42+
properties.initProperties = initAnimationProperties(chart, properties, options);
4343
return properties;
4444
}
4545
}

test/specs/animation.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ describe('Initial animation', function() {
44
box: 'x',
55
ellipse: 'width',
66
label: 'x',
7-
line: 'x',
7+
line: 'x2',
88
point: 'radius',
99
polygon: 'y'
1010
};

0 commit comments

Comments
 (0)