Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the order in which the hooks (plugins) of Chart.js are executed?

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.

like image 627
toto_tico Avatar asked Mar 29 '19 11:03

toto_tico


1 Answers

I 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 initializing chart.
  • afterInit: Called after chart has been initialized and before the first update.
  • beforeUpdate: Called before updating chart. If any plugin returns false, the update is cancelled (and thus subsequent render(s)) until another update is triggered.
  • afterUpdate: Called after chart 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 the chart datasets. If any plugin returns false, the datasets update is cancelled until another update is triggered. @since version 2.1.5
  • afterDatasetsUpdate: Called after the chart datasets have been updated. Note that this hook will not be called if the datasets update has been previously cancelled.
  • beforeDatasetUpdate: Called before updating the chart dataset at the given args.index. If any plugin returns false, the datasets update is cancelled until another update is triggered.
  • afterDatasetUpdate: Called after the chart datasets at the given args.index has been updated. Note that this hook will not be called if the datasets update has been previously cancelled.
  • beforeLayout: Called before laying out chart. If any plugin returns false, the layout update is cancelled until another update is triggered.
  • afterLayout: Called after the chart has been layed out. Note that this hook will not be called if the layout update has been previously cancelled.
  • beforeRender: Called before rendering chart. If any plugin returns false, the rendering is cancelled until another render is triggered.
  • afterRender: Called after the chart 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 drawing chart at every animation frame specified by the given easing value. If any plugin returns false, the frame drawing is cancelled until another render is triggered.
  • afterDraw: Called after the chart 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 the chart datasets. If any plugin returns false, the datasets drawing is cancelled until another render is triggered.
  • afterDatasetsDraw: Called after the chart datasets have been drawn. Note that this hook will not be called if the datasets drawing has been previously cancelled.
  • beforeDatasetDraw: Called before drawing the chart dataset at the given args.index (datasets are drawn in the reverse order). If any plugin returns false, the datasets drawing is cancelled until another render is triggered.
  • afterDatasetDraw: Called after the chart datasets at the given args.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 the tooltip. If any plugin returns false, the tooltip drawing is cancelled until another render is triggered.
  • afterTooltipDraw: Called after drawing the tooltip. Note that this hook will not be called if the tooltip drawing has been previously cancelled.
  • beforeEvent: Called before processing the specified event. If any plugin returns false, the event will be discarded.
  • afterEvent: Called after the event has been consumed. Note that this hook will not be called if the event has been previously discarded.
  • resize: Called after the chart as been resized.
  • destroy: Called after the chart as been destroyed.
like image 105
toto_tico Avatar answered Oct 25 '22 01:10

toto_tico