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

nel*_*ela 14 highcharts

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

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

谁能告诉我怎么做?

使用工具提示定位器,我只能设置默认位置.

Jug*_*kar 54

工具提示定位器不仅仅是默认位置.函数参数包含有关您的点位置和工具提示尺寸的信息,使用它可以非常简单地将其定位到右侧.

Highchart/stock允许您按如下方式定义备用定位器

tooltip:{
    positioner:function(boxWidth, boxHeight, point){
        ...
    }
}
Run Code Online (Sandbox Code Playgroud)

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

highstock附带的默认工具提示定位器如下(来源)

/**
 * 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};
}
Run Code Online (Sandbox Code Playgroud)

有了上述所有信息,我认为你有足够的工具来实现你的需求,只需修改函数使float向右而不是默认的left.

我将继续为您提供最简单的定位工具提示实现,您应该能够根据后面提到的默认工具提示定位器的代码实现边缘情况

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

阅读更多@ Customizing Highcharts - 工具提示定位

  • +1很棒的答案.1年后仍然是我能找到的最佳答案和google上的#1搜索结果.应该标记为正确的答案. (2认同)