2

我正在尝试在Highchart上绘制我的数据库中的一些数据,但我似乎无法让数据显示。

我有以下 PHP(片段)从数据库中获取数据并对其进行 json_encodes:

<?php

    $result = mysqli_query($cxn,"SELECT * FROM counter");

    while($row = mysqli_fetch_array($result)) {
        $value = $row['value'];
        $timestamp = strtotime($row['datetime']);
        $data[] = "[$value, $timestamp]";
    }   

    json_encode($data);

?>

json_encode 打印以下内容(我使用的是日期时间):

["[500, 1384122794]","[600, 1384153203]"]

然后我有以下内容来绘制数据:

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

        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
        <script src="http://code.highcharts.com/stock/highstock.js"></script>
        <script>
            $(function() {
                $.getJSON('http://www.website.com/graph.php', function(data) { // I put website.com on purpose
                    // Create the chart
                    $('#container').highcharts('StockChart', {
                        rangeSelector : {
                            selected : 1
                        },

                        title : {
                            text : 'Title'
                        },

                        series : [{
                            name : 'AAPL',
                            data : data,
                            tooltip: {
                                valueDecimals: 2
                            }
                        }]
                    });
                });

            });
        </script>
    </body>
</html>

问题是该图实际上并没有显示点,它只是一个空白图。

我究竟做错了什么?

4

4 回答 4

3

为了回答我自己的问题,我将 while 循环更改为:

while($row = mysqli_fetch_array($result)) {
    $value = $row['value'];
    $timestamp = strtotime($row['datetime']) * 1000;
    $data[] = [$timestamp, (int)$value];
}
于 2013-11-11T18:39:17.330 回答
1

问题是您返回字符串,您需要将数据作为数字返回。我建议熟悉 json_encode() 标志,例如 JSON_NUMERIC_CHECK。其次$data[] = "[$value, $timestamp]";,行需要是数组而不是带有打印括号的字符串。

于 2013-11-12T00:04:46.947 回答
0

您想将“数据”数组分配给错误的单列数据

看这里 :

$(function () {
        $('#container').highcharts({
            title: {
                text: 'Monthly Average Temperature',
                x: -20 //center
            },
            subtitle: {
                text: 'Source: WorldClimate.com',
                x: -20
            },
            xAxis: {
                categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
                    'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
            },
            yAxis: {
                title: {
                    text: 'Temperature (°C)'
                },
                plotLines: [{
                    value: 0,
                    width: 1,
                    color: '#808080'
                }]
            },
            tooltip: {
                valueSuffix: '°C'
            },
            legend: {
                layout: 'vertical',
                align: 'right',
                verticalAlign: 'middle',
                borderWidth: 0
            },
            series: [{
                name: 'Tokyo',
                data: [7.0, 6.9, 9.5, 14.5, 18.2, 21.5, 25.2, 26.5, 23.3, 18.3, 13.9, 9.6]
            }, {
                name: 'New York',
                data: [-0.2, 0.8, 5.7, 11.3, 17.0, 22.0, 24.8, 24.1, 20.1, 14.1, 8.6, 2.5]
            }, {
                name: 'Berlin',
                data: [-0.9, 0.6, 3.5, 8.4, 13.5, 17.0, 18.6, 17.9, 14.3, 9.0, 3.9, 1.0]
            }, {
                name: 'London',
                data: [3.9, 4.2, 5.7, 8.5, 11.9, 15.2, 17.0, 16.6, 14.2, 10.3, 6.6, 4.8]
            }]
        });
    }); 

一种解决方案是使用 for 循环并在每个循环中分配数据和名称

于 2013-11-11T19:44:01.317 回答
-1

Step-1 创建数据库 [highchartdemo]

Step-2 创建两个表 [demo_viewer, demo_click]

CREATE TABLE IF NOT EXISTS `demo_viewer` (

  `id` int(11) NOT NULL AUTO_INCREMENT,

  `numberofview` int(11) NOT NULL,

  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,

  PRIMARY KEY (`id`)

) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=10 ;


CREATE TABLE IF NOT EXISTS `demo_click` (

  `id` int(11) NOT NULL AUTO_INCREMENT,

  `numberofclick` int(12) NOT NULL,

  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,

  PRIMARY KEY (`id`)

) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=10 ;

demo_viewer 表: 在此处输入图像描述

demo_click 表: 在此处输入图像描述

Step3- 创建数据库配置文件 config.php

    $dbHost = "localhost";

    $dbDatabase = "highchartdemo";

    $dbPasswrod = "root";

    $dbUser = "root";


    $mysqli = mysqli_connect($dbHost, $dbUser, $dbPasswrod, $dbDatabase);

?>

Step-4 创建 Index.php 文件 index.php

<?php


    require('config.php');


    /* Getting demo_viewer table data */

    $sql = "SELECT SUM(numberofview) as count FROM demo_viewer 

            GROUP BY YEAR(created_at) ORDER BY created_at";

    $viewer = mysqli_query($mysqli,$sql);

    $viewer = mysqli_fetch_all($viewer,MYSQLI_ASSOC);

    $viewer = json_encode(array_column($viewer, 'count'),JSON_NUMERIC_CHECK);


    /* Getting demo_click table data */

    $sql = "SELECT SUM(numberofclick) as count FROM demo_click 

            GROUP BY YEAR(created_at) ORDER BY created_at";

    $click = mysqli_query($mysqli,$sql);

    $click = mysqli_fetch_all($click,MYSQLI_ASSOC);

    $click = json_encode(array_column($click, 'count'),JSON_NUMERIC_CHECK);


?>


<!DOCTYPE html>

<html>

<head>

    <title>Highcharts get data from database</title>

    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.js"></script>

    <script src="https://code.highcharts.com/highcharts.js"></script>

</head>

<body>


<script type="text/javascript">


$(function () { 


    var data_click = <?php echo $click; ?>;

    var data_viewer = <?php echo $viewer; ?>;


    $('#container').highcharts({

        chart: {

            type: 'column'

        },

        title: {

            text: 'Yearly Website Ratio'

        },

        xAxis: {

            categories: ['2013','2014','2015', '2016']

        },

        yAxis: {

            title: {

                text: 'Rate'

            }

        },

        series: [{

            name: 'Click',

            data: data_click

        }, {

            name: 'View',

            data: data_viewer

        }]

    });

});


</script>


<div class="container">

    <br/>

    <h2 class="text-center">Highcharts get data from database</h2>

    <div class="row">

        <div class="col-md-10 col-md-offset-1">

            <div class="panel panel-default">

                <div class="panel-heading">Graphical View</div>

                <div class="panel-body">

                    <div id="container"></div>

                </div>

            </div>
        </div>
    </div>
</div>
</body>
</html>
于 2019-02-15T08:47:58.807 回答