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

标签 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

最佳答案

我可以使用 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
        };
    }
},

关于highcharts - 如何在 Highchart 中移动工具提示位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39198173/

相关文章:

css - Highchart Angular 溢出容器

mysql - 从 Mysql 获取 highstock 面积图数据。

javascript - HighCharts 折线图上缺少数据点

javascript - 在 Highcharts 中动态添加系列

highcharts - 在 Highcharts 中使用对象数组作为系列数据

javascript - 向 Highcharts 上的 Pie Tooltip 添加额外的数据

javascript - 如何防止在图形区域或浏览器窗口调整大小时隐藏 highcharts 工具提示弹出窗口?

Highcharts 图例项目悬停事件?

javascript - HighCharts 类型错误 : ha is not a function

javascript - 清除后 Highstock 图表不显示