-
Notifications
You must be signed in to change notification settings - Fork 137
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
nodeHeight as a function #16
base: dev
Are you sure you want to change the base?
Conversation
This is a great idea and thank you for sending in the pull request. I see that you also add examples, docs and tests. Awesome work! I will be happy to include this functionality, but would like to recommend a few changes before doing so. First of all, add function
functor(v) {
return typeof v === "function" ? v : function() {
return v;
};
} This little function can help you handle In renderer.js, the code in constructor Then in var nodeHeight = helper.functor(options.nodeHeight)(node.data); In var nodeHeightFn = helper.functor(options.nodeHeight); and the rest of the code can use - if(typeof options.nodeHeight === 'function'){
- node.x = -pos - options.nodeHeight(node.data);
- }
- else{
- node.x = -pos - options.nodeHeight;
- }
+ node.x = -pos - nodeHeightFn(node.data); |
Thank you very much! I have updated the code as you described above. It is a very neat way how to handle the |
@@ -16,6 +16,20 @@ const helper = { | |||
sum(array, accessor){ | |||
return array.map(accessor) | |||
.reduce(((prev, current) => prev + current), 0); | |||
}, | |||
functor(v) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for adding the check, but a helper function like this should be quite abstract and does not get too involved with how will be used. To be specific, this functor should be useful for other value not just nodeHeight or event limited to Number. This can be used for String, Object, etc. as well. In conclusion, I wouldn't add the check here.
If you really want to add the check, it might be better to do this check in getWaypoints()
and layout
by checking the output value of nodeHeightFn()
regardless of the type of options.nodeHeight
. That way you also validate if somebody accidentally set options.nodeHeight to non-Number constant.
The check can print the warning like nodeHeight does not return a Number, returning 10 instead of ${value}
.
Thank you for making the change so quickly. While reviewing it, I just thought of another important issue that needs to be address first, The way Labella works is that it distributes labels into layers. Using a top down example, nodes in the first layer should have the same y. Similarly, nodes in the second layer should have same y. By doing so, it only needs to remove overlaps for one axis, x. Originally, because all nodes have same height, Additionally, I recommend passing |
Yeah, I agree that the check is very specific and the helpers should be very abstract. Did not thought about that. Damn, there is still a lot I need to learn haha. I removed it and I will fix the gaps and passing the entire node instead of nodeData tomorrow (I am located in Norway and it is 22:32 here atm.). I agree that the user can change it anywhere else. That is what I btw like about the implementation- it is very open. You do not need to dig in properties to get the list of nodes. |
Take your time. There is no rush on this. Really appreciate your help. |
I think we need something like this:
|
A little tweak from the code above so yPos is only computed once instead of for every node. Haven't tested but something along this line. For the path correction you have to modify var numLayers = ... // max of node.getLayerIndex() + 1;
case 'down':
var yPos;
if(gaps){
yPos = gaps.reduce(function(prev, current){
prev.push(current + prev[prev.length-1]));
return prev;
}, [options.layerGap]);
}
else{
yPos = [];
for(var i=0;i<numLayers;i++){
yPos.push(i*gap + options.layerGap);
}
}
nodes.forEach(function(node){
node.x = node.currentPos;
node.y = yPos[node.getLayerIndex()];
node.dx = node.width;
node.dy = nodeHeightFn(node);
});
break; |
I find my version a bit more readable but also a bit dumb :-D I have managed to tweak the |
Hei Krist, |
No problem. Thank you for bringing up the idea in the first place. We can leave this open to keep track (and you already made some progress). If anybody decide to do something with it, just add the comment so others will know. No pressure to do anything though. Thank you again for your voluntary work. |
Tomas Prochazka seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account. You have signed the CLA already but the status is still pending? Let us recheck it. |
Hello,
I thought it might be cool if you could actually specify the
nodeHeight
as function so I implemented it!Why?
Sometimes you want the height of the node to be dependent on the inner content because sometimes you want the content to be a long text and sometimes it is just a one line. There is no reason of having all the nodes with a fixed height.
Implementation?
Renderer now accepts the option
nodeHeight
as a function. This function has one parameter-nodeData
. This parameter is equal to what the programmer passes tonode.data
. This param is used in example as a source of the node's content. I do not pass the entire node because then the programmer would be able to change some of the calculated numbers and we do not want that. This function MUST return a number. If it does not, then an error is shown in the console with a description of what happened and with whatnodeData
and a fallback value is used so the rendering process is completed without any problems.I have also added: