php - 来自数据库的动态样条 Highcharts

标签 php javascript ajax highcharts

我尝试制作样条 Highcharts 并实现 How to load data from JSON to Highchart? 中的解决方案,这是米娜·加布里埃尔的回答。代码如下所示。

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);
?>

在 highchart 脚本中:

<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代码,它运行正常。我错过了什么吗?

最佳答案

根据给定的信息和this post ,我对这个问题的最佳选择是 $count[0] 是一个字符串,highcharts 需要它严格是数字。你能为我尝试以下操作吗

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

关于php - 来自数据库的动态样条 Highcharts ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11805321/

相关文章:

php - 使用php发送信息

javascript - 访问框架内的框架

javascript - 发送 JSON stringify 到 PHP 如何读取它

php - 设置和使用 php-resque 的正确方法是什么?

php - 快速 3 http 请求

php - 转义文字,使其用双引号引起来

javascript - Vue.js/Nginx/Node.js - 413 请求实体太大

javascript - 使用 jQuery 更新脚本标签的 src - 使用 jQuery 重新加载脚本而不刷新页面

javascript - 使用 php 提交表单,通过 javascript 检查电子邮件

javascript - owl carousel 在 ajax 加载的项目上不能正常工作