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

时间:2012-08-15 08:37:43

标签: highcharts

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

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

谁能告诉我怎么做?

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

2 个答案:

答案 0 :(得分:53)

工具提示定位器不仅仅是默认位置。函数参数包含有关您的积分位置和信息的信息。工具提示尺寸,使用它可以非常简单地将它放在右边。

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

答案 1 :(得分:1)

将工具提示始终放在光标右侧的更好解决方案如下:

function (labelWidth, labelHeight, point) { 
    return { 
        x: point.plotX + labelWidth / 2 + 20,
        y: point.plotY + labelHeight / 2 
    };
}
相关问题