2014-11-22 20 views
0

我該怎麼辦根據d.Status!==""下面分別d屬於數據(sortedFeatures)如何根據其數據的符號添加到組

feats = cont.selectAll('feats') 
      .data(sortedFeatures) 
      .enter() 
      .append('g') 
      .attr('transform', function(d, i) { 
       d['y'] = (fontsize * i); 
       d['x'] = self.x_scale(d.start); 
       return 'translate(' + self.x_scale(d.start) + ',' + fontsize * i + ')'; 
      }).attr('type', 'featureGroup'); 

polygonLastDelivery = feats.append('g'); 

polygonLastDelivery.append("path") 
     .attr("class", "point") 
     .attr("d", d3.svg.symbol().type("cross"))//I don't want to add this symbol if d.Status==="" can I make a function out of this? 
     .attr("transform", function(d) { return "translate(" + 15 + "," + 22 + ")"; }); 

這就是我想要一個符號添加到新創建的組「polygonLastDelivery」取決於 .data(sortedFeatures)每個是否包含狀態文本或者是否爲空。你怎麼解決這個問題?

我嘗試上述whitout成功以下

.attr("d", function(d) { return d3.svg.symbol().type("cross");}) 
+1

根據「狀態」屬性篩選您的選擇,然後將符號添加到過濾的選擇。 – 2014-11-22 14:57:16

回答

0

你可以使用過濾器這樣的:

polygonLastDelivery.append("path") 
    .filter(function(d) { return d.status !== "" }) 
    .attr("class", "point") 
    .attr("d", d3.svg.symbol().type("cross")) 
    .attr("transform", function(d) { return "translate(" + 15 + "," + 22 + ")"; }); 

或者,如果您想根據狀態來改變你的符號,你可以使用每個:

polygonLastDelivery.each(function(d){ 
    var object = d3.select(this); 
    var symbol = (d.status !== "") ? 'cross': 'diamond'; 
    object.append("path") 
     .attr("class", "point") 
     .attr("d", d3.svg.symbol().type(symbol)) 
     .attr("transform", function(d) { return "translate(" + 15 + "," + 22 + ")"; }); 
});