使用PHP和SQL的highstock烛台图表中未显示JSON数据

时间:2019-04-27 18:31:23

标签: php mysql sql json highcharts

我正在尝试显示highstock candlestick chart,但无法实现。 我的网站在图表应显示的位置显示一个空白,但图表中没有任何显示。 如何解决它以显示图表?

我有两个脚本:

datachart.inc.php:

<?php
include '../dbh.php'; //It connects to the database

$sql = "SELECT * from table";
$result = $conn->query($sql);
$row = mysqli_fetch_array($result);

$data = array();
$count = 0;
while ($row=mysql_fetch_array($result))
{
  $newdate = strtotime($row['date']) * 1000; 
  $data[] = array($newdate, (float)$row['open'], (float)$row['high'], 
(float)$row['low'], (float)$row['close']);
  $count++;
}   
echo json_encode($data);
?>

index.htm:

<!DOCTYPE HTML>
<HTML>
   <BODY>
      <script>
      $(function() {
    $.getJSON('datachart.inc.php', function(data) {

    // create the chart
    chart = new Highcharts.StockChart({
        chart : {
            renderTo : 'container',
        },

        rangeSelector : {
            selected : 1
        },

        title : {
            text : 'Test Price'
        },

        series : [{
            type : 'candlestick',
            name : '',
            data : data,
            tooltip: {
                valueDecimals: 2
            },
            dataGrouping : {
                units : [
                    ['week', // unit name
                    [1] // allowed multiples
                ], [
                    'month', 
                    [1, 2, 3, 4, 6]]
                ]
            }
        }]
    });
    });
});
</script>

<div id="container" style="height: 250px; min-width: 250px"></div>

   </BODY>
</HTML>

点击here查看我的sql表图片

1 个答案:

答案 0 :(得分:0)

已解决:

1)导入javascript文件

2)删除“ $ row = mysqli_fetch_array($ result);”

3)将“ while($ row = mysqli_fetch_array($ result))”更改为“ while($ row = mysqli_fetch_array($ result))”