数据库中的动态样条曲线高图


dynamic spline highchart from database

我试图制作一个样条曲线高图,并实现"如何将数据从JSON加载到高图?"的解决方案?,这是米娜·加布里埃尔的回答。代码看起来是这样的。

test.php

}
// Set the JSON header
header("Content-type: text/json");
// The x value is the current JavaScript time, which is the Unix time multiplied     by       1000.
$x = time() * 1000;
$y = rand(0,100) ; 

// Create a PHP array and echo it as JSON
$ret = array($x, $y);
echo json_encode($ret);
?>

在高图脚本中:

<script>
/**
 * Request data from the server, add it to the graph and set a timeout to request again
 */
var chart; // global
function requestData() {
$.ajax({
    url: 'http://localhost:8080/test.php',
    success: function(point) {
        var series = chart.series[0],
            shift = series.data.length > 20; // shift if the series is longer than 20
        // add the point
        chart.series[0].addPoint(point, true, shift);
        // call it again after one second
        setTimeout(requestData, 1000);    
    },
    cache: false
   });
 }
 $(document).ready(function() {
   chart = new Highcharts.Chart({
      chart: {
        renderTo: 'container',
        defaultSeriesType: 'spline',
        events: {
            load: requestData
        }
    },
    title: {
        text: 'Live random data'
    },
    xAxis: {
        type: 'datetime',
        tickPixelInterval: 100,
        maxZoom: 20 * 1000
    },
    yAxis: {
        minPadding: 0.2,
        maxPadding: 0.2,
        title: {
            text: 'Value',
            margin: 80
        }
    },
    series: [{
        name: 'Random data',
        data: []
     }]
   });        
});
  </script>
  <  /head>
<body>

这些都很有效。但是,当我试图更改test.php中的代码,将y值设置为数据库中的数字时,如下所示:

<?php
header("Content-type: text/json");
$db = mysql_connect("localhost","myusername","mypassword");
mysql_select_db("mydatabase");

$day=date('Y-m-d'); //UTC standar time
$result = mysql_query("SELECT COUNT(*) FROM table WHERE time='{$day}';");
$count = mysql_fetch_array($result);
// The x value is the current JavaScript time, which is the Unix time multiplied by       1000.
$x = time() * 1000;
$y = $count[0]; 
// Create a PHP array and echo it as JSON
$ret = array($x, $y);
echo json_encode($ret);
?>

折线图不起作用。我已经检查了sql代码,它运行得很好。我错过什么了吗?

根据给定的信息和这篇文章,我对这个问题的最佳选择是$count[0]是一个字符串,highcharts需要它是严格的数字。你能帮我试试下面的吗

   $y = intval($count[0]); // OR floatval($count[0]);