gka/d3-jetpack

translate on single axis

Closed this issue · 2 comments

gka commented
export default function(xy, dim) {
  return this.attr('transform', function(d,i) {
    var p = [typeof xy == 'function' ? xy.call(this, d,i) : xy];
    if (dim == 'x') p = p[p, 0]); else if (dim == 'y') p = [0, p];
    return 'translate(' + p[0] +','+ p[1]+')';
  });
}

so I can do

sel.translate(d3.f('x'), 'x')
gka commented

or without the need for extra strings:

export default function(xy, dim) {
  return this.attr('transform', function(d,i) {
    var p = [typeof xy == 'function' ? xy.call(this, d,i) : xy];
    if (dim === 0) p = p[p, 0]); else if (dim === 1) p = [0, p];
    return 'translate(' + p[0] +','+ p[1]+')';
  });
}
export default function(xy, dim) {
  return this.call(function(sel){
    if (sel.node().getBBox){
      sel.attr('transform', function(d,i) {
          var p = [typeof xy == 'function' ? xy.call(this, d,i) : xy];
          if (dim == 'x') p = p[p, 0]; else if (dim == 'y') p = [0, p];
          return 'translate(' + p[0] +','+ p[1]+')';
        });
    } else {
      sel.style('transform', function(d,i) {
        var p = typeof xy == 'function' ? xy.call(this, d,i) : xy
        if (dim == 'x') p = p[p, 0]; else if (dim == 'y') p = [0, p];
        return 'translate(' + p[0] + 'px, ' + p[1] + 'px)';
      });
    }
  })
}