1

I've noticed the y0, y1 etc naming convention in d3.js examples e.g. http://bl.ocks.org/mbostock/3886394

I've been verbosing (if that's even a word) the code a little to help me (a beginner!) understand it in more detail.

But I'm unsure of how best to describe the y0 etc naming conventions? Are they simply the data before and the next block of data's position?

For example I have expanded this block of code:

data.forEach(function(d) {
    var y0 = 0;
    d.ages = color.domain().map(function(name) { return {name: name, y0: y0, y1: y0 += +d[name]}; });
    d.ages.forEach(function(d) { d.y0 /= y0; d.y1 /= y0; });
  });

To the following so it's easier to see how the scope of the variables are being used e.g. y0 += +d[name]}; although a nice concise bit of code I found it tricky to see at first, that the outer y0 was being updated at the same as using it for y1.

data.forEach(function(d) {
    var y0 = 0;

    d.stats = color.domain().map(function(name) {
        var y1 = y0 + (+d[name]);

        var stat = {
            id: name, 
            y0: y0,
            y1: y1,
        };

        y0 = y1;

        return stat;
    });

    d.stats.forEach(function(stat) { 
        stat.y0 = stat.y0 / y0;
        stat.y1 = stat.y1 / y0; 
    });

 });

I think my mini rewrite is ok (at least the visualisation looks the same in the end - but please correct me if I'm wrong).

The only thing I'm finding hard to explain in code is the y0, y1 etc.

2
  • 1
    This isn't really a general convention. In this particular case, y0 refers to the lower y value of a bar and y1 to the upper y value. Nov 21, 2013 at 12:06
  • Cool thanks Lars, that's good enough for me. If you pop it as a response I'll give you some SO points :-) Nov 21, 2013 at 12:19

1 Answer 1

3

This isn't really a general convention. In this particular case, y0 refers to the lower y value of a bar and y1 to the upper y value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.