Vue-chartjs: How can I access the vue component instance inside callback of options.tooltips?

Created on 19 Feb 2019  ·  9Comments  ·  Source: apertureless/vue-chartjs

First of all, Thank You for this wonderful package 😍 . This package made my day ❤️ .

I'm in a situation where I need to construct the tooltip data using props. So I have used tooltips.callbacks.label option referred from docs.

Inside the label: function(tooltipItem,data){} I need to use props passed from the parent component, but what I'm getting is tooltip instance for this (As per the docs).

Is there any way to get VueComponet instance inside that function? Thanks in Advance 😃

Environment

  • vue.js version: ^2.5.16
  • vue-chart.js version: ^3.4.0
  • npm version: 6.4.1
❓ question

Most helpful comment

¯_(ツ)_/¯

Just like I said:

So put your options in a computed property and use arrow functions and you should be fine.

https://codepen.io/apertureless/pen/BZKBdX

Instead of

label: function(tooltipItem,data){}

use

label: (tooltipItem, data) => {
}

All 9 comments

Well, this has nothing to to with either chart.js or vue-chartjs.
Thats Javascript basics. this in callbacks always refers to the method that calls the callback. In this case this will refer to the window object / chartjs object and not to the vue compontent prototype.

There are enough resources and tutorials on how to handle this in callbacks.

The easiest way is to use arrow functions or .bind(this).
So put your options in a computed property and use arrow functions and you should be fine.

@theenadayalank - did you get a chance to implement this? if so, would you mind sharing the code.

¯_(ツ)_/¯

Just like I said:

So put your options in a computed property and use arrow functions and you should be fine.

https://codepen.io/apertureless/pen/BZKBdX

Instead of

label: function(tooltipItem,data){}

use

label: (tooltipItem, data) => {
}

@apertureless it helped, thank you! quick question - i see you are passing currency prop in your example with options. I need to pass another prop to component but for datacollection calculations without passing whole data prop itself. Would you mind sharing what is the best way to do this? let me know if you want me to share the code.

I don't really understand what you want to achieve.
Please provide an example.

@apertureless from your example: https://codepen.io/apertureless/pen/BZKBdX
i want currency () to be used in data/function call - data: [this.getRandomInt(this.currency)....

This will not work. You can't access props in your data.
Just use a computed property.

@apertureless any plans/workaround to improve this? the use case for your example would be - i have the same chart for different currencies and i want to derive data in my datacollection based on the prop as the filter and show all charts on the same page:


Hi @apertureless - any word on this one? it doesn't look efficient to duplicate datacollection multiple times just because we can't pass this prop to filter the data. I am sure there is a way to do it more efficient - i just don't know it and i guess you are the best person to help with it :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Tirke picture Tirke  ·  4Comments

egorzekov picture egorzekov  ·  4Comments

Scalpel78 picture Scalpel78  ·  4Comments

Sykomaniac picture Sykomaniac  ·  3Comments

m-jooibar picture m-jooibar  ·  4Comments