17

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

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

谁能告诉我该怎么做?

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

4

2 回答 2

59

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

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

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

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

highstock 自带的默认tooltip定位器如下(源码

/**
 * 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};
}

有了以上所有信息,我认为您有足够的工具来实现您的要求,只需修改函数以使浮动向右而不是默认向左。

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

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

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

于 2012-08-15T19:23:18.207 回答
1

使工具提示始终位于光标右侧的更好解决方案如下:

function (labelWidth, labelHeight, point) { 
    return { 
        x: point.plotX + labelWidth / 2 + 20,
        y: point.plotY + labelHeight / 2 
    };
}
于 2018-03-15T21:37:09.437 回答