我如何使用PHP绘制谷歌散点图

时间:2011-04-05 08:20:14

标签: php decimal plot google-visualization

我一直试图使用google api在散点图上绘制这些图。但它似乎没有用。

S.No:1 Price:0.632 Volume:10.26
S.No:2 Price:0.631 Volume:10
S.No:3 Price:0.631 Volume:20
S.No:4 Price:0.631 Volume:4.65

我有100个这样的条目,代表比特币的价值。

我想以垂直轴和序列号的价格绘制它。在水平轴上。体积将定义圆的直径。

<html>
  <head>
    <script type="text/javascript" src="https://www.google.com/jsapi"></script>
    <script type="text/javascript">
      google.load("visualization", "1", {packages:["corechart"]});
      google.setOnLoadCallback(drawChart);
      function drawChart() {
        var data = new google.visualization.DataTable();
        data.addColumn('number', 'Transactions');
        data.addColumn('number', 'Price');
        data.addRows(6);
        data.setValue(1, 0.632, 10.26);
        data.setValue(2, 0.631, 10);
        data.setValue(3,0.631, 20);
        data.setValue(4, 0.631, 4.65);



        var chart = new google.visualization.ScatterChart(document.getElementById('chart_div'));
        chart.draw(data, {width: 400, height: 240,
                          title: 'Bitcoins Transaction Prices',
                          hAxis: {title: 'Transactions', minValue: 1, maxValue: 100},
                          vAxis: {title: 'Price', minValue: 0, maxValue: 5},
                          legend: 'none'
                         });
      }
    </script>
  </head>

  <body>
    <div id="chart_div"></div>
  </body>
</html>

这会为我返回一个空白页面。什么可能错了?我按照this示例进行操作。 我从中获取值的PHP代码如下所示。

<?php
//get the unix time stamp of day 30 days back from today
$last_thirty = strtotime('-30 days'); 
//get the unix time stamp of day 1 month back from today
$last_month = strtotime("-1 month"); 
//get the data using bitcoinchart http api
$json = file_get_contents('http://bitcoincharts.com/t/lasttrades.json?limit=10&since=' . $last_month);
//decode json to php array
$data = json_decode($json);
$no = 1;
//loop through it
foreach ($data as $item) {
    $sn =  "S.No:".$no . "";
    $price = "Price:{$item->price}";
    $volume = "Volume:{$item->volume}";
    $no++;
    echo $sn . "<br/>";
    echo $price . "<br/>";
    echo $volume . "<br/>";

} ?>

1 个答案:

答案 0 :(得分:1)