Highcharts数据未正确显示

时间:2015-11-18 13:25:49

标签: javascript highcharts

我有一张图表,显示员工每天的上班时间和超时时间。我从数据库中获取了数据,一切运行良好。但现在列显示图表中的数据不正确。我无法弄清楚为什么会如此。有人可以帮我吗?

This is my chart. The column is always full no matter what value is set.

This is the data that is coming from the json file.

function InitHighChart()
{
    $("#chart").html("Wait, Loading graph...");

    var options = {
            chart: {
            type: 'column',
            renderTo: 'chart',
            zoomType: 'xy',
            backgroundColor:'transparent'
        },
        credits: {
            enabled: false
        },
        title: {
            text: '<?php echo $name; ?>\'s Attendence',
            x: -20
        },
        xAxis: {
            type: 'datetime',
            categories: [{}],
            labels: {
                rotation: -20,
            }           
        },
         yAxis: {

            type: 'datetime',
            plotBands: [{ 
                from: 1447747200000,
                to: 1447779600000,
                color: 'rgba(68, 170, 213, 0.1)',
                zIndex: 5,
               label: {
                    text: 'Office Timing',
                     style: {
                        color: '#606060'
                    } 
                } 
            }],
             plotLines: [{
                    //value: $inavg,
                    color: 'green',
                    dashStyle: 'shortdash',
                    width: 2,
                    label: {
                        text: 'Average Time In'
                    },
                    zIndex: 5
                }, {
                    //value: $outavg,
                    color: 'red',
                    dashStyle: 'shortdash',
                    width: 2,
                    label: {
                        text: 'Average Time Out'
                    },
                    zIndex: 5
                }],

        dateTimeLabelFormats: {

            minute: '%l:%M %p',
            hour: '%l:%M %p'

        },
        min: 1447729200000,
            max: 1447797600000,
        tickInterval:  1000,
                title: {
                    text: 'Time'
                }
            }, 
        tooltip: {
            formatter: function() {
                var s = '<b>'+ this.x +'</b>';

                $.each(this.points, function(i, point) {
                     s += '<br/>'+  point.series.name + ':  ' + Highcharts.dateFormat('%l:%M %p', point.y)
                });

                return s;
            },
            shared: true
        },
        series: [{},{}]
    };  

    $.ajax({
        url: "json.php?id=<?php echo $_GET['id']; ?>",
        type:'post',
        dataType: "json",
        success: function(data){
            options.xAxis.categories = data.categories;
            options.yAxis.plotLines[0].value = data.inavg;
            options.yAxis.plotLines[1].value = data.outavg;
            options.series[0].name = 'Time In';
            options.series[0].data = data.timein;
            options.series[1].name = 'Time Out';
            options.series[1].data = data.timeout;
            console.log(options);
            var chart = new Highcharts.Chart(options);          
        }
    });
}





<?php 
require_once("session.php"); 
require_once("connection.php");
$id= $_GET['id'];   
$query= "SELECT * ";
$query.= "FROM attendence ";
$query.= "WHERE emp_id = {$id}";
$result=mysqli_query($conn,$query);
if(!$result){
echo ("Database query failed. ".mysqli_connect_error());
}  
$categories=array();
$timein=array();
$timeout=array();
$avrgi="";
$avrgo="";
$count=0;
while($row = mysqli_fetch_assoc($result)){ 
$categories[]= $row['date'];
$itime=$row['time_in'];
$split=str_split($itime, 11);
$timei=end($split);
$timein[]= strtotime($timei)*1000;
$otime=$row['time_out'];
$osplit=str_split($otime, 11);
$timeo=end($osplit);
$timeout[]= strtotime($timeo)*1000;
$avrgi+=strtotime($timei)*1000;
$avrgo+=strtotime($timeo)*1000;
$count++;
} 
$inavg= $avrgi/$count;
$outavg= $avrgo/$count;      

$graph_data=array('categories'=>$categories,'timein'=>$timein,
'timeout'=>$timeou    t,'inavg'=>$inavg,'outavg'=>$outavg);
echo json_encode($graph_data);
exit;
?>

1 个答案:

答案 0 :(得分:0)

如果无法查看图表,我猜您发送的数据超出了轴的最大值。

如果上面代码中的数据和链接图像中的数据准确无误,那确实如此。因此,条形延伸超出了绘图区域的末端。

您要根据第一个日期在轴上设置最小值和最大值,然后将数据数据发送到各个日期的系列。那是行不通的......

如果要根据时间轴绘制多个日期,则需要处理和抽象数据,例如秒数,而不是使用时间戳。