使用MySQL填充highcharts数据

时间:2016-07-02 06:08:45

标签: php html mysql highcharts

我是网络编程的新手,我有跟踪开支的个人项目。 直截了当,我有数据库数据包含本月和上个月的总支出。我可以手动显示它(通过回声)。但我无法将价值传递给高级系列。由于许多小的单独数据,我想使用php / mysql而不是json手动填充它。

Graph2.php

<?php
$mysql_hostname = "localhost";
$mysql_user = "root";
$mysql_password = "mypassword";
$mysql_database = "mytraining";
$bd = mysqli_connect($mysql_hostname, $mysql_user, $mysql_password, $mysql_database);
if (mysqli_connect_errno()) {
        printf("Connect failed: %s\n", mysqli_connect_error());
        exit();
    }

$query2 = "select year(date) as y, month(date) as m, sum(amount) as p from expenditure_bak group by year(date), month(date) order by m ASC";  
$sth = mysqli_query($bd, $query2);
$rows = array();
$rows['name'] = 'Revenue';
while($r = mysqli_fetch_array($sth)) {
    $rows['data'][]= round($r['p'],2);
}
//echo join($rows['data'], ','); //able to display correct value 660.2,60

?>

PopupGraph.html

<?php
    include('graph2.php'); 
?>
<!DOCTYPE HTML>
<html>
    <head>

        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>Monthly Expenditure</title>

        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
        <style type="text/css">
        ${demo.css}
        </style>
        <script type="text/javascript">
        $(function () {
            $('#container').highcharts({
                title: {
                    text: 'Monthly Expenditure'
                },
                xAxis: {
                    categories: ['Jun', 'Jul']
                },
                yAxis: [{
                        min: 0,
                        title: {
                            text: ''
                        }
                    }, {
                        min: 0,
                        opposite: false, //optional, you can have it on the same side.
                        title: {
                            text: 'Average'
                        }
                    }
                ],
                labels: {
                    items: [{
                        html: 'Monthly Expenditure',
                        style: {
                            left: '50px',
                            top: '18px',
                            color: (Highcharts.theme && Highcharts.theme.textColor) || 'black'
                        }
                    }]
                },

                tooltip: {
                    formatter: function () {
                        return '<b>' + this.x + '</b><br/>' +
                            this.series.name + ': ' + this.y 
                            /*+ '<br/>' + 'Total: ' + this.point.stackTotal*/;
                    }
                },

                series: [{
                    type: 'spline',
                    name: 'Average',
                    yAxis: 1, //to make y axis
                    //data: [660.2,60], //able to show correct graph with static input  
                    data: [<?php echo join($rows[data], ',') ?>],
                    marker: {
                        lineWidth: 2,
                        lineColor: Highcharts.getOptions().colors[3],
                        fillColor: 'white'
                    }
                }]
            });
        });


        </script>
    </head>
    <body>
    <script src="https://code.highcharts.com/highcharts.js"></script>
    <script src="https://code.highcharts.com/modules/exporting.js"></script>

    <div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>

    </body>
</html>

那么,在highcharts系列中将数据:[] 放入正确的代码是什么。

输出应该像JSFiddle,但显然使用来自数据库的动态数据。

最后,我想展示类似的东西 Expenditure Tracking

1 个答案:

答案 0 :(得分:0)

您可能需要对数据进行编码。我建议你看一下Pass a PHP array to a JavaScript function的答案。基于此,您应该能够做到这样的事情:

JSON.parse(<?php echo json_encode(join($rows[data], ',')) ?>);

那么你应该能够得到你想要的结果。您可能不需要JSON.parse。