Skip to content

Commit 5d5e48d

Browse files
authored
Isolate properties / modes from animation options (#8332)
* Isolate properties / modes from animation options * tabs, something wrong with the linter * Update misleading variable name
1 parent 284e357 commit 5d5e48d

File tree

18 files changed

+563
-303
lines changed

18 files changed

+563
-303
lines changed

docs/docs/configuration/animations.mdx

Lines changed: 84 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -33,21 +33,21 @@ function example() {
3333
}]
3434
},
3535
options: {
36-
animation: {
37-
tension: {
38-
duration: 1000,
39-
easing: 'linear',
40-
from: 1,
41-
to: 0,
42-
loop: true
43-
}
44-
},
45-
scales: {
46-
y: { // defining min and max so hiding the dataset does not change scale range
47-
min: 0,
48-
max: 100
49-
}
36+
animations: {
37+
tension: {
38+
duration: 1000,
39+
easing: 'linear',
40+
from: 1,
41+
to: 0,
42+
loop: true
43+
}
44+
},
45+
scales: {
46+
y: { // defining min and max so hiding the dataset does not change scale range
47+
min: 0,
48+
max: 100
5049
}
50+
}
5151
}
5252
};
5353
const chart = new Chart(ctx, cfg);
@@ -77,24 +77,24 @@ function example() {
7777
}]
7878
},
7979
options: {
80-
animation: {
81-
show: {
82-
x: {
83-
from: 0
84-
},
85-
y: {
86-
from: 0
87-
}
80+
transitions: {
81+
show: {
82+
x: {
83+
from: 0
8884
},
89-
hide: {
90-
x: {
91-
to: 0
92-
},
93-
y: {
94-
to: 0
95-
}
85+
y: {
86+
from: 0
87+
}
88+
},
89+
hide: {
90+
x: {
91+
to: 0
92+
},
93+
y: {
94+
to: 0
9695
}
9796
}
97+
}
9898
}
9999
};
100100
const chart = new Chart(ctx, cfg);
@@ -107,10 +107,30 @@ function example() {
107107
</TabItem>
108108
</Tabs>
109109

110-
## Animation Configuration
110+
## Animation configuration
111+
112+
Animation configuration consists of 3 keys.
113+
114+
| Name | Type | Details
115+
| ---- | ---- | -------
116+
| animation | `object` | [animation](#animation)
117+
| animations | `object` | [animations](#animations)
118+
| transitions | `object` | [transitions](#transitions)
119+
120+
These keys can be configured in following paths:
111121

112-
The default configuration is defined here: <a href="https://github.com/chartjs/Chart.js/blob/master/src/core/core.animations.js#L6-L55" target="_blank">core.animations.js</a>
113-
Namespace: `options.animation`, the global options are defined in `Chart.defaults.animation`.
122+
* `` - chart options
123+
* `controllers[type]` - controller type options
124+
* `controllers[type].datasets` - dataset type options
125+
* `datasets[type]` - dataset type options
126+
127+
These paths are valid under `defaults` for global confuguration and `options` for instance configuration.
128+
129+
## animation
130+
131+
The default configuration is defined here: <a href="https://github.com/chartjs/Chart.js/blob/master/src/core/core.animations.js#L9-L56" target="_blank">core.animations.js</a>
132+
133+
Namespace: `options.animation`
114134

115135
| Name | Type | Default | Description
116136
| ---- | ---- | ------- | -----------
@@ -119,84 +139,65 @@ Namespace: `options.animation`, the global options are defined in `Chart.defaul
119139
| `debug` | `boolean` | `undefined` | Running animation count + FPS display in upper left corner of the chart.
120140
| `delay` | `number` | `undefined` | Delay before starting the animations.
121141
| `loop` | `boolean` | `undefined` | If set to `true`, the animations loop endlessly.
122-
| [[mode]](#animation-mode-configuration) | `object` | [defaults...](#default-modes) | Option overrides for update mode. Core modes: `'active'`, `'hide'`, `'reset'`, `'resize'`, `'show'`. See **Hide and show [mode]** example above.
123-
| [[property]](#animation-property-configuration) | `object` | `undefined` | Option overrides for a single element `[property]`. These have precedence over `[collection]`. See **Looping tension [property]** example above.
124-
| [[collection]](#animation-properties-collection-configuration) | `object` | [defaults...](#default-collections) | Option overrides for multiple properties, identified by `properties` array.
125142

126143
These defaults can be overridden in `options.animation` or `dataset.animation` and `tooltip.animation`. These keys are also [Scriptable](../general/options.md#scriptable-options).
127144

128-
## Animation mode configuration
145+
## animations
129146

130-
Mode option configures how an update mode animates the chart.
131-
The cores modes are `'active'`, `'hide'`, `'reset'`, `'resize'`, `'show'`.
132-
A custom mode can be used by passing a custom `mode` to [update](../developers/api.md#updatemode).
133-
A mode option is defined by the same options of the main [animation configuration](#animation-configuration).
147+
Animations options configures which element properties are animated and how.
148+
In addition to the main [animation configuration](#animation-configuration), the following options are available:
134149

135-
### Default modes
136-
137-
Namespace: `options.animation`
138-
139-
| Mode | Option | Value | Description
140-
| -----| ------ | ----- | -----
141-
| `active` | duration | 400 | Override default duration to 400ms for hover animations
142-
| `resize` | duration | 0 | Override default duration to 0ms (= no animation) for resize
143-
| `show` | colors | `{ type: 'color', properties: ['borderColor', 'backgroundColor'], from: 'transparent' }` | Colors are faded in from transparent when dataset is shown using legend / [api](../developers/api.md#showdatasetIndex).
144-
| `show` | visible | `{ type: 'boolean', duration: 0 }` | Dataset visiblity is immediately changed to true so the color transition from transparent is visible.
145-
| `hide` | colors | `{ type: 'color', properties: ['borderColor', 'backgroundColor'], to: 'transparent' }` | Colors are faded to transparent when dataset id hidden using legend / [api](../developers/api.md#hidedatasetIndex).
146-
| `hide` | visible | `{ type: 'boolean', easing: 'easeInExpo' }` | Visibility is changed to false at a very late phase of animation
147-
148-
## Animation property configuration
149-
150-
Property option configures which element property to use to animate the chart and its starting and ending values.
151-
A property option is defined by the same options of the main [animation configuration](#animation-configuration), adding the following ones:
152-
153-
Namespace: `options.animation[animation]`
150+
Namespace: `options.animations[animation]`
154151

155152
| Name | Type | Default | Description
156153
| ---- | ---- | ------- | -----------
154+
| `properties` | `string[]` | `key` | The property names this configuration applies to. Defaults to the key name of this object.
157155
| `type` | `string` | `typeof property` | Type of property, determines the interpolator used. Possible values: `'number'`, `'color'` and `'boolean'`. Only really needed for `'color'`, because `typeof` does not get that right.
158156
| `from` | `number`\|`Color`\|`boolean` | `undefined` | Start value for the animation. Current value is used when `undefined`
159157
| `to` | `number`\|`Color`\|`boolean` | `undefined` | End value for the animation. Updated value is used when `undefined`
160158
| `fn` | <code>&lt;T&gt;(from: T, to: T, factor: number) => T;</code> | `undefined` | Optional custom interpolator, instead of using a predefined interpolator from `type` |
161159

162-
## Animation properties collection configuration
163-
164-
Properties collection option configures which set of element properties to use to animate the chart.
165-
Collection can be named whatever you like, but should not collide with a `[property]` or `[mode]`.
166-
A properties collection option is defined by the same options as the [animation property configuration](#animation-property-configuration), adding the following one:
167-
168-
The animation properties collection configuration can be adjusted in the `options.animation[collection]` namespace.
169-
170-
| Name | Type | Default | Description
171-
| ---- | ---- | ------- | -----------
172-
| `properties` | `string[]` | `undefined` | Set of properties to use to animate the chart.
173-
174-
### Default collections
160+
### Default animations
175161

176162
| Name | Option | Value
177163
| ---- | ------ | -----
178-
| `numbers` | `type` | `'number'`
179164
| `numbers` | `properties` | `['x', 'y', 'borderWidth', 'radius', 'tension']`
165+
| `numbers` | `type` | `'number'`
166+
| `colors` | `properties` | `['color', 'borderColor', 'backgroundColor']`
180167
| `colors` | `type` | `'color'`
181-
| `colors` | `properties` | `['borderColor', 'backgroundColor']`
182-
183-
Direct property configuration overrides configuration of same property in a collection.
184-
185-
From collections, a property gets its configuration from first one that has its name in properties.
186168

187169
:::note
188-
These default collections are overridden by most dataset controllers.
170+
These default animations are overridden by most of the dataset controllers.
189171
:::
190172

173+
## transitions
174+
175+
The core transitions are `'active'`, `'hide'`, `'reset'`, `'resize'`, `'show'`.
176+
A custom transtion can be used by passing a custom `mode` to [update](../developers/api.md#updatemode).
177+
Transition extends the main [animation configuration](#animation-configuration) and [animations configuration](#animations-configuration).
178+
179+
### Default transitions
180+
181+
Namespace: `options.transitions[mode]`
182+
183+
| Mode | Option | Value | Description
184+
| -----| ------ | ----- | -----
185+
| `'active'` | animation.duration | 400 | Override default duration to 400ms for hover animations
186+
| `'resize'` | animation.duration | 0 | Override default duration to 0ms (= no animation) for resize
187+
| `'show'` | animations.colors | `{ type: 'color', properties: ['borderColor', 'backgroundColor'], from: 'transparent' }` | Colors are faded in from transparent when dataset is shown using legend / [api](../developers/api.md#showdatasetIndex).
188+
| `'show'` | animations.visible | `{ type: 'boolean', duration: 0 }` | Dataset visiblity is immediately changed to true so the color transition from transparent is visible.
189+
| `'hide'` | animations.colors | `{ type: 'color', properties: ['borderColor', 'backgroundColor'], to: 'transparent' }` | Colors are faded to transparent when dataset id hidden using legend / [api](../developers/api.md#hidedatasetIndex).
190+
| `'hide'` | animations.visible | `{ type: 'boolean', easing: 'easeInExpo' }` | Visibility is changed to false at a very late phase of animation
191+
191192
## Disabling animation
192193

193194
To disable an animation configuration, the animation node must be set to `false`, with the exception for animation modes which can be disabled by setting the `duration` to `0`.
194195

195196
```javascript
196-
chart.options.animation = false; // disables the whole animation
197-
chart.options.animation.active.duration = 0; // disables the animation for 'active' mode
198-
chart.options.animation.colors = false; // disables animation defined by the collection of 'colors' properties
199-
chart.options.animation.x = false; // disables animation defined by the 'x' property
197+
chart.options.animation = false; // disables all animations
198+
chart.options.animations.colors = false; // disables animation defined by the collection of 'colors' properties
199+
chart.options.animations.x = false; // disables animation defined by the 'x' property
200+
chart.options.transitions.active.animation.duration = 0; // disables the animation for 'active' mode
200201
```
201202

202203
## Easing

docs/docs/developers/api.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ myLineChart.data.datasets[0].data[2] = 50; // Would update the first dataset's v
2828
myLineChart.update(); // Calling update now animates the position of March from 90 to 50.
2929
```
3030

31-
A `mode` string can be provided to indicate what should be updated and what animation configuration should be used. Core calls this method using any of `'active'`, `'hide'`, `'reset'`, `'resize'`, `'show'` or `undefined`. `'none'` is also a supported mode for skipping animations for single update. Please see [animations](../configuration/animations.mdx) docs for more details.
31+
A `mode` string can be provided to indicate transition configuration should be used. Core calls this method using any of `'active'`, `'hide'`, `'reset'`, `'resize'`, `'show'` or `undefined`. `'none'` is also a supported mode for skipping animations for single update. Please see [animations](../configuration/animations.mdx) docs for more details.
3232

3333
Example:
3434

samples/animations/drop.html

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@
3333
labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'],
3434
datasets: [{
3535
label: 'My First dataset',
36-
animation: {
36+
animations: {
3737
y: {
3838
duration: 2000,
39-
delay: 100
39+
delay: 500
4040
}
4141
},
42-
backgroundColor: window.chartColors.red,
42+
backgroundColor: 'rgba(170,0,0,0.1)',
4343
borderColor: window.chartColors.red,
4444
data: [
4545
randomScalingFactor(),
@@ -50,7 +50,8 @@
5050
randomScalingFactor(),
5151
randomScalingFactor()
5252
],
53-
fill: false,
53+
fill: 1,
54+
tension: 0.5
5455
}, {
5556
label: 'My Second dataset',
5657
fill: false,
@@ -68,10 +69,17 @@
6869
}]
6970
},
7071
options: {
71-
animation: {
72+
animations: {
7273
y: {
7374
easing: 'easeInOutElastic',
74-
from: 0
75+
from: (ctx) => {
76+
if (ctx.type === 'data') {
77+
if (ctx.mode === 'default' && !ctx.dropped) {
78+
ctx.dropped = true;
79+
return 0;
80+
}
81+
}
82+
}
7583
}
7684
},
7785
responsive: true,

samples/animations/loop.html

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
}]
6363
},
6464
options: {
65-
animation: {
65+
animations: {
6666
radius: {
6767
duration: 400,
6868
easing: 'linear',
@@ -74,23 +74,18 @@
7474
hoverRadius: 6
7575
}
7676
},
77-
responsive: true,
77+
interaction: {
78+
mode: 'nearest',
79+
axis: 'x',
80+
intersect: false
81+
},
7882
plugins: {
7983
title: {
8084
display: true,
8185
text: 'Chart.js Line Chart'
8286
},
83-
tooltip: {
84-
mode: 'nearest',
85-
axis: 'x',
86-
intersect: false,
87-
},
88-
},
89-
hover: {
90-
mode: 'nearest',
91-
axis: 'x',
92-
intersect: false
9387
},
88+
responsive: true,
9489
scales: {
9590
x: {
9691
display: true,

src/controllers/controller.bar.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,7 @@ BarController.defaults = {
520520
datasets: {
521521
categoryPercentage: 0.8,
522522
barPercentage: 0.9,
523-
animation: {
523+
animations: {
524524
numbers: {
525525
type: 'number',
526526
properties: ['x', 'y', 'base', 'width', 'height']

src/controllers/controller.bubble.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,9 @@ BubbleController.id = 'bubble';
133133
BubbleController.defaults = {
134134
datasetElementType: false,
135135
dataElementType: 'point',
136-
animation: {
136+
animations: {
137137
numbers: {
138+
type: 'number',
138139
properties: ['x', 'y', 'borderWidth', 'radius']
139140
}
140141
},

src/controllers/controller.doughnut.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -329,15 +329,17 @@ DoughnutController.defaults = {
329329
datasetElementType: false,
330330
dataElementType: 'arc',
331331
animation: {
332-
numbers: {
333-
type: 'number',
334-
properties: ['circumference', 'endAngle', 'innerRadius', 'outerRadius', 'startAngle', 'x', 'y', 'offset', 'borderWidth']
335-
},
336332
// Boolean - Whether we animate the rotation of the Doughnut
337333
animateRotate: true,
338334
// Boolean - Whether we animate scaling the Doughnut from the centre
339335
animateScale: false
340336
},
337+
animations: {
338+
numbers: {
339+
type: 'number',
340+
properties: ['circumference', 'endAngle', 'innerRadius', 'outerRadius', 'startAngle', 'x', 'y', 'offset', 'borderWidth']
341+
},
342+
},
341343
aspectRatio: 1,
342344

343345
datasets: {

src/controllers/controller.polarArea.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,12 +122,14 @@ PolarAreaController.id = 'polarArea';
122122
PolarAreaController.defaults = {
123123
dataElementType: 'arc',
124124
animation: {
125+
animateRotate: true,
126+
animateScale: true
127+
},
128+
animations: {
125129
numbers: {
126130
type: 'number',
127131
properties: ['x', 'y', 'startAngle', 'endAngle', 'innerRadius', 'outerRadius']
128132
},
129-
animateRotate: true,
130-
animateScale: true
131133
},
132134
aspectRatio: 1,
133135
indexAxis: 'r',

src/core/core.animation.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export default class Animation {
2828

2929
this._active = true;
3030
this._fn = cfg.fn || interpolators[cfg.type || typeof from];
31-
this._easing = effects[cfg.easing || 'linear'];
31+
this._easing = effects[cfg.easing] || effects.linear;
3232
this._start = Math.floor(Date.now() + (cfg.delay || 0));
3333
this._duration = Math.floor(cfg.duration);
3434
this._loop = !!cfg.loop;

0 commit comments

Comments
 (0)