Skip to content

Enable initial animation on annotations #755

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 51 commits into from
Feb 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
28fa957
Enable initial animation on annotations
stockiNail Jun 1, 2022
172eb5b
fixes some CC issues
stockiNail Jun 1, 2022
2e357e1
apply review 1
stockiNail Jun 1, 2022
980f3b6
adds fixtures
stockiNail Jun 1, 2022
91c3ab7
adds test cases with callback
stockiNail Jun 1, 2022
56e0f55
improves code on default application and drawPoint function
stockiNail Jun 1, 2022
05aa1b5
adds test case on callback which returns undef
stockiNail Jun 1, 2022
2247bc1
adds animation spec to test that the annotation is really animating
stockiNail Jun 3, 2022
9910062
adds types
stockiNail Jun 3, 2022
92dfb51
adds doc
stockiNail Jun 3, 2022
781a2f6
adds samples
stockiNail Jun 3, 2022
6ca8aec
fixes and improves test case on init animation
stockiNail Jun 3, 2022
983f197
fixes lint
stockiNail Jun 3, 2022
3e70704
fixes multiple access to cart config in test case
stockiNail Jun 3, 2022
962ee4b
Add element diagrams to the annotation types guide
stockiNail Jun 6, 2022
05e619c
Merge branch 'master' of https://github.com/chartjs/chartjs-plugin-an…
stockiNail Jun 8, 2022
22a3aa3
Merge branch 'master' of https://github.com/chartjs/chartjs-plugin-an…
stockiNail Jun 8, 2022
f5186fe
Merge branch 'master' of https://github.com/chartjs/chartjs-plugin-an…
stockiNail Jun 8, 2022
22251c3
Merge remote-tracking branch 'origin/master' into initAnimation
stockiNail Jun 9, 2022
6d87201
Merge branch 'master' of https://github.com/chartjs/chartjs-plugin-an…
stockiNail Jul 21, 2022
84366b3
Merge branch 'master' of https://github.com/chartjs/chartjs-plugin-an…
stockiNail Jul 22, 2022
ec3a554
Merge branch 'master' of https://github.com/chartjs/chartjs-plugin-an…
stockiNail Jul 26, 2022
1324e43
Merge branch 'master' of https://github.com/chartjs/chartjs-plugin-an…
stockiNail Aug 4, 2022
693e84d
Merge branch 'master' of https://github.com/chartjs/chartjs-plugin-an…
stockiNail Aug 19, 2022
cc3fb5a
Merge remote-tracking branch 'origin/master' into initAnimation
stockiNail Aug 19, 2022
30b5941
Merge branch 'master' of https://github.com/chartjs/chartjs-plugin-an…
stockiNail Aug 19, 2022
b30c95e
Merge branch 'master' of https://github.com/chartjs/chartjs-plugin-an…
stockiNail Sep 28, 2022
ff2a323
Merge branch 'master' of https://github.com/chartjs/chartjs-plugin-an…
stockiNail Sep 28, 2022
8ac1896
Merge remote-tracking branch 'origin/master' into initAnimation
stockiNail Sep 28, 2022
8aea0ad
apply review
stockiNail Sep 28, 2022
b686c7a
Merge branch 'master' of https://github.com/chartjs/chartjs-plugin-an…
stockiNail Oct 28, 2022
30446eb
Merge branch 'master' of https://github.com/chartjs/chartjs-plugin-an…
stockiNail Nov 15, 2022
63e01f8
Merge branch 'master' of https://github.com/chartjs/chartjs-plugin-an…
stockiNail Nov 15, 2022
235712c
Merge branch 'master' of https://github.com/chartjs/chartjs-plugin-an…
stockiNail Nov 17, 2022
5114654
Merge branch 'master' of https://github.com/chartjs/chartjs-plugin-an…
stockiNail Jan 9, 2023
0672d27
Merge remote-tracking branch 'origin/master' into initAnimation
stockiNail Jan 9, 2023
e43a62e
Merge branch 'master' of https://github.com/chartjs/chartjs-plugin-an…
stockiNail Jan 27, 2023
5069380
Merge branch 'master' of https://github.com/chartjs/chartjs-plugin-an…
stockiNail Jan 27, 2023
7f2e747
changes initAnimation options name to init
stockiNail Jan 27, 2023
76294b6
Merge remote-tracking branch 'origin/master' into initAnimation
stockiNail Jan 30, 2023
f9835e1
Merge branch 'master' of https://github.com/chartjs/chartjs-plugin-an…
stockiNail Feb 4, 2023
dfcef02
Merge remote-tracking branch 'origin/master' into initAnimation
stockiNail Feb 4, 2023
f8cc9c6
Merge branch 'master' of https://github.com/chartjs/chartjs-plugin-an…
stockiNail Feb 6, 2023
b4fd4ec
Merge remote-tracking branch 'origin/master' into initAnimation
stockiNail Feb 6, 2023
65c55ec
Merge branch 'master' of https://github.com/chartjs/chartjs-plugin-an…
stockiNail Feb 24, 2023
67f2425
Merge remote-tracking branch 'origin/master' into initAnimation
stockiNail Feb 24, 2023
c552b5a
Merge branch 'master' of https://github.com/chartjs/chartjs-plugin-an…
stockiNail Feb 25, 2023
07463fa
Merge remote-tracking branch 'origin/master' into initAnimation
stockiNail Feb 25, 2023
e079753
Merge remote-tracking branch 'origin/master' into initAnimation
stockiNail Feb 25, 2023
8034582
Merge branch 'master' of https://github.com/chartjs/chartjs-plugin-an…
stockiNail Feb 25, 2023
b2f1faf
rebased
stockiNail Feb 25, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/.vuepress/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ module.exports = {
'box/disclosure',
'box/canvas',
'box/image',
'box/initAnim',
'box/gradient',
]
},
Expand Down Expand Up @@ -196,6 +197,7 @@ module.exports = {
'point/combined',
'point/outsideChartArea',
'point/shadow',
'point/initAnim',
]
},
{
Expand Down
31 changes: 30 additions & 1 deletion docs/guide/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,36 @@ The following options apply to all annotations unless they are overwritten on a

| Name | Type | [Scriptable](options.md#scriptable-options) | Default | Notes
| ---- | ---- | :----: | ---- | ----
| `drawTime` | `string` | Yes | `'afterDatasetsDraw'` | See [drawTime](options.md#draw-time).
| `drawTime` | `string` | Yes | `'afterDatasetsDraw'` | See [drawTime](options#draw-time).
| `init` | `boolean` | [See initial animation](#initial-animation) | `false` | Enable the animation to the annotations when they are drawing at chart initialization

### Initial animation

The `init` option is scriptable but it doesn't get the [options context](./options#option-context) as argument but a specific context because the element has not been initialized yet, when the callback is invoked.

This is the signature of the scriptable option:

```javascript
({chart, properties, options}) => void | boolean | AnnotationBoxModel
```

where the properties is the element model

```javascript
{
x: number,
y: number,
x2: number,
y2: number,
centerX: number,
centerY: number,
width: number,
height: number,
radius?: number
}
```

which can be used in the callback to return an object with the initial values of the element, to provide own initial animation.

## Events

Expand Down
1 change: 1 addition & 0 deletions docs/guide/types/_commonOptions.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ The following options are available for all annotations.
| [`borderShadowColor`](#styling) | [`Color`](../options.md#color) | Yes | `'transparent'`
| [`display`](#general) | `boolean` | Yes | `true`
| [`drawTime`](#general) | `string` | Yes | `'afterDatasetsDraw'`
| [`init`](../configuration.html#common) | `boolean` | [See initial animation](../configuration.html#initial-animation) | `undefined`
| [`id`](#general) | `string` | No | `undefined`
| [`shadowBlur`](#styling) | `number` | Yes | `0`
| [`shadowOffsetX`](#styling) | `number` | Yes | `0`
Expand Down
120 changes: 120 additions & 0 deletions docs/samples/box/initAnim.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
# Initial animations

```js chart-editor
// <block:setup:4>
const DATA_COUNT = 12;
const MIN = 10;
const MAX = 100;

const numberCfg = {count: DATA_COUNT, min: MIN, max: MAX};

const data = {
labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
datasets: [{
data: Utils.numbers(numberCfg)
}]
};
// </block:setup>

// <block:annotation1:1>
const annotation1 = {
type: 'box',
backgroundColor: 'rgba(0, 150, 0, 0.2)',
borderColor: 'rgba(0, 150, 0)',
borderRadius: 4,
borderWidth: 1,
init: true,
label: {
display: true,
content: 'Fade'
},
xMax: 6.5,
xMin: 4.5,
yMax: 60,
yMin: 40
};
// </block:annotation1>

// <block:annotation2:2>
const annotation2 = {
type: 'box',
backgroundColor: 'rgba(0, 150, 0, 0.2)',
borderColor: 'rgba(0, 150, 0)',
borderRadius: 4,
borderWidth: 1,
init: () => ({y: 0, y2: 0}),
label: {
display: true,
content: 'Flyin from top'
},
xMax: 2.5,
xMin: 0.5,
yMax: 30,
yMin: 10
};
// </block:annotation2>

// <block:annotation3:3>
const annotation3 = {
type: 'box',
backgroundColor: 'rgba(0, 150, 0, 0.2)',
borderColor: 'rgba(0, 150, 0)',
borderRadius: 4,
borderWidth: 1,
init: () => ({x: 0, x2: 0}),
label: {
display: true,
content: 'Flyin from left'
},
xMax: 10.5,
xMin: 8.5,
yMax: 90,
yMin: 70
};
// </block:annotation3>

/* <block:config:0> */
const config = {
type: 'line',
data,
options: {
scales: {
y: {
beginAtZero: true,
max: 100,
min: 0
}
},
plugins: {
annotation: {
common: {
drawTime: 'afterDraw'
},
annotations: {
annotation1,
annotation2,
annotation3
}
}
}
}
};
/* </block:config> */

const actions = [
{
name: 'Randomize',
handler: function(chart) {
chart.data.datasets.forEach(function(dataset, i) {
dataset.data = dataset.data.map(() => Utils.rand(MIN, MAX));
});
chart.update();
}
}
];

module.exports = {
actions: actions,
config: config,
};
```
156 changes: 156 additions & 0 deletions docs/samples/point/initAnim.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
# Initial animations

```js chart-editor
// <block:setup:4>
const DATA_COUNT = 12;
const MIN = 10;
const MAX = 100;

const numberCfg = {count: DATA_COUNT, min: MIN, max: MAX};

const data = {
labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
datasets: [{
data: Utils.numbers(numberCfg)
}]
};
// </block:setup>

// <block:annotation1:1>
const annotation1 = {
type: 'point',
backgroundColor: 'rgba(0, 150, 0, 0.2)',
borderColor: 'rgba(0, 150, 0)',
borderRadius: 4,
borderWidth: 1,
init: true,
label: {
display: true,
content: 'Fade',
textAlign: 'center'
},
radius: 40,
xMax: 6.5,
xMin: 4.5,
yMax: 60,
yMin: 40
};
const labelAnnotation1 = {
type: 'label',
init: true,
content: 'Fade',
xMax: 6.5,
xMin: 4.5,
yMax: 60,
yMin: 40
};
// </block:annotation1>

// <block:annotation2:2>
const annotation2 = {
type: 'point',
backgroundColor: 'rgba(0, 150, 0, 0.2)',
borderColor: 'rgba(0, 150, 0)',
borderRadius: 4,
borderWidth: 1,
init: () => ({centerY: 0}),
label: {
display: true,
content: 'Flyin from top',
textAlign: 'center'
},
radius: 40,
xMax: 2.5,
xMin: 0.5,
yMax: 30,
yMin: 10
};
const labelAnnotation2 = {
type: 'label',
init: () => ({y: 0, y2: 0, width: 0, height: 0}),
content: 'Flyin from top',
xMax: 2.5,
xMin: 0.5,
yMax: 30,
yMin: 10
};
// </block:annotation2>

// <block:annotation3:3>
const annotation3 = {
type: 'point',
backgroundColor: 'rgba(0, 150, 0, 0.2)',
borderColor: 'rgba(0, 150, 0)',
borderRadius: 4,
borderWidth: 1,
init: () => ({centerX: 0}),
label: {
display: true,
content: 'Flyin from left',
textAlign: 'center'
},
radius: 40,
xMax: 10.5,
xMin: 8.5,
yMax: 90,
yMin: 70
};
const labelAnnotation3 = {
type: 'label',
init: () => ({x: 0, x2: 0, width: 0, height: 0}),
content: 'Flyin from left',
xMax: 10.5,
xMin: 8.5,
yMax: 90,
yMin: 70
};
// </block:annotation3>

/* <block:config:0> */
const config = {
type: 'line',
data,
options: {
scales: {
y: {
beginAtZero: true,
max: 100,
min: 0
}
},
plugins: {
annotation: {
common: {
drawTime: 'afterDraw'
},
annotations: {
annotation1,
labelAnnotation1,
annotation2,
labelAnnotation2,
annotation3,
labelAnnotation3
}
}
}
}
};
/* </block:config> */

const actions = [
{
name: 'Randomize',
handler: function(chart) {
chart.data.datasets.forEach(function(dataset, i) {
dataset.data = dataset.data.map(() => Utils.rand(MIN, MAX));
});
chart.update();
}
}
];

module.exports = {
actions: actions,
config: config,
};
```
3 changes: 2 additions & 1 deletion src/annotation.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,15 @@ export default {
},
common: {
drawTime: 'afterDatasetsDraw',
init: false,
label: {
}
}
},

descriptors: {
_indexable: false,
_scriptable: (prop) => !hooks.includes(prop),
_scriptable: (prop) => !hooks.includes(prop) && prop !== 'init',
annotations: {
_allKeys: false,
_fallback: (prop, opts) => `elements.${annotationTypes[resolveType(opts.type)].id}`
Expand Down
Loading