2012-08-15 103 views

回答

49

工具提示定位器远不止是默认位置。函数参数包含有关您的点位置的信息&工具提示尺寸,使用它可以很方便地将它放在右侧。

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

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

请注意,您在您的处置三个参数(boxWidth,boxHeight,点),这些似乎足以满足大多数的使用情况来计算所需的提示位置。 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}; 
} 

与所有上述信息,我觉得你有足够的工具,通过简单修改功能,使浮来实现您的要求对,而不是默认的左边。

我会继续前进,给你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

+0

如果你是在谈论缺省定位器代码中的注释,那么你应该感谢Highchart团队。如果它的答案作为整体,那么谢谢:) – 2012-08-19 18:43:49

+2

+1很好的答案。 1年后,仍然是我可以找到的最佳答案和谷歌上的#1搜索结果。应该标记为正确的答案。 – 2013-10-29 18:23:52