Is it possible to select an element after you have called for it with the call() method? Trying to see if I can select the tip and then apply the attribute, ‘data-year’, to it. I can’t do this in the definition of the tip itself as there is no data being passed into it. Should I be using the ‘this’ keyword somehow? Thank you for reading
This is how I’m currently trying to select the tip.
@JohnnyBizzel Thanks you for your post. I should’ve mentioned that I was using the d3-tip library and not doing it this way. I mainly wanted to know how to get attributes onto the tip while still using the library.
I can’t do this in the definition of the tip itself as there is no data being passed into it. Should I be using the ‘this’ keyword somehow?
I think the problem you are having might be rooted in 3 aspects:
JS basics.
Functional programming.
Declaration order.
The call method is in this case a JS function method. It will assign/lend a function coming to an existing object/class from another function/object argument of the call.
Now, due to the order you are suggesting when declaring the methods, you might have not passed any additional functionality to the borrowing function when making the call for tip.
It is one way. Any additional change you made over the borrower function afterwards won’t modify tip: at that point if you want to change something related to tip, you will have to do it over tip.
That is the same for ANY functionality subject to a call method.
A simple example of an implementation of a call method:
I think this is the reason? Was that your question?
Also, @Siegeprogrammer, if I am not wrong with d3.js it appears that the ons are made over a non-existing selection. I think you have to select before adding behaviour.
@evaristoc. There is actually a selection already being made. It is the circle itself that is currently selected. In my code, i don’t think i can select the tooltip with ‘tip’ because that isn’t a valid tag to be selected. I tried selecting it with the id (#tooltip) but I don’t think it works either.
It will not work, I find it a bit odd that the html method call can access my dataSet through d=>‘some function’. But I can’t do the same with the attr method.
d3.tip doesn’t have the attribute data-year defined : https://github.com/Caged/d3-tip/blob/master/index.js. It has what it looks an attr builder though (line 77). After inspecting the code it appears that values for attr in tip are not defined as functors as for html and other methods of tip. It is then likely that functionality that allows that attribute to collect data, like in attr(‘data-year’, d=>d[‘Year’]) is not defined for attr in tip. Not sure if that is the reason though. In my opinion, there should be a way to make it work as you expect. However I don’t know if that is what you want (initializing tip with the data-year attribute)? And if so, why?
It appears that the behaviour assigned to the created data-year attribute is well defined on the calling function.
What I wanted to say in the previous post was to recommend you to follow the example as in the readme of d3.tip repository: GitHub - caged/d3-tip: d3 tooltips. There are reasons for that.
And this affirmation I made:
is NOT TRUE! Now I seem to remember that what happened was that by keeping it joined the attributes that you joined to the d3 instance will stay the same and you won’t be able to modify them. Anyway, as I said I have some time not using d3.