如何使用两个y轴删除高图图表中的间隙

时间:2013-12-10 19:05:57

标签: javascript jquery graph highcharts

我一直试图摆脱左边的差距而没有任何作用。只有当我在右侧添加反向yAxis时才会出现此问题。以下是我正在使用的代码:

$(function () {
    var categories = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
    $('#container').highcharts({
        chart: {
            type: 'areaspline',
            zoomType: 'xy',
        },
        title: {
            text: 'Stats',
        },
        subtitle: {
            text: 'YTD 2013 - 2014',
        },
        xAxis: {
            labels: {
                enabled: true,
                formatter: function () {
                    return categories[this.value];
                }
            },
            tickInterval: 1,
            minPadding: 0,
            maxPadding: 0,
            startOnTick: true,
            endOnTick: true,
        },
        yAxis: [{ //--- Primary yAxis
            min: 0,
            minPadding: 0,
            maxPadding: 0,
            startOnTick: true,
            endOnTick: true,
            title: {
                text: 'Mds',
                style: {
                    color: '#8dc63f',
                }
            },
            labels: {
                style: {
                    color: '#8dc63f',
                    fontWeight: 'bold'
                }
            }
        }, { //--- Secondary yAxis


            labels: {
                formatter: function () {
                    var mil = '$';
                    return mil + Highcharts.numberFormat(this.value, 0);
                },
                style: {
                    color: '#3fb4ed',
                    fontWeight: 'bold'
                }
            },
            title: {
                text: 'Revenue',
                style: {
                    color: '#3fb4ed',
                }

            },
            opposite: true
        }],
        series: [{
            type: 'areaspline',
            color: '#005a84',
            fillOpacity: 0.2,
            yAxis: 0,
            zIndex: 3,
            name: 'Goals',
            data: [5, 5, 5, 5, 6, 8, 8, 8, 10, 10, 10, 10],
        }, {
            type: 'areaspline',
            color: '#8dc63f',
            fillOpacity: 0.2,
            yAxis: 0,
            zIndex: 4,
            name: 'This Year',
            data: [21, 14, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        }, {
            type: 'areaspline',
            color: '#d9531e',
            fillOpacity: 0.2,
            yAxis: 0,
            zIndex: 3,
            visible: false,
            name: 'Last Year',
            data: [20, 2, 2, 7, 8, 5, 7, 3, 2, 2, 5, 5],
        }, {
            type: 'column',
            color: '#3fb4ed',
            fillOpacity: 0.2,
            yAxis: 1,
            zIndex: 1,
            visible: false,
            name: 'Revenue',
            data: [1, 1, 1, 1, 1, 1, 1, 1, 3, 4, 4, 2]
        }]

    });
});

我已经尝试了所有可能的解决方案。通过API文档并搜索每个论坛。任何帮助都会非常有用。

3 个答案:

答案 0 :(得分:2)

您可以设置xAxis:

    xAxis: {
        labels: {
            enabled: true,
            formatter: function () {
                return categories[this.value];
            }
        },
        tickInterval: 1,
        minPadding: 0,
        maxPadding: 0,
        min: 0.5,                     // remove padding
        max: categories.length - 1.5, // remove padding
        startOnTick: false,           // allow to start not from tick
        endOnTick: false              // allow to end not at tick
    },

示例:http://jsfiddle.net/p2EYM/24/ - 启用“收入” - 您将看到第一列和最后一列将被切断。

答案 1 :(得分:0)

这样做的原因是,即使系列在加载时隐藏,您也有一个需要显示宽度的列系列。如果您只显示系列“收入”,您可以看到它正在使用之前存在的空白空间。你也可以玩tickPixelInterval - 但为此你不需要使用分类的xAxis。

example。 对于两个yAxis元素,我也将showEmpty设置为false。

答案 2 :(得分:0)

如果您从

更改系列的最后一个
    type: 'column',

    type: 'areaspline',

左右两侧没有间隙。您需要输入column吗?

用户Dusty在此处Highcharts Remove Space across the x-Axis回答了您的问题的解决方案。您必须为x轴设置:

    min: 0.5,
    max: 10.5,

注意:如果您将column类型系列转为visible: true,则会将第一列的一半和最后一列关闭。