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

发布于 2024-10-30 12:54:47 字数 2393 浏览 0 评论 0原文

我一直在尝试使用 google api 将这些绘制在散点图上。但这似乎不起作用。

S.No:1 价格:0.632 数量:10.26
S.No:2 价格:0.631 数量:10
S.No:3 价格:0.631 数量:20
S.No:4 价格:0.631 数量:4.65

我有一百个这样的条目,它们代表了比特币的价值。

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

<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>

这为我返回一个空白页。可能有什么问题吗?我按照这个示例进行操作。 下面给出了我从中获取值的 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/>";

} ?>

I have been trying to plot these on a scatter chart using google api. But it doesn't seem to work.

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

I have hundred entries like this, which represents the bitcoin value.

I want to plot it in a way that price in on the vertical axis and Serial no. in horizontal axis. The volume will define the diameter of the circle.

<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 returns a blank page for me. Whats possibly wrong? I follow this example.
My php code from which i get the values is given below.

<?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/>";

} ?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文