chartjs / chartjs-plugin-annotation

Annotation plugin for Chart.js
MIT License
603 stars 325 forks source link

Add `beforeDraw` and `afterDraw` hooks to the annotations #744

Closed stockiNail closed 1 year ago

stockiNail commented 2 years ago

This PR is adding before/after draw hooks in order to allow the user to apply whatever needed customization on out-of the-box annotations. Fix #146.

The hooks will be invoked once for every annotation for each drawing cycle.

plugins: {
  annotation: {
    annotations: {
      line: {
        type: 'line',
        ....
        beforeDraw(context) {
          console.log('beforeDraw');
        },
        afterDraw(context) {
          console.log('afterDraw');
        },
      },
    }
  }
}
Hook Arguments Return type Default Description
beforeDraw context void annotation.beforeDraw invoked before the element drawing
afterDraw context void annotation.afterDraw invoked after the element drawing

Common hooks for all annotations can be set in the annotation plugin options:

plugins: {
  annotation: {
    beforeDraw(context) {
      console.log('Common beforeDraw');
    },
    afterDraw(context) {
      console.log('Common afterDraw');
    },
    annotations: {
      line: {
        type: 'line',
        ....
      },
    }
  }
}

TODO

stockiNail commented 2 years ago

I still don't like the counters How about making it simpler, just draw before/after the main element is drawn? We could add afterDrawChildren later, if it's really needed.

I think its reasonable But Let me take time to review the PR My memory is not so persistent and I dont remember so well what I wrote

stockiNail commented 2 years ago

How about making it simpler, just draw before/after the main element is drawn?

done! review applied!

No counters anymore!