-
Notifications
You must be signed in to change notification settings - Fork 11.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make
Chart.Animation/animations/Tooltip
importable (#5382)
- Loading branch information
1 parent
9fbac88
commit d284686
Showing
8 changed files
with
975 additions
and
921 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,172 +1,43 @@ | ||
/* global window: false */ | ||
'use strict'; | ||
|
||
var defaults = require('./core.defaults'); | ||
var Element = require('./core.element'); | ||
var helpers = require('../helpers/index'); | ||
|
||
defaults._set('global', { | ||
animation: { | ||
duration: 1000, | ||
easing: 'easeOutQuart', | ||
onProgress: helpers.noop, | ||
onComplete: helpers.noop | ||
} | ||
}); | ||
|
||
module.exports = function(Chart) { | ||
|
||
Chart.Animation = Element.extend({ | ||
chart: null, // the animation associated chart instance | ||
currentStep: 0, // the current animation step | ||
numSteps: 60, // default number of steps | ||
easing: '', // the easing to use for this animation | ||
render: null, // render function used by the animation service | ||
|
||
onAnimationProgress: null, // user specified callback to fire on each step of the animation | ||
onAnimationComplete: null, // user specified callback to fire when the animation finishes | ||
}); | ||
|
||
Chart.animationService = { | ||
frameDuration: 17, | ||
animations: [], | ||
dropFrames: 0, | ||
request: null, | ||
|
||
/** | ||
* @param {Chart} chart - The chart to animate. | ||
* @param {Chart.Animation} animation - The animation that we will animate. | ||
* @param {Number} duration - The animation duration in ms. | ||
* @param {Boolean} lazy - if true, the chart is not marked as animating to enable more responsive interactions | ||
*/ | ||
addAnimation: function(chart, animation, duration, lazy) { | ||
var animations = this.animations; | ||
var i, ilen; | ||
|
||
animation.chart = chart; | ||
|
||
if (!lazy) { | ||
chart.animating = true; | ||
} | ||
|
||
for (i = 0, ilen = animations.length; i < ilen; ++i) { | ||
if (animations[i].chart === chart) { | ||
animations[i] = animation; | ||
return; | ||
} | ||
} | ||
|
||
animations.push(animation); | ||
|
||
// If there are no animations queued, manually kickstart a digest, for lack of a better word | ||
if (animations.length === 1) { | ||
this.requestAnimationFrame(); | ||
} | ||
}, | ||
|
||
cancelAnimation: function(chart) { | ||
var index = helpers.findIndex(this.animations, function(animation) { | ||
return animation.chart === chart; | ||
}); | ||
|
||
if (index !== -1) { | ||
this.animations.splice(index, 1); | ||
chart.animating = false; | ||
} | ||
}, | ||
|
||
requestAnimationFrame: function() { | ||
var me = this; | ||
if (me.request === null) { | ||
// Skip animation frame requests until the active one is executed. | ||
// This can happen when processing mouse events, e.g. 'mousemove' | ||
// and 'mouseout' events will trigger multiple renders. | ||
me.request = helpers.requestAnimFrame.call(window, function() { | ||
me.request = null; | ||
me.startDigest(); | ||
}); | ||
} | ||
}, | ||
|
||
/** | ||
* @private | ||
*/ | ||
startDigest: function() { | ||
var me = this; | ||
var startTime = Date.now(); | ||
var framesToDrop = 0; | ||
var exports = module.exports = Element.extend({ | ||
chart: null, // the animation associated chart instance | ||
currentStep: 0, // the current animation step | ||
numSteps: 60, // default number of steps | ||
easing: '', // the easing to use for this animation | ||
render: null, // render function used by the animation service | ||
|
||
if (me.dropFrames > 1) { | ||
framesToDrop = Math.floor(me.dropFrames); | ||
me.dropFrames = me.dropFrames % 1; | ||
} | ||
|
||
me.advance(1 + framesToDrop); | ||
|
||
var endTime = Date.now(); | ||
|
||
me.dropFrames += (endTime - startTime) / me.frameDuration; | ||
|
||
// Do we have more stuff to animate? | ||
if (me.animations.length > 0) { | ||
me.requestAnimationFrame(); | ||
} | ||
}, | ||
|
||
/** | ||
* @private | ||
*/ | ||
advance: function(count) { | ||
var animations = this.animations; | ||
var animation, chart; | ||
var i = 0; | ||
|
||
while (i < animations.length) { | ||
animation = animations[i]; | ||
chart = animation.chart; | ||
|
||
animation.currentStep = (animation.currentStep || 0) + count; | ||
animation.currentStep = Math.min(animation.currentStep, animation.numSteps); | ||
|
||
helpers.callback(animation.render, [chart, animation], chart); | ||
helpers.callback(animation.onAnimationProgress, [animation], chart); | ||
|
||
if (animation.currentStep >= animation.numSteps) { | ||
helpers.callback(animation.onAnimationComplete, [animation], chart); | ||
chart.animating = false; | ||
animations.splice(i, 1); | ||
} else { | ||
++i; | ||
} | ||
} | ||
} | ||
}; | ||
|
||
/** | ||
* Provided for backward compatibility, use Chart.Animation instead | ||
* @prop Chart.Animation#animationObject | ||
* @deprecated since version 2.6.0 | ||
* @todo remove at version 3 | ||
*/ | ||
Object.defineProperty(Chart.Animation.prototype, 'animationObject', { | ||
get: function() { | ||
return this; | ||
} | ||
}); | ||
onAnimationProgress: null, // user specified callback to fire on each step of the animation | ||
onAnimationComplete: null, // user specified callback to fire when the animation finishes | ||
}); | ||
|
||
/** | ||
* Provided for backward compatibility, use Chart.Animation#chart instead | ||
* @prop Chart.Animation#chartInstance | ||
* @deprecated since version 2.6.0 | ||
* @todo remove at version 3 | ||
*/ | ||
Object.defineProperty(Chart.Animation.prototype, 'chartInstance', { | ||
get: function() { | ||
return this.chart; | ||
}, | ||
set: function(value) { | ||
this.chart = value; | ||
} | ||
}); | ||
// DEPRECATIONS | ||
|
||
/** | ||
* Provided for backward compatibility, use Chart.Animation instead | ||
* @prop Chart.Animation#animationObject | ||
* @deprecated since version 2.6.0 | ||
* @todo remove at version 3 | ||
*/ | ||
Object.defineProperty(exports.prototype, 'animationObject', { | ||
get: function() { | ||
return this; | ||
} | ||
}); | ||
|
||
}; | ||
/** | ||
* Provided for backward compatibility, use Chart.Animation#chart instead | ||
* @prop Chart.Animation#chartInstance | ||
* @deprecated since version 2.6.0 | ||
* @todo remove at version 3 | ||
*/ | ||
Object.defineProperty(exports.prototype, 'chartInstance', { | ||
get: function() { | ||
return this.chart; | ||
}, | ||
set: function(value) { | ||
this.chart = value; | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
/* global window: false */ | ||
'use strict'; | ||
|
||
var defaults = require('./core.defaults'); | ||
var helpers = require('../helpers/index'); | ||
|
||
defaults._set('global', { | ||
animation: { | ||
duration: 1000, | ||
easing: 'easeOutQuart', | ||
onProgress: helpers.noop, | ||
onComplete: helpers.noop | ||
} | ||
}); | ||
|
||
module.exports = { | ||
frameDuration: 17, | ||
animations: [], | ||
dropFrames: 0, | ||
request: null, | ||
|
||
/** | ||
* @param {Chart} chart - The chart to animate. | ||
* @param {Chart.Animation} animation - The animation that we will animate. | ||
* @param {Number} duration - The animation duration in ms. | ||
* @param {Boolean} lazy - if true, the chart is not marked as animating to enable more responsive interactions | ||
*/ | ||
addAnimation: function(chart, animation, duration, lazy) { | ||
var animations = this.animations; | ||
var i, ilen; | ||
|
||
animation.chart = chart; | ||
|
||
if (!lazy) { | ||
chart.animating = true; | ||
} | ||
|
||
for (i = 0, ilen = animations.length; i < ilen; ++i) { | ||
if (animations[i].chart === chart) { | ||
animations[i] = animation; | ||
return; | ||
} | ||
} | ||
|
||
animations.push(animation); | ||
|
||
// If there are no animations queued, manually kickstart a digest, for lack of a better word | ||
if (animations.length === 1) { | ||
this.requestAnimationFrame(); | ||
} | ||
}, | ||
|
||
cancelAnimation: function(chart) { | ||
var index = helpers.findIndex(this.animations, function(animation) { | ||
return animation.chart === chart; | ||
}); | ||
|
||
if (index !== -1) { | ||
this.animations.splice(index, 1); | ||
chart.animating = false; | ||
} | ||
}, | ||
|
||
requestAnimationFrame: function() { | ||
var me = this; | ||
if (me.request === null) { | ||
// Skip animation frame requests until the active one is executed. | ||
// This can happen when processing mouse events, e.g. 'mousemove' | ||
// and 'mouseout' events will trigger multiple renders. | ||
me.request = helpers.requestAnimFrame.call(window, function() { | ||
me.request = null; | ||
me.startDigest(); | ||
}); | ||
} | ||
}, | ||
|
||
/** | ||
* @private | ||
*/ | ||
startDigest: function() { | ||
var me = this; | ||
var startTime = Date.now(); | ||
var framesToDrop = 0; | ||
|
||
if (me.dropFrames > 1) { | ||
framesToDrop = Math.floor(me.dropFrames); | ||
me.dropFrames = me.dropFrames % 1; | ||
} | ||
|
||
me.advance(1 + framesToDrop); | ||
|
||
var endTime = Date.now(); | ||
|
||
me.dropFrames += (endTime - startTime) / me.frameDuration; | ||
|
||
// Do we have more stuff to animate? | ||
if (me.animations.length > 0) { | ||
me.requestAnimationFrame(); | ||
} | ||
}, | ||
|
||
/** | ||
* @private | ||
*/ | ||
advance: function(count) { | ||
var animations = this.animations; | ||
var animation, chart; | ||
var i = 0; | ||
|
||
while (i < animations.length) { | ||
animation = animations[i]; | ||
chart = animation.chart; | ||
|
||
animation.currentStep = (animation.currentStep || 0) + count; | ||
animation.currentStep = Math.min(animation.currentStep, animation.numSteps); | ||
|
||
helpers.callback(animation.render, [chart, animation], chart); | ||
helpers.callback(animation.onAnimationProgress, [animation], chart); | ||
|
||
if (animation.currentStep >= animation.numSteps) { | ||
helpers.callback(animation.onAnimationComplete, [animation], chart); | ||
chart.animating = false; | ||
animations.splice(i, 1); | ||
} else { | ||
++i; | ||
} | ||
} | ||
} | ||
}; |
Oops, something went wrong.