如果日期范围太大,则在Highcharts中分组日期

时间:2013-08-22 16:57:53

标签: highcharts

我使用Highcharts为我的客户显示一些统计信息,但当客户选择长数据范围时我遇到问题

这是默认视图enter image description here

中我的高保真的第一张图片

如果我在这里选择太长的日期范围就是结果 enter image description here

这是我的代码

$(function () {
    var chart;
    $(document).ready(function() {
        chart = new Highcharts.Chart({
            chart: {
                renderTo: 'container',
                type: 'line',
                marginRight: 50,
                marginBottom: 80,
                dataGrouping: {
                    enabled: true
                }
            },                  
            title: {
                text: 'Visits Statistics',
                x: -20 //center
            },
            credits: {
                text: '',
                href: ''
            },
            subtitle: {
                text: '',
                x: -20
            },
            xAxis: {
                categories: [<?php print $categories; ?>],
                labels: {
                    rotation: -45,
                    align: 'right',
                    style: {
                        fontSize: '10px',
                        fontFamily: 'Verdana, sans-serif'
                    }
                }
            },
            yAxis: {
                title: {
                    text: 'Visits'
                },
                min: 0,
                plotLines: [{
                    value: 0,
                    width: 1,
                    color: '#808080'
                }]
            },
            tooltip: {
                formatter: function() {
                    return '<b>'+ this.x +' '+'</b><br/>'+ this.y +'Hit';
                }
            },
            legend: {
                layout: 'vertical',
                align: 'right',
                verticalAlign: 'top',
                x: -10,
                y: 10,
                borderWidth: 0
            },
            series: [{name:'from 2011-09-1',data: [<?php print $visits; ?>]}]
        });
    });
});

1 个答案:

答案 0 :(得分:3)

如果您的图表配置正确,Highcharts可以自动管理x轴中的时间值。您的案例中的问题是,您已告知Highcharts使用您的类别,并显示所有类别。

要设置图表以避免这种情况,您需要做两件事:

  • 将x轴类型设置为datetime
  • 确保您的数据格式正确
    • 或者,如果您无法处理数据,请使用pointStartpointInterval

使用您的示例:

// ...
xAxis: {
    //remove categories and set type as 'datetime'
    type: 'datetime',
    labels: {
        rotation: -45,
        align: 'right',
        style: {
            fontSize: '10px',
            fontFamily: 'Verdana, sans-serif'
        }
    }
},
// ...
series: [{
    name:'from 2011-09-1',
    // since you probably don't want to change your data, we leave it alone...
    data: [<?php print $visits; ?>],
    // ... and instead, set `pointStart` and `pointInterval`
    pointStart: Date.UTC(2011, 8, 1), // September 1, 2011
    pointInterval: 24 * 3600 * 1000 // each point is 1 day (measured in milliseconds)
}]