score:0

Accepted answer

The problem is you are using local variables d and i as function parameters while setting the transform attribute. Parameter i in local scope overrides the actual variable. The value of local variable i would be always zero as there is no data bind to that element.

var Legend = LegendCanvas.append("g")
   .attr("class", "legend container")
   .attr("transform", function (d, i) { //Remove i
       if (i === 0) {
          return "translate(0,0)"
       } else { 
          PrevElemLength += this.previousElementSibling.getBBox().width;
          return "translate(" + (PrevElemLength) + ",0)"
       }
  });

I have also made slight updates to the code for improvements.

var LegendCanvas = d3.select("svg")
  .append("g")
  .attr("class", "legend canvas")
  .attr("x", 0)
  .attr("y", 0)
  .attr("width", 500)
  .style("fill", "#ffcccc")
  .attr("transform", "translate(0,15)");
var PrevElemLength = 0;
var Data = [{
  OriginId: 1,
  UniqueName: "Some Long Text 1"
}, {
  OriginId: 2,
  UniqueName: "Some Long Text 2"
}];
/*<note>Loop through each data series, call the Valueline variable and plot the line. */
var Color = d3.scale.category10();
Data.forEach(function(Key, i) {

  /*<note>Add a g element to the legend container. */
  var Legend = LegendCanvas.append("g")
    .attr("class", "legend container")
    .attr("transform", function() {
      if (i === 0) {
        return "translate(0,0)"
      } else {
        var marginLeft = 5;
        PrevElemLength += this.previousElementSibling.getBBox().width;
        return "translate(" + (PrevElemLength + marginLeft) + ",0)"
      }
    });

  /*<note>Adds a rectangle to pre-fix each legend. */
  Legend.append("rect")
    .attr("width", 5)
    .attr("height", 5)
    .style("fill", function() {
      return Key.color = Color(Key.UniqueName);
    });

  /*<note>Adds the legend text. */
  Legend.append("text")
    .attr("x", function() {
      return this.parentNode.getBBox().width + 5;
    })
    .attr("dy", "0.4em")
    .attr("class", "legend text")
    .style("fill", function() {
      return Key.color = Color(Key.UniqueName);
    })
    .text(Key.UniqueName);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
<svg height=500 width=500></svg>

The d3 way of implementation(Using data binding) would be as follows

var LegendCanvas = d3.select("svg")
  .append("g")
  .attr("class", "legend canvas")
  .attr("x", 0)
  .attr("y", 0)
  .attr("width", 500)
  .style("fill", "#ffcccc")
  .attr("transform", "translate(0,15)");

var Data = [{
  OriginId: 1,
  UniqueName: "Some Long Text 1"
}, {
  OriginId: 2,
  UniqueName: "Some Long Text 2"
}];

var Color = d3.scale.category10();

var Legend = LegendCanvas.selectAll(".legend")
  .data(Data)
  .enter()
  .append("g")
  .attr("class", "legend container");  

Legend.append("rect")
  .attr("width", 5)
  .attr("height", 5)
  .style("fill", function(Key) {
    return Key.color = Color(Key.UniqueName);
  });

Legend.append("text")
  .attr("x", function() {
    return this.parentNode.getBBox().width + 5;
  })
  .attr("dy", "0.4em")
  .attr("class", "legend text")
  .style("fill", function(Key) {
    return Key.color = Color(Key.UniqueName);
  })
  .text(function(Key){ return Key.UniqueName });

var PrevElemLength = 0;
Legend.attr("transform", function(d, i) {
    if (i === 0) {
      return "translate(0,0)"
    } else {
      var marginLeft = 5;
      PrevElemLength += this.previousElementSibling.getBBox().width;
      return "translate(" + (PrevElemLength + marginLeft) + ",0)"
    }
  });  
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
<svg width=500 height=500></svg>

score:0

Try this :

//Legend
    var legend = vis.selectAll(".legend")
        .data(color.domain())
        .enter().append("g")
        .attr("class", "legend")
        .attr("transform", function(d, i) { return "translate(0," + i * 20 + ")"; });

    legend.append("image")
        .attr("x", 890)
        .attr("y", 70)
        .attr("width", 20)
        .attr("height", 18)
        .attr("xlink:href",function (d) {
            return "../assets/images/dev/"+d+".png";
        })

    legend.append("text")
        .attr("x", 910)
        .attr("y", 78)
        .attr("dy", ".35em")
        .style("text-anchor", "start")
        .text(function(d) {
                return d;
        }); 

Related Query

More Query from same tag