简体   繁体   中英

JSON data not showing in highstock candlestick chart using PHP and SQL

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM