Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

d3 + adding text labels + formatting

Tags:

d3.js

this is my fiddle, which I am building with from this example

what is the best way to convert the text lables from 0.08 to 8% format.

This is my attempt, but is there a better/other way?

.text(function(d) { return Math.floor(d.frequency*100)+"%"; });

like image 909
HattrickNZ Avatar asked Mar 16 '26 19:03

HattrickNZ


1 Answers

The most common way to do it (let's call it the "d3 way") is to use the d3 formatting functions:

var format = d3.format("%");

Here is the Fiddle: https://jsfiddle.net/cr91xf1t/

You can play with several built in formats, or even create your own. Have a look here: http://bl.ocks.org/zanarmstrong/05c1e95bf7aa16c4768e

like image 170
Gerardo Furtado Avatar answered Mar 20 '26 21:03

Gerardo Furtado



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!