Grabbing the title in plugin for ChartJS

463 Views Asked by At

I want to figure out how to grab the title in a plugin in ChartJS I have the title in the configuration like so:

                    title: {
                        display: true,
                        fontSize: 10,                     
                        text: "This is my title",
                        padding: titlePadding,         
                    }

I was just curious if I want to do some afterDraw work, how do I grab it and alter it? I want to do some manipulation later in the event sequence.

1

There are 1 best solutions below

0
On

You can get the title text by calling chart.titleBlock.options.text, although even if you update it and call chart.update() it will render the updated title in the first draw tick but the other draw ticks it will use the normal defined title again, see fiddle: https://jsfiddle.net/Leelenaleee/kc8qua61/12/

You are better off using a scriptable option for the title as shown in de documentation sample of chart.js here: https://www.chartjs.org/docs/master/samples/line/stepped.html.

It takes a part of the config to show in the title as a dynamic part and internally chart.js handles it, you can also use external variables here.