如何在Highchart中移动工具提示位置?

时间:2016-08-29 03:39:42

标签: highcharts

我使用highcharts.js来构建水平条形图。它工作正常,但默认工具提示水平显示,所以我希望工具提示位置是垂直的而不是水平的。

有可能实现这一目标吗?任何帮助赞赏! JSFiddle - Example

示例代码:

$(function () {
    var chart;

    $(document).ready(function() {

        chart = new Highcharts.Chart({

            chart: {

                renderTo: 'container',

                type: 'bar'

            },

            title: {

                text: 'Stacked bar chart'

            },

            xAxis: {

                categories: ['Apples', 'Oranges', 'Pears', 'Grapes', 'Bananas']

            },

            yAxis: {

                min: 0,

                title: {

                    text: 'Total fruit consumption'

                },
                stackLabels: {
                enabled: true,
                style: {
                    fontWeight: 'bold',
                    color: (Highcharts.theme && Highcharts.theme.textColor) || 'gray'
                }
            }

            },

            legend: {

               align: 'right',
            x: -100,
            verticalAlign: 'top',
            y: 20,
            floating: true,
            backgroundColor: (Highcharts.theme && Highcharts.theme.legendBackgroundColorSolid) || 'white',
            borderColor: '#CCC',
            borderWidth: 1,
            shadow: false

            },

            tooltip: {

              formatter: function() {
                return '<b>'+ this.x +'</b><br/>'+
                    this.series.name +': '+ this.y +'<br/>'+
                    'Total: '+ this.point.stackTotal;
            }

            },

            plotOptions: {

                series: {

                    stacking: 'normal',
                    dataLabels: {
                    enabled: true,
                    color: (Highcharts.theme && Highcharts.theme.dataLabelsColor) || 'white'
                }


                }

            },

                series: [{

                name: 'John',

                data: [5, 3, 4, 7, 2]

            }, {

                name: 'Jane',

                data: [2, 2, 3, 2, 1]

            }, {

                name: 'Joe',

                data: [3, 4, 4, 2, 5]

            }]

        });

    });


});

预期输出 enter image description here

1 个答案:

答案 0 :(得分:3)

我可以使用Tooltip.positioner解决此问题,如下所示 -

tooltip: {

    formatter: function() {
        return '<b>' + this.x + '</b><br/>' +
            this.series.name + ': ' + this.y + '<br/>' +
            'Total: ' + this.point.stackTotal;
    },
    positioner: function(labelWidth, labelHeight, point) {
        var tooltipX = point.plotX + 20;
        var tooltipY = point.plotY - 30;
        return {
            x: tooltipX,
            y: tooltipY
        };
    }
},