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

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

报告相同问题?