Bobby
Bobby

Reputation: 123

d3.format thousand separator on variables?

Hello I'm yet again stuck on d3...

I'd like to know how to use a thousand seperator on a variable all the examples I've managed to find seem to be on static data.

This is what I've tried so far:

d3.csv("OrderValueToday.csv", function(obj) { 

var text = 'Today = £';
var totalSales = text + d3.format(",") + obj[0].Today;

svgLabel = d3.select("#label").append("h2")
 .text (totalSales);

});

However it just outputs a load a stuff on the webpage this is it:

Today = £function (n){var e=d;if(m&&n%1)return"";var u=0>n||0===n&&0>1/n?(n=-n,"-"):a;    if(0>p){var c=Zo.formatPrefix(n,h);n=c.scale(n),e=c.symbol+d}else n*=p;n=g(n,h);var x=n.lastIndexOf("."),M=0>x?n:n.substring(0,x),_=0>x?"":t+n.substring(x+1);!s&&f&&(M=i(M));var b=v.length+M.length+_.length+(y?0:u.length),w=l>b?new Array(b=l-b+1).join(r):"";return y&&(M=i(w+M)),u+=v,n=M+_,("<"===o?u+n+w:">"===o?w+u+n:"^"===o?w.substring(0,b>>=1)+u+n+w.substring(b):u+(y?n:w+n))+e}20000

So all I want is to be able to make the totalSales value have thousand separators so like 20,000 everything else I've tried doesnt do anything. I've read this https://github.com/mbostock/d3/wiki/Formatting but didnt see what I could do for my scenario.

Any help would be greatly appreciated. Cheers

Upvotes: 6

Views: 7784

Answers (1)

jshanley
jshanley

Reputation: 9128

Specifying a d3.format returns a formatting function, which you must then call as a function, passing in the number to be formatted as an argument:

var myNumber = 22400;
d3.format(',')(myNumber); // returns '22,400'

Sometimes you will see a format function stored as a variable like this:

var commaFormat = d3.format(',');
commaFormat(1234567); // returns '1,234,567'

In your case, you could do the following:

var totalSales = text + d3.format(',')(obj[0].Today);

Upvotes: 6

Related Questions