javascript - 如何在鼠标悬停时获取 highcharts 图形点的值?

标签 javascript highcharts

我正在使用以下逻辑创建一个 highcharts 图表系列:

this.series = [];

for(var i in headerData) {
    var header = headerData[i];

    this.series.push({
        name: header.name,
        data:[],
        yAxis:parseInt(header.axis),
        id: header.id,
        type: 'column',
        zIndex: 1,
        events: {
            mouseOver: function (e) {
                console.log('Point: ', e.point);
            }
        }
    });
}

read该点是事件 e 的属性,但在我的例子中 e.pointundefined。我也无法在 e 中找到与鼠标悬停点相关的任何内容。

有人知道如何获取鼠标悬停的点的 x 值和 y 值吗?

最佳答案

您需要在 plotOptions 属性上设置 event。下面是一个取自 Highcharts API 文档的示例:

$(function () {
    var $reporting = $('#reporting');

    $('#container').highcharts({

        title: {
            text: 'Mouse events demo'
        },
        subtitle: {
            text: 'On point mouse over or mouse out, the values should be reported in top left'
        },
        plotOptions: {
            series: {
                point: {
                    events: {
                        mouseOver: function () {
                            var chart = this.series.chart;
                            if (!chart.lbl) {
                                chart.lbl = chart.renderer.label('')
                                    .attr({
                                        padding: 10,
                                        r: 10,
                                        fill: Highcharts.getOptions().colors[1]
                                    })
                                    .css({
                                        color: '#FFFFFF'
                                    })
                                    .add();
                            }
                            chart.lbl
                                .show()
                                .attr({ 
                                    text: 'x: ' + this.x + ', y: ' + this.y 
                                });
                        }
                    }
                },
                events: {
                    mouseOut: function () {
                        if (this.chart.lbl) {
                            this.chart.lbl.hide();
                        }
                    }
                }
            }
        },

        tooltip: {
            enabled: false
        },

        series: [{
            data: [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4]
        }]
    });
});

Demo

关于javascript - 如何在鼠标悬停时获取 highcharts 图形点的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30064294/

相关文章:

javascript - 如何从字符串中剪切第三个符号并在没有第三个符号的情况下发出警报

javascript - Fabric js : Increase font size instead of just scaling when resize with mouse

javascript - MaterializeCSS - ASP.Net MVC : Checkbox in modal

javascript - 主题行邮件中的 Mailto 链接无法使用 JavaScript 工作

php - highcharts JSON 谷歌地球

php - 单击时为 Highcharts 生成有效的 JSON

jquery - 需要使用 Highcharts 使工具提示边框颜色与标记颜色相同

javascript - 具有复合键的 Backbone.js

javascript - 在 react 中动态更新 Highcharts 图表

highcharts - 如何在 HIGHCHARTS 上的一个系列中包含两个数据?