数据库中的动态样条高图(dynamic spline highchart from database)

我尝试制作样条线高图并实现如何将数据从JSON加载到Highchart的解决方案? ,这是Mina Gabriel的回答。 代码看起来像这样。

在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代码,它的工作正常。 我错过了什么?

I try to make a spline highchart and implement solution from How to load data from JSON to Highchart?, that's an answer from Mina Gabriel. The codes look like this.

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

And in the highchart script:

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

And those just work well. But when I try to change the code in the test.php to set the y-value as a number from database like this:

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

the line chart doesnt work. I've checked the sql code and it just works right. Did I miss something?

最满意答案

从给定的信息和这篇文章 ,我最好的问题是$ count [0]是一个字符串,highcharts需要它是严格的数字。 你能为我试试以下吗?

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

From the given information and this post, my best bet to the issue is that the $count[0] is a string, highcharts needs it to be strictly numeric. Could you try the following for me

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

更多推荐