JokerMM JokerMM - 3 months ago 9
Javascript Question

Google charts numbers

i use a google charts for getting data from google analytics to see it in my website
there is some data have a decimal number
how to make the charts forgot the number after the decimal.

I use a php foreach in the data generate for the charts
like this:

var data = google.visualization.arrayToDataTable([
['WEEK', 'total Cost' ,{ role: 'annotation' } ],
<? foreach($SOMETHING->getRows() as $row) {
$test = $row[2];
$test = number_format($test,0);?>
["<? echo $row[0]; ?>", <? echo $row[2]; ?>, <? echo $test; ?> ],
<? }?>

]);


i try the php coding "number_format" but it working in the script if i see the inspect of the page but not showing any charts in the page

if i put this code it work but the decimal is still there

var data = google.visualization.arrayToDataTable([
['WEEK', 'total Cost' ,{ role: 'annotation' } ],
<? foreach($tristanweekdata->getRows() as $row) { ?>
["<? echo $row[0]; ?>", <? echo $row[2]; ?>, <? echo $row[2]; ?> ],
<? }?>

]);


P.S.: this code is for one of my charts but its effect all the charts and all of them not showing anything

what should i do for remove the decimal????

thanks

Answer

Wrap your value in PHP's floor or intval method: (using number_format as you have it will add commas at the thousandths place in your numbers, so that should be avoided).

echo floor(456.8)."<br/>";
echo floor(456)."<br/>";
echo floor(456.1)."<br/>";
echo floor(456.0);

// outputs:
// 456
// 456
// 456
// 456

echo intval(456.8)."<br/>";
echo intval(456)."<br/>";
echo intval(456.1)."<br/>";
echo intval(456.0);

// outputs:
// 456
// 456
// 456
// 456

So in your example above:

$test = floor($row[2]);
//or
$test = intval($row[2]);

Floor: Returns the next lowest integer value

http://php.net/manual/en/function.floor.php

Intval

Returns the integer value of var, using the specified base for the conversion (the default is base 10)

http://php.net/manual/en/function.intval.php