Highcharts - 从mysql获取类别

时间:2013-08-21 20:06:16

标签: php javascript mysql json highcharts

我正在尝试将我的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>

2 个答案:

答案 0 :(得分: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);
});

答案 1 :(得分:0)

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

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