Skip to content
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

Enable drawTime option as integer to relate the annotation drawing to a specific dataset drawing #889

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/guide/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ 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#draw-time).
| `drawTime` | `string`\|`number` | 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
Expand Down
4 changes: 3 additions & 1 deletion docs/guide/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ module.exports = {

## Draw Time

The `drawTime` option for an annotation determines where in the chart lifecycle the drawing occurs. Four potential options are available:
The `drawTime` option for an annotation determines where in the chart lifecycle the drawing occurs. Four potential predefined options are available:

| Option | Notes
| ---- | ----
Expand All @@ -92,6 +92,8 @@ The `drawTime` option for an annotation determines where in the chart lifecycle
| `'afterDatasetsDraw'` | Occurs after drawing of datasets but before items such as the tooltip
| `'afterDraw'` | After other drawing is completed.

Furthermore, the `drawTime` option can be set as number which represents the dataset index used to draw the annotation, before that dataset will be.

## Option Context

The option context is used to give contextual information when resolving options and only applies to scriptable options. The object is preserved, so it can be used to store and pass information between calls / options.
Expand Down
2 changes: 1 addition & 1 deletion docs/guide/types/_commonInnerLabel.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ All of these options can be [Scriptable](../options.md#scriptable-options)
| [`color`](#fonts-and-colors) | [`Color`\|`Color[]`](../options#color) | `'black'` | Text color.
| `content` | `string`\|`string[]`\|[`Image`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/Image)\|[`HTMLCanvasElement`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement) | `null` | The content to show in the label.
| `display` | `boolean` | `false` | Whether or not the label is shown.
| `drawTime` | `string` | `options.drawTime` | See [drawTime](../options#draw-time). Defaults to the annotation draw time if unset
| `drawTime` | `string`\|`number` | `options.drawTime` | See [drawTime](../options#draw-time). Defaults to the annotation draw time if unset
| [`font`](#fonts-and-colors) | [`Font`\|`Font[]`](../options#font) | `{ weight: 'bold' }` | Label font
| `height` | `number`\|`string` | `undefined` | Overrides the height of the image or canvas element. Could be set in pixel by a number, or in percentage of current height of image or canvas element by a string. If undefined, uses the height of the image or canvas element. It is used only when the content is an image or canvas element.
| `opacity` | `number` | `undefined` | Overrides the opacity of the image or canvas element. Could be set a number in the range 0.0 to 1.0, inclusive. If undefined, uses the opacity of the image or canvas element. It is used only when the content is an image or canvas element.
Expand Down
2 changes: 1 addition & 1 deletion docs/guide/types/_commonOptions.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ The following options are available for all annotations.
| [`borderDashOffset`](#styling) | `number` | Yes | `0`
| [`borderShadowColor`](#styling) | [`Color`](../options.md#color) | Yes | `'transparent'`
| [`display`](#general) | `boolean` | Yes | `true`
| [`drawTime`](#general) | `string` | Yes | `'afterDatasetsDraw'`
| [`drawTime`](#general) | `string`\|`number` | 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`
Expand Down
2 changes: 1 addition & 1 deletion docs/guide/types/line.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ All of these options can be [Scriptable](../options.md#scriptable-options)
| [`color`](#fonts-and-colors) | [`Color`\|`Color[]`](../options#color) | `'#fff'` | Text color.
| `content` | `string`\|`string[]`\|[`Image`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/Image)\|[`HTMLCanvasElement`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement) | `null` | The content to show in the label.
| `display` | `boolean` | `false` | Whether or not the label is shown.
| `drawTime` | `string` | `options.drawTime` | See [drawTime](../options#draw-time). Defaults to the line annotation draw time if unset.
| `drawTime` | `string`\|`number` | `options.drawTime` | See [drawTime](../options#draw-time). Defaults to the line annotation draw time if unset.
| [`font`](#fonts-and-colors) | [`Font`\|`Font[]`](../options#font) | `{ weight: 'bold' }` | Label font.
| `height` | `number`\|`string` | `undefined` | Overrides the height of the image or canvas element. Could be set in pixel by a number, or in percentage of current height of image or canvas element by a string. If undefined, uses the height of the image or canvas element. It is used only when the content is an image or canvas element.
| `opacity` | `number` | `undefined` | Overrides the opacity of the image or canvas element. Could be set a number in the range 0.0 to 1.0, inclusive. If undefined, uses the opacity of the image or canvas element. It is used only when the content is an image or canvas element.
Expand Down
4 changes: 4 additions & 0 deletions src/annotation.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ export default {
draw(chart, 'afterDatasetsDraw', options.clip);
},

beforeDatasetDraw(chart, _args, options) {
draw(chart, _args.index, options.clip);
},

beforeDraw(chart, _args, options) {
draw(chart, 'beforeDraw', options.clip);
},
Expand Down
127 changes: 127 additions & 0 deletions test/fixtures/box/drawTime.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
module.exports = {
tolerance: 0.0055,
config: {
type: 'bar',
data: {
labels: ['a', 'b', 'c', 'd', 'e', 'f'],
datasets: [{
data: [0, 5, 10, 15, 20, 22],
backgroundColor: 'gray'
}, {
data: [0, 5, 10, 15, 20, 22],
backgroundColor: 'lightGray'
}, {
data: [0, 5, 10, 15, 20, 22],
backgroundColor: 'lightGray'
}]
},
options: {
scales: {
x: {
display: false
},
y: {
display: false,
min: 0,
max: 25
}
},
animation: false,
plugins: {
legend: false,
tooltip: false,
annotation: {
annotations: {
beforeDraw: {
type: 'box',
drawTime: 'beforeDraw',
xMin: 0.5,
xMax: 1.5,
yMin: 0.5,
yMax: 4.5,
backgroundColor: 'rgb(33, 101, 171)',
label: {
display: true,
drawTime: 'afterDraw',
content: 'beforeDraw'
}
},
afterDraw: {
type: 'box',
drawTime: 'afterDraw',
xMin: 1.5,
xMax: 2.5,
yMin: 5.5,
yMax: 9.5,
backgroundColor: 'rgb(33, 101, 171)',
label: {
display: true,
drawTime: 'afterDraw',
content: 'afterDraw'
}
},
beforeDatasetsDraw: {
type: 'box',
drawTime: 'beforeDatasetsDraw',
xMin: 2.5,
xMax: 3.5,
yMin: 10.5,
yMax: 14.5,
backgroundColor: 'rgb(33, 101, 171)',
label: {
display: true,
drawTime: 'afterDraw',
content: 'beforeDatasetsDraw'
}
},
afterDatasetsDraw: {
type: 'box',
drawTime: 'afterDatasetsDraw',
xMin: 3.5,
xMax: 4.5,
yMin: 15.5,
yMax: 19.5,
backgroundColor: 'rgb(33, 101, 171)',
label: {
display: true,
drawTime: 'afterDraw',
content: 'afterDatasetsDraw'
}
},
dataset0: {
type: 'box',
drawTime: 0,
xMin: 4.5,
xMax: 5.5,
yMin: 10.5,
yMax: 14.5,
backgroundColor: 'rgb(33, 101, 171)',
label: {
display: true,
drawTime: 'afterDraw',
content: ['dataset', 'index 0']
}
},
dataset1: {
type: 'box',
drawTime: 1,
xMin: 3.5,
xMax: 4.5,
yMin: 5.5,
yMax: 9.5,
backgroundColor: 'rgb(33, 101, 171)',
label: {
display: true,
drawTime: 'afterDraw',
content: ['dataset', 'index 1']
}
}
}
}
}
}
},
options: {
spriteText: true
}
};
Binary file added test/fixtures/box/drawTime.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
121 changes: 121 additions & 0 deletions test/fixtures/box/labelDrawTime.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
module.exports = {
tolerance: 0.0055,
config: {
type: 'bar',
data: {
labels: ['a', 'b', 'c', 'd', 'e', 'f'],
datasets: [{
data: [0, 5, 10, 15, 20, 22],
backgroundColor: 'gray'
}, {
data: [0, 5, 10, 15, 20, 22],
backgroundColor: 'lightGray'
}, {
data: [0, 5, 10, 15, 20, 22],
backgroundColor: 'lightGray'
}]
},
options: {
scales: {
x: {
display: false
},
y: {
display: false,
min: 0,
max: 25
}
},
animation: false,
plugins: {
legend: false,
tooltip: false,
annotation: {
annotations: {
beforeDraw: {
type: 'box',
drawTime: 'beforeDraw',
xMin: 0.5,
xMax: 1.5,
yMin: 0.5,
yMax: 4.5,
backgroundColor: 'rgb(33, 101, 171)',
label: {
display: true,
content: 'beforeDraw'
}
},
afterDraw: {
type: 'box',
drawTime: 'afterDraw',
xMin: 1.5,
xMax: 2.5,
yMin: 5.5,
yMax: 9.5,
backgroundColor: 'rgb(33, 101, 171)',
label: {
display: true,
content: 'afterDraw'
}
},
beforeDatasetsDraw: {
type: 'box',
drawTime: 'beforeDatasetsDraw',
xMin: 2.5,
xMax: 3.5,
yMin: 10.5,
yMax: 14.5,
backgroundColor: 'rgb(33, 101, 171)',
label: {
display: true,
content: 'beforeDatasetsDraw'
}
},
afterDatasetsDraw: {
type: 'box',
drawTime: 'afterDatasetsDraw',
xMin: 3.5,
xMax: 4.5,
yMin: 15.5,
yMax: 19.5,
backgroundColor: 'rgb(33, 101, 171)',
label: {
display: true,
content: 'afterDatasetsDraw'
}
},
dataset0: {
type: 'box',
drawTime: 0,
xMin: 4.5,
xMax: 5.5,
yMin: 10.5,
yMax: 14.5,
backgroundColor: 'rgb(33, 101, 171)',
label: {
display: true,
content: ['dataset', 'index 0']
}
},
dataset1: {
type: 'box',
drawTime: 1,
xMin: 3.5,
xMax: 4.5,
yMin: 5.5,
yMax: 9.5,
backgroundColor: 'rgb(33, 101, 171)',
label: {
display: true,
content: ['dataset', 'index 1']
}
}
}
}
}
}
},
options: {
spriteText: true
}
};
Binary file added test/fixtures/box/labelDrawTime.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.