1- import { isObject , isFunction , valueOrDefault , defined , callback } from 'chart.js/helpers' ;
1+ import { isObject , isFunction , 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+ 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 }
0 commit comments