php - 使用 Ajax 生成两个日期之间的 HighChart

标签 php jquery ajax highcharts

我正在尝试使用 Ajax 获取两个日期之间的数据并使用 HighCharts 显示图表。 以下是我到目前为止所尝试过的;

<!DOCTYPE HTML>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <link href="../webroot/css/cake.generic.css" type="text/css" rel="stylesheet">
        <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() {
                //default

                $('#dynamic_data').change(function() {
                    var startdate = document.getElementById('startdate').value;
                    var enddate = document.getElementById('enddate').value;

                });

                var options = {
                    chart: {
                        renderTo: 'container',
                        type: 'column'
                    },
                    title: {
                        text: 'Highcharts Chart PHP with MySQL Example',
                        x: -20 //center
                    },
                    subtitle: {
                        text: 'Sumber : Jabatan XYZ',
                        x: -20
                    },
                    xAxis: {
                        categories: []
                    },
                    yAxis: {
                        title: {
                            text: 'Jumlah Pelawat'
                        },
                        plotLines: [{
                                value: 0,
                                width: 1,
                                color: '#808080'
                            }]
                    },
                    tooltip: {
                        headerFormat: '<span style="font-size:11px">{series.name}</span><br>',
                        pointFormat: '<span style="color:{point.color}">{point.name}</span>:<b>{point.y}</b> of total<br/>'
                    },
                    plotOptions: {
                        series: {
                            borderWidth: 0,
                            dataLabels: {
                                enabled: true,
                                format: '{point.y}'
                            }
                        }
                    },
                    legend: {
                        layout: 'vertical',
                        align: 'right',
                        verticalAlign: 'top',
                        x: -40,
                        y: 100,
                        floating: true,
                        borderWidth: 1,
                        shadow: true
                    },
                    series: []
                };
                function getAjaxData(id) {
                    $.getJSON("data/data-basic-colm-ajax.php", function(json) {
                        options.xAxis.categories = json[0]['data']; //xAxis: {categories: []}
                        options.series[0] = json[1];                        
                        chart = new Highcharts.Chart(options);
                    });
                }

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

    </head>
    <body>
        <a class="link_header" href="/highcharts/">&lt;&lt; Back to index</a>
        <div class="menu_top" >

        <div id="dynamic_data">

                <input type="date" id="startdate">
                <input type="date" id="enddate">
           </div>
        </div>
        <div id="container" style="min-width: 400px; height: 400px; margin: 0 auto;"></div>
    </body>
</html>

这是我的data-basic-colm-ajax.php 文件,

<?php

require '../../conn/connection.php';

$startdate = $_GET['startdate'];
$enddate = $_GET['enddate'];


$result = mysql_query('SELECT DISTINCT(membername), COUNT(membername)
AS member 
FROM responses
WHERE timerecorded>=" . $startdate . " AND AND timerecorded<=" . $enddate . " GROUP BY membername;');

$bln = array();
$bln['name'] = 'Bulan';
$rows['name'] = 'Jumlah Pelawat';
while ($r = mysql_fetch_array($result)) {
    $bln['data'][] = $r['membername'];
    $rows['data'][] = $r['member'];
}
$rslt = array();
array_push($rslt, $bln);
array_push($rslt, $rows);
print json_encode($rslt, JSON_NUMERIC_CHECK);

mysql_close($con);

由于我是 Ajax 新手,因此我们将不胜感激。

最佳答案

我不确定为什么你想要在 div 上进行 change 事件,尝试更改为:

<div id="dynamic_data">
   <input type="date" id="startdate">
   <input type="date" id="enddate">
   // add button
   <input type="button" id="myBtn">
</div>

JS

// this function called after button was clicked
$('#myBtn').click(function(){
   var startdate = $('#startdate').val(),
      enddate   = $('#enddate').val();
     getAjaxData(startdate, enddate);
});

// i'm assume the queries from database already worked out
// simply use this below code to send parameter during request
function getAjaxData(startdate, enddate) {
  $.getJSON( "data/data-basic-colm-ajax.php", { startdate: startdate, enddate: enddate }, function(json) {
     options.xAxis.categories = json[0]['data']; //xAxis: {categories: []}
     options.series[0] = json[1];                        
     chart = new Highcharts.Chart(options);
  });
}

如果您想在第一次加载页面时显示图表,请使用日期的默认值调用 getAjaxData(startdate, enddate)

关于php - 使用 Ajax 生成两个日期之间的 HighChart,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32114158/

相关文章:

javascript - 将 jQuery 生成的表发送到另一个 php 页面

javascript - Ajax:从不同域加载XML?

javascript - 使用ajax和php时如何防止在服务器端多次提交上传

javascript - jQuery .on click 事件并不总是在 Ajax 加载的内容上触发

php - mysql查询顺序错误

javascript - 如何将CSS类添加到父元素?

javascript - 如何删除 li 元素

javascript - 从数据库中检索后将 CSS 类添加到 HTML 标签

php - 通过 PHP 文件查询的表单条目和使用 JQuery AJAX 的 PHP 文件的输出结果

php - Laravel 捕获 whoops 应用级异常并使用唯一 ID 发布到日志#