Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can I access the vue component instance inside callback of options.tooltips? #475

Closed
theenadayalank opened this issue Feb 19, 2019 · 9 comments

Comments

@theenadayalank
Copy link

theenadayalank commented Feb 19, 2019

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
@apertureless
Copy link
Owner

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.

@ivanvorona
Copy link

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

@apertureless
Copy link
Owner

apertureless commented Mar 8, 2019

¯_(ツ)_/¯

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) => {
}

@ivanvorona
Copy link

ivanvorona commented Mar 12, 2019

@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.

@apertureless
Copy link
Owner

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

@ivanvorona
Copy link

@apertureless from your example: https://codepen.io/apertureless/pen/BZKBdX
i want currency (<line-chart currency="€":chart-data="message" :width="400" :height="200">) to be used in data/function call - data: [this.getRandomInt(this.currency)....

@apertureless
Copy link
Owner

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

@ivanvorona
Copy link

@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:
<line-chart currency="EUR":chart-data="message" :width="400" :height="200">
<line-chart currency="USD":chart-data="message" :width="400" :height="200">
<line-chart currency="CAD":chart-data="message" :width="400" :height="200">

@ivanvorona
Copy link

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 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants