The documentation of chartJS plugins is available here, however it is incomplete as there is no full description of each of the hooks:
afterInit
beforeUpdate (cancellable)
afterUpdate
beforeLayout (cancellable)
afterLayout
beforeDatasetsUpdate (cancellable)
afterDatasetsUpdate
beforeDatasetUpdate (cancellable)
afterDatasetUpdate
beforeRender (cancellable)
afterRender
beforeDraw (cancellable)
afterDraw
beforeDatasetsDraw (cancellable)
afterDatasetsDraw
beforeDatasetDraw (cancellable)
afterDatasetDraw
beforeEvent (cancellable)
afterEvent
resize
destroy
This list can be summarize to the following functions, yet the order is not clear for me:
- Update
- Layout
- DatasetsUpdate
- DatasetUpdate
- Render
- Draw
- DatasetsDraw
- DatasetDraw
- Event
- resize
- destroy
Some of the names might know what is the order of execution of the hooks.
Use case
I want to implement several behaviours on the charts that activate on certain conditions on the data, as an example, I want to update the legend labels when there is no data available, so, instead of displaying the name of the category in the legend, it would display a message saying no data.
To be sure, I have manage to implement plugins, but since the order is not clear for me, I keep getting things mixed up due to the order.
The documentation of chartJS plugins is available here, however it is incomplete as there is no full description of each of the hooks:
afterInit
beforeUpdate (cancellable)
afterUpdate
beforeLayout (cancellable)
afterLayout
beforeDatasetsUpdate (cancellable)
afterDatasetsUpdate
beforeDatasetUpdate (cancellable)
afterDatasetUpdate
beforeRender (cancellable)
afterRender
beforeDraw (cancellable)
afterDraw
beforeDatasetsDraw (cancellable)
afterDatasetsDraw
beforeDatasetDraw (cancellable)
afterDatasetDraw
beforeEvent (cancellable)
afterEvent
resize
destroy
This list can be summarize to the following functions, yet the order is not clear for me:
- Update
- Layout
- DatasetsUpdate
- DatasetUpdate
- Render
- Draw
- DatasetsDraw
- DatasetDraw
- Event
- resize
- destroy
Some of the names might know what is the order of execution of the hooks.
Use case
I want to implement several behaviours on the charts that activate on certain conditions on the data, as an example, I want to update the legend labels when there is no data available, so, instead of displaying the name of the category in the legend, it would display a message saying no data.
To be sure, I have manage to implement plugins, but since the order is not clear for me, I keep getting things mixed up due to the order.
Share Improve this question edited Mar 29, 2019 at 12:09 toto_tico asked Mar 29, 2019 at 11:13 toto_ticototo_tico 19k10 gold badges101 silver badges118 bronze badges 1- 1 Not quite the order, but I found the internal documentation of each of the hooks in the code. Still the order is not clear. – toto_tico Commented Mar 29, 2019 at 12:11
2 Answers
Reset to default 15I found the internal documentation of each of the hooks in the code (see Below for a prettified version). Not quite the order, but it can help figuring it out.
Note that some hooks have the same name but they differ in the parameters (you can check those directly in the code:
beforeInit
: Called before initializingchart
.afterInit
: Called afterchart
has been initialized and before the first update.beforeUpdate
: Called before updatingchart
. If any plugin returnsfalse
, the update is cancelled (and thus subsequent render(s)) until anotherupdate
is triggered.afterUpdate
: Called afterchart
has been updated and before rendering. Note that this hook will not be called if the chart update has been previously cancelled.beforeDatasetsUpdate
: Called before updating thechart
datasets. If any plugin returnsfalse
, the datasets update is cancelled until anotherupdate
is triggered. @since version 2.1.5afterDatasetsUpdate
: Called after thechart
datasets have been updated. Note that this hook will not be called if the datasets update has been previously cancelled.beforeDatasetUpdate
: Called before updating thechart
dataset at the givenargs.index
. If any plugin returnsfalse
, the datasets update is cancelled until anotherupdate
is triggered.afterDatasetUpdate
: Called after thechart
datasets at the givenargs.index
has been updated. Note that this hook will not be called if the datasets update has been previously cancelled.beforeLayout
: Called before laying outchart
. If any plugin returnsfalse
, the layout update is cancelled until anotherupdate
is triggered.afterLayout
: Called after thechart
has been layed out. Note that this hook will not be called if the layout update has been previously cancelled.beforeRender
: Called before renderingchart
. If any plugin returnsfalse
, the rendering is cancelled until anotherrender
is triggered.afterRender
: Called after thechart
has been fully rendered (and animation completed). Note that this hook will not be called if the rendering has been previously cancelled.beforeDraw
: Called before drawingchart
at every animation frame specified by the given easing value. If any plugin returnsfalse
, the frame drawing is cancelled until anotherrender
is triggered.afterDraw
: Called after thechart
has been drawn for the specific easing value. Note that this hook will not be called if the drawing has been previously cancelled.beforeDatasetsDraw
: Called before drawing thechart
datasets. If any plugin returnsfalse
, the datasets drawing is cancelled until anotherrender
is triggered.afterDatasetsDraw
: Called after thechart
datasets have been drawn. Note that this hook will not be called if the datasets drawing has been previously cancelled.beforeDatasetDraw
: Called before drawing thechart
dataset at the givenargs.index
(datasets are drawn in the reverse order). If any plugin returnsfalse
, the datasets drawing is cancelled until anotherrender
is triggered.afterDatasetDraw
: Called after thechart
datasets at the givenargs.index
have been drawn (datasets are drawn in the reverse order). Note that this hook will not be called if the datasets drawing has been previously cancelled.beforeTooltipDraw
: Called before drawing thetooltip
. If any plugin returnsfalse
, the tooltip drawing is cancelled until anotherrender
is triggered.afterTooltipDraw
: Called after drawing thetooltip
. Note that this hook will not be called if the tooltip drawing has been previously cancelled.beforeEvent
: Called before processing the specifiedevent
. If any plugin returnsfalse
, the event will be discarded.afterEvent
: Called after theevent
has been consumed. Note that this hook will not be called if theevent
has been previously discarded.resize
: Called after the chart as been resized.destroy
: Called after the chart as been destroyed.
old question but in chart.js 4.4.0, you can get a sense of order by stepping through Chart class (https://github.com/chartjs/Chart.js/blob/master/src/core/core.controller.js) which works out to:
- beforeUpdate
- BeforeElementsUpdate
- beforeLayout
- afterLayout
- reset(if animations disabled)
- beforeDatasetsUpdate
- beforeDatasetUpdate
- afterDatasetUpdate
- afterDatasetsUpdate
- afterUpdate
- beforeRender
- beforeDraw
- beforeDatasetsDraw
- beforeDatasetDraw
- afterDatasetDraw
- afterDatasetsDraw
- afterDraw
- afterRender
before/afterInit and before/afterEvent hooks will happen before the above and before/afterDestroy happens on its own.