2016-04-07 16 views
0

Açıklama metninde değerler yerine yüzde göstermeye çalışıyorum. formats.percentage kullanmayı denedim, ancak istenen sonucu vermiyor, aslında metin görüntülenmiyor.yüzdesi

var legend = svg.selectAll(".legend") 
       .data([0].concat(colorScale.quantiles()), function(d) { return d; }); 

legend.enter().append("g").attr("class", "legend"); 

legend.append("rect") 
    .attr("x", function(d, i) { return legendElementWidth * i; }) 
    .attr("y", height) 
    .attr("width", legendElementWidth) 
    .attr("height", gridSize/2) 
    .style("fill", function(d, i) { return colors[i]; }); 

legend.append("text") 
    .attr("class", "mono") 
    // .text(function(d) { return "≥ " + Math.round(d); }) -- this was original 
    // replaced with below piece 
    .text(function(d) { 
     var r = colors.invertExtent(d); 
     return formats.percent(r[0]); 
    } 
    .attr("x", function(d, i) { return legendElementWidth * i; }) 
    .attr("y", height + gridSize); 

legend.exit().remove(); 

cevap

1
Sen d3 biçimlendirme araçlarını da kullanabilirsiniz

: https://github.com/mbostock/d3/wiki/Formatting

aşağıdaki do kullanmak için:

yerine formats.percent ait
d3.format(' insert format parameter here ')(insert value here); 

(r [0]); Senin durumunda

olurdu: belirtildiği gibi sadece değerden sonra bir '%' ekleyerek değil ki, bu% " '' 100 ile son eki ile çarpın" olsa

.text(d3.format('%')(r[0])); 

unutmayın doc.

var r = [ 0.4, 80]; //Whatever values you have in r 
 

 
var s = d3.select('body') 
 
    .append('svg') 
 
    .attr('width', 500) 
 
    .attr('height', 500); 
 

 
s.append('text') 
 
    .text(d3.format('%')(r[0])) //Thats where you apply the format : 0.4 -> 40% 
 
    .attr('x', 10) 
 
    .attr('y', 20); 
 

 
s.append('text') 
 
    .text(d3.format('%')(r[1])) 
 
    .attr('x', 10) 
 
    .attr('y', 40);
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script> 
 

 
<body> 
 
</body>

+0

mükemmel:

Aşağıda kukla sayılar ile nasıl kullanılacağına dair bir örnektir. Çıktıyı yüzde biçiminde aldım. Teşekkürler – Cini09

İlgili konular