3 ms·
I've been working with d3.js often lately for some freelance work. Out of curiosity, I viewed source trulla's script and have some comments and observations: 1
by zackzackzack 15y ago
I've been working with d3.js often lately for some freelance work. Out of curiosity, I viewed source trulla's script and have some comments and observations:
1.) To convert numbers, they use +number, where number is a string. That is some excellent short hand and relies on automatic type conversion in javascript. Not sure what the speed is like, but I imagine it doesn't slow down things much,
2.) They forgot that they had jQuery loaded in already. You can see this by looking at the lines when they define w and h. They use d3 to get the height and width in a awkward way. $("vis").width() would have done the same for less thought.
3.) They are using some tools to check for or create automatic clean javascript. Not a semi-colon or tab out of line. Probably jslint, because they use a forEach at some point and I don't believe coffeescript uses forEach's in it's output. Coffeescript is probably worth the time to learn then; All those function(d){return d.a} become just (d)-> d.a.
Depending on your style, this could be useful as well:
getter = function(attr){
return function(d){
return d[attr];
}
}
ds.attr("x",get("x"))
4.) I am embarrassed I didn't know d3.svg.axis existed. That saves time and mistakes.
All in all, well done. Learned a good amount from reading through that. Thanks!
- golike 14y agoThanks for the code compliments Zack. As I've gotten more experienced with D3, I've been trying to use it exclusively, even for jQuery type things. In this case I'm just using jQuery for old browser fallback, since D3 doesn't go to great lengths to support old browser quirks. And btw, no code cleanup tools in use, just my OCD. ;) I haven't used Coffeescript yet. Will have to check it out.