谷歌图表更多的小数

时间:2014-12-13 10:16:44

标签: database google-visualization linechart

我想在google折线图中显示更多小数。目前图表没有显示小数点后的数字。我想图表显示2或3位小数。

这是我的代码:

<html>
<head>

<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">      </script>
<script type="text/javascript">

google.load('visualization', '1', {'packages':['corechart']});

google.setOnLoadCallback(drawChart);

function drawChart() {
  var jsonData = $.ajax({
      url: "Lat.php",
      dataType:"json",
      async: false
      }).responseText;

  var data = new google.visualization.DataTable(jsonData);

  var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
  chart.draw(data, {width: 600, height: 400});
}


</script>
</head>

<body>
<!--Div that will hold the chart-->
<div id="chart_div"></div>
</body>
</html>

该图表显示了数据库中的数据,我使用以下代码调用此数据:

<?php

$databaseName = "pws";
$con = mysql_connect('localhost', 'root', 'usbw') or die('Error connecting to server');
mysql_select_db($databaseName, $con); 
$query = mysql_query('SELECT * FROM gpx');

$table = array();
$table['cols'] = array(

   array('label' => 'id',  'type' => 'number'),
   array('label' => 'Lat', 'type' => 'number'),
   array('label' => 'Lon', 'type' => 'number'),
   array('label' => 'Ele', 'type' => 'number')
);

$rows = array();
while($r = mysql_fetch_assoc($query)) {

$temp = array();
    $temp[] = array('v' => (int)$r['id']);
    $temp[] = array('v' => (int) $r['Lat']); 
    $temp[] = array('v' => (int) $r['Lon']);
    $temp[] = array('v' => (int) $r['Ele']);

$rows[] = array('c' => $temp);
}


$table['rows'] = $rows;

$jsonTable = json_encode($table);

header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Content-type: application/json');

echo $jsonTable;
?>

有人知道如何让折线图显示更多小数吗?

编辑: 没关系,我设法解决了。 php文件中的(int)是问题,通过删除它们问题就解决了

1 个答案:

答案 0 :(得分:0)

通过删除php文件中的(int),我设法让它工作

像这样:

$temp = array();
    $temp[] = array('v' => $r['id']);
    $temp[] = array('v' => $r['Lat']); 
    $temp[] = array('v' => $r['Lon']);
    $temp[] = array('v' => $r['Ele']);

$rows[] = array('c' => $temp);
}