Skip to content

Commit 84545be

Browse files
authored
Define default init animation logic to each annotation element (#863)
* Move default init animation logic to each annotation element * move defaults in an inner object of helpers * remove element passed argument * fixes JS doc * CC duplications * Returned value could be AnnotationElement to animate options
1 parent f447a34 commit 84545be

File tree

7 files changed

+30
-21
lines changed

7 files changed

+30
-21
lines changed

docs/guide/configuration.md

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

7575
```javascript
76-
({chart, properties, options}) => void | boolean | AnnotationBoxModel
76+
({chart, properties, options}) => void | boolean | AnnotationElement
7777
```
7878

7979
where the properties is the element model

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: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,24 @@
1-
import {isObject, isFunction, valueOrDefault, defined, callback} from 'chart.js/helpers';
1+
import {isObject, isFunction, 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+
ellipse: (properties) => ({centerX: properties.centerX, centerY: properties.centerX, radius: 0, width: 0, height: 0}),
12+
label: (properties) => boxAppering(properties.centerX, properties.centerY),
13+
line: (properties) => boxAppering(properties.x, properties.y),
14+
point: (properties) => ({centerX: properties.centerX, centerY: properties.centerY, radius: 0, width: 0, height: 0}),
15+
polygon: (properties) => boxAppering(properties.centerX, properties.centerY)
16+
};
17+
818
/**
919
* @typedef { import("chart.js").Chart } Chart
1020
* @typedef { import('../../types/element').AnnotationBoxModel } AnnotationBoxModel
21+
* @typedef { import('../../types/element').AnnotationElement } AnnotationElement
1122
* @typedef { import('../../types/options').AnnotationPointCoordinates } AnnotationPointCoordinates
1223
* @typedef { import('../../types/label').CoreLabelOptions } CoreLabelOptions
1324
* @typedef { import('../../types/label').LabelPositionObject } LabelPositionObject
@@ -93,17 +104,16 @@ export function isBoundToPoint(options) {
93104
* @param {Chart} chart
94105
* @param {AnnotationBoxModel} properties
95106
* @param {CoreAnnotationOptions} options
96-
* @param {boolean} [centerBased=false]
97-
* @returns {AnnotationBoxModel}
107+
* @returns {AnnotationElement}
98108
*/
99-
export function initAnimationProperties(chart, properties, options, centerBased = false) {
109+
export function initAnimationProperties(chart, properties, options) {
100110
const initAnim = options.init;
101111
if (!initAnim) {
102112
return;
103113
} else if (initAnim === true) {
104-
return applyDefault(properties, centerBased);
114+
return applyDefault(properties, options);
105115
}
106-
return checkCallbackResult(properties, centerBased, callback(initAnim, [{chart, properties, options}]));
116+
return execCallback(chart, properties, options);
107117
}
108118

109119
/**
@@ -125,16 +135,15 @@ export function loadHooks(options, hooks, hooksContainer) {
125135
return activated;
126136
}
127137

128-
function applyDefault({centerX, centerY}, centerBased) {
129-
if (centerBased) {
130-
return {centerX, centerY, radius: 0, width: 0, height: 0};
131-
}
132-
return {x: centerX, y: centerY, x2: centerX, y2: centerY, width: 0, height: 0};
138+
function applyDefault(properties, options) {
139+
const type = options.type || 'line';
140+
return defaultInitAnimation[type](properties);
133141
}
134142

135-
function checkCallbackResult(properties, centerBased, result) {
143+
function execCallback(chart, properties, options) {
144+
const result = invoke(options.init, [{chart, properties, options}]);
136145
if (result === true) {
137-
return applyDefault(properties, centerBased);
146+
return applyDefault(properties, options);
138147
} else if (isObject(result)) {
139148
return result;
140149
}

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
};

types/options.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Chart, Color, PointStyle, BorderRadius, CoreInteractionOptions } from 'chart.js';
22
import { AnnotationEvents, PartialEventContext, EventContext } from './events';
33
import { LabelOptions, BoxLabelOptions, LabelTypeOptions } from './label';
4-
import { AnnotationBoxModel } from './element';
4+
import { AnnotationBoxModel, AnnotationElement } from './element';
55

66
export type DrawTime = 'afterDraw' | 'afterDatasetsDraw' | 'beforeDraw' | 'beforeDatasetsDraw';
77

@@ -42,7 +42,7 @@ export interface CoreAnnotationOptions extends AnnotationEvents, ShadowOptions,
4242
borderWidth?: Scriptable<number, PartialEventContext>,
4343
display?: Scriptable<boolean, PartialEventContext>,
4444
drawTime?: Scriptable<DrawTime, PartialEventContext>,
45-
init?: boolean | ((chart: Chart, properties: AnnotationBoxModel, options: AnnotationOptions) => void | boolean | AnnotationBoxModel),
45+
init?: boolean | ((chart: Chart, properties: AnnotationBoxModel, options: AnnotationOptions) => void | boolean | AnnotationElement),
4646
id?: string,
4747
xMax?: Scriptable<ScaleValue, PartialEventContext>,
4848
xMin?: Scriptable<ScaleValue, PartialEventContext>,

0 commit comments

Comments
 (0)