0

我正在尝试将数据从我的 mysql 获取到 Highcharts 条形图中。我有来自 mysql 的数据,其中包括端口号和每个端口的平均字节数。这是我的 php 查询和 html 页面的内容。数据(平均字节)很好地显示在图表上。我无法获取要显示的类别(端口)数据。

<?php
$con = mysql_connect("localhost","root","kdkdkdk") or die(mysql_error());
mysql_select_db("silk", $con);

$result = mysql_query("SELECT dstPortNum,avgByte FROM statistic_avgbytesport ORDER BY avgByte DESC LIMIT 10;");

$rows = array();
while($r = mysql_fetch_array($result)) {
    $row[0] = $r[0];
    $row[1] = $r[1];
    array_push($rows,$row);
}

print json_encode($rows, JSON_NUMERIC_CHECK);
#print json_encode($rows[0][0], JSON_NUMERIC_CHECK);

mysql_close($con);
?>

和我的 html 页面来显示图表。

<!DOCTYPE HTML>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>Highcharts Pie Chart</title>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
        <script type="text/javascript">
        $(document).ready(function() {
            var options = {
                chart: {
                    renderTo: 'avgbytes'                  
                },
                title: {
                    text: 'Average Bytes Per Port'
                },

                pane: {
                    size:'80%'
                },

                xAxis: {
                    title: {
                        text: 'PORTS'
                    },
                    categories: []
                },

                yAxis: {
                    min: 0,
                    title: {
                        text: 'Bytes'
                    }
                },

                series: [{
                    type: 'bar',
                    name: 'Average Bytes',
                    data: []
                }]
            }

            $.getJSON("averageBytesPerPort.php", function(json) {
                options.series[0].data = json;
                chart = new Highcharts.Chart(options);
            });



        });   
        </script>
        <script src="http://code.highcharts.com/highcharts.js"></script>
        <script src="http://code.highcharts.com/highcharts-more.js"></script>

    </head>
    <body>
        <div id="avgbytes" style="width: 300px; height: 300px; margin: 0 auto"></div>
    </body>
</html>
4

2 回答 2

0

你的数据到底是如何显示的,你没有把 $conn 作为 mysql_query() 的第二个参数;它应该是

$result = mysql_query("SELECT dstPortNum,avgByte FROM statistic_avgbytesport ORDER BY avgByte DESC LIMIT 10;",$conn);
于 2013-08-24T18:38:35.633 回答
0

你可以使用这样的东西:

$.getJSON("averageBytesPerPort.php", function(json) {
    var options = {
        chart: {
            renderTo: 'avgbytes'                  
        },

        title: {
            text: 'Average Bytes Per Port'
        },

        pane: {
            size:'80%'
        },

        xAxis: {
            title: {
                text: 'PORTS'
            },
            categories: data.categories
        },

        yAxis: {
            min: 0,
            title: {
                text: 'Bytes'
            }
        },

        series: [{
            type: 'bar',
            name: 'Average Bytes',
            data: data
        }]
    }

    chart = new Highcharts.Chart(options);
});
于 2013-08-23T11:24:48.470 回答