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' ;
22import { clamp } from './helpers.core' ;
33
44const isPercentString = ( s ) => typeof s === 'string' && s . endsWith ( '%' ) ;
55const toPercent = ( s ) => parseFloat ( s ) / 100 ;
66const 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 }
0 commit comments