Highcharts 工具提示始终位于光标右侧

标签 highcharts

我想在光标右侧显示工具提示。

我查看了文档/示例,但找不到强制工具提示保留在光标右侧的方法。

谁能告诉我该怎么做?

tooltip positioner我只能设置默认位置。

最佳答案

工具提示定位器不仅仅是默认位置。函数参数包含有关点位置和工具提示尺寸的信息,使用这些信息将其定位到右侧应该相当简单。

Highchart/stock allows you to define your alternate positioner如下

tooltip:{
    positioner:function(boxWidth, boxHeight, point){
        ...
    }
}

请注意,您可以使用三个参数(boxWidth、boxHeight、point),这些似乎足以满足大多数用例来计算所需的工具提示位置。 boxWidth 和 boxHeight 是工具提示所需的宽度和高度,因此您可以将它们用于边缘情况来调整工具提示并防止其溢出图表,甚至更糟的是被剪切。

highstock 自带的默认工具提示定位器如下 ( Source )

/**
 * Place the tooltip in a chart without spilling over
 * and not covering the point it self.
 */
getPosition: function (boxWidth, boxHeight, point) {

    // Set up the variables
    var chart = this.chart,
        plotLeft = chart.plotLeft,
        plotTop = chart.plotTop,
        plotWidth = chart.plotWidth,
        plotHeight = chart.plotHeight,
        distance = pick(this.options.distance, 12), // You can use a number directly here, as you may not be able to use pick, as its an internal highchart function 
        pointX = point.plotX,
        pointY = point.plotY,
        x = pointX + plotLeft + (chart.inverted ? distance : -boxWidth - distance),
        y = pointY - boxHeight + plotTop + 15, // 15 means the point is 15 pixels up from the bottom of the tooltip
        alignedRight;

    // It is too far to the left, adjust it
    if (x < 7) {
        x = plotLeft + pointX + distance;
    }

    // Test to see if the tooltip is too far to the right,
    // if it is, move it back to be inside and then up to not cover the point.
    if ((x + boxWidth) > (plotLeft + plotWidth)) {
        x -= (x + boxWidth) - (plotLeft + plotWidth);
        y = pointY - boxHeight + plotTop - distance;
        alignedRight = true;
    }

    // If it is now above the plot area, align it to the top of the plot area
    if (y < plotTop + 5) {
        y = plotTop + 5;

        // If the tooltip is still covering the point, move it below instead
        if (alignedRight && pointY >= y && pointY <= (y + boxHeight)) {
            y = pointY + plotTop + distance; // below
        }
    } 

    // Now if the tooltip is below the chart, move it up. It's better to cover the
    // point than to disappear outside the chart. #834.
    if (y + boxHeight > plotTop + plotHeight) {
        y = mathMax(plotTop, plotTop + plotHeight - boxHeight - distance); // below
    }


    return {x: x, y: y};
}

根据上述所有信息,我认为您有足够的工具来实现您的要求,只需修改函数以使 float 到右侧而不是默认的左侧。

我会继续给你 simplest implementation of positioning tooltip to right ,您应该能够根据后面提到的默认工具提示定位器的代码来实现边缘情况

tooltip: {
    positioner: function(boxWidth, boxHeight, point) {         
        return {x:point.plotX + 20,y:point.plotY};         
    }
}

了解更多@ Customizing Highcharts - Tooltip positioning

关于Highcharts 工具提示始终位于光标右侧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11966286/

相关文章:

javascript - 在 jQuery 工具提示中使用动态 ID 渲染 Highchart

javascript - Graph渲染完成后执行函数

javascript - Highcharts - 向大型树形图添加更多深入分析

javascript - Highcharts:如果先前的数组只有空值,则系列不会更新

javascript - Highcharts,仅在选定点上显示标记

javascript - 如何用HighCharts制作数学图表?

apache-spark - 是否有更好的界面可以向Zeppelin添加Highcharts支持

javascript - Highcharts 不在 Internet Explorer 11 中呈现

javascript - 如何以给定的时间间隔重新渲染从 Rails Controller 检索数据的图形?

php - 如何使用 mysql 数据库中的 X 和 Y 值设置 Highcharts 系列