duanhui4160 2019-04-27 18:31
浏览 104
已采纳

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

I'm trying to display a highstock candlestick chart, but I can't achieve it. My website shows a blank where the chart should be, but there's nothing showing in the chart. How can I fix it to show the chart?

I have two scripts:

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>

Click here to see a picture of my sql table

  • 写回答

1条回答 默认 最新

  • down_load1117 2019-04-28 19:11
    关注

    Solved:

    1) Import the javascript files

    2) Remove "$row = mysqli_fetch_array($result);"

    3) Change "while ($row=mysql_fetch_array($result))" to "while ($row=mysqli_fetch_array($result))"

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划