如何使Highcharts中的渲染标签始终可见,并且相对于单击或悬停的点仍然可见

How to make a rendered label in Highcharts always visible and still relative to the point clicked or hovered over

本文关键字:单击 相对于 悬停 Highcharts 标签 何使      更新时间:2023-09-26

除了点击时打开的工具提示外,我还需要一个鼠标悬停时打开的不同工具提示。

我试过使用render.lable,但是当标签被渲染时,它有时会离开图表区域,变得不可读。有没有办法使渲染的标签不超出图表区域或图表顶部的位置?

$(function () {
$('#container').highcharts({
    title: {
        text: 'Highcharts custom label'
    },
    xAxis: {
        categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
    },
    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]
    }]
}, function (chart) { // on complete
    var point = chart.series[0].points[8];
    chart.renderer.label('Max observation', 340, 50, 'callout', point.plotX + chart.plotLeft, point.plotY + chart.plotTop)
        .css({
            color: '#FFFFFF'
        })
        .attr({
            position: 'absolute',
            fill: 'rgba(0, 0, 0, 0.75)',
            padding: 8,
            r: 5,
            zIndex: 999
        })
        .add();
});

});

http://jsfiddle.net/54tnht7b/

我会以不同的方式处理这个问题。

对于悬停时需要激活的零件,我会使用工具提示的标准功能,并在单击时使用单独的弹出窗口。

请参阅以下答案以获取示例:

  • Highcharts:工具提示单击而不是悬停
chart.renderer.label('Max observation', 340, 50, 'callout', point.plotX + chart.plotLeft, point.plotY + chart.plotTop)
    .css({
        color: '#FFFFFF'
    })

在此块中,您将标签的位置设置为不在图表上的区域。

 chart.renderer.label('Max observation', 250, 50, 'callout', point.plotX + chart.plotLeft, point.plotY + chart.plotTop)
    .css({
        color: '#FFFFFF'
    })

在这里,它被更改为重新出现在图表上。这是jsfiddle-http://jsfiddle.net/54tnht7b/2/

这就是你想要的吗?