php - 使用 MySQL 填充 highcharts 数据

标签 php html mysql highcharts

我是网络编程新手,我有个人项目来跟踪支出。 开门见山,我的数据库数据包含本月和上个月的总支出。我可以手动显示它(通过 echo)。但我无法将值传递给 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 系列的 data: [] 中放置的正确代码是什么?

输出应类似于 JSFiddle但显然使用数据库中的动态数据。

最后,我想显示类似的内容 Expenditure Tracking

最佳答案

您可能需要对数据进行编码。我建议您看一下这里的答案 Pass a PHP array to a JavaScript function 。基于此,您应该能够执行以下操作:

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

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

关于php - 使用 MySQL 填充 highcharts 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38157082/

相关文章:

php - PHP 表单提交代码中的未定义索引错误

jquery - 响应式 header 中 CSS 背景转换的问题

css - CSS float属性在处理嵌套div时如何组合?

MySQL 数据库主机名

javascript防止在n个utf 8字符之后写入表单元素

php - 在 PHP 中从 MySQL 结果创建一个没有重复项的数组

php - HTML2PDF - 下载 pdf 文件并将其显示到页面

php - Laravel-5.6 'LIKE ' 在 where 和 or where select

php - 在 Laravel 中检查 session 超时

javascript - jquery 从特定表单获取所有输入