score:5

Accepted answer

here's one solution i've put together in jsfiddle, although it's a bit of a hack.

add the following line to the mouseover event:

chart.tooltip.refresh(chart.series[1].points[2]);

this displays the tooltip for an appropriately placed point in a hidden series.

a custom tooltip formatter then returns the required text.

score:2

another solution would be to use the "label" property of plotbands/plotlines with "usehtml:true" flag and css hover.

xaxis: {
//...code    
   plotlines: [{
      //...code
      dashstyle: 'dash',
      color: '#000000', 
      width: 2,
      label: {
          text: `<div class="plotline">
                     <div id="custom-tooltip" class="thetooltip">
                         my custom tooltip!
                     </div>
                 </div>`,
          usehtml: true,
      }
   }]
}

modified css would be:

.thetooltip {
    display: none; //the main change
    border: 1px solid #2f7ed8;
    background-color: #fff;
    opacity: 0.8500000000000001;
    padding: 4px 12px;
    border-radius: 3px;
    position: absolute;
    top:100px;
    box-shadow: 1px 1px 3px #666;
}

and of course in plotline class:

.plotline {
     &:hover {
       .thetooltip {
          display: block;
        }
     }
}

in my opinion, it is a more robust solution - first, it is a css solution, not js, and from the ux/ui point of view works smoother than the quirky js (just try to set the timeout right!)

score:8

i made another variant based on gregs answer but for plotlines instead, since that was what i needed. i suppose it would be easily translated to plotbands as well.

this variant also works with events, but instead displays another div instead of relying on a particular hidden series.

the example can be found at jsfiddle.

so the tooltip is added in the same container as the chart

<div id="tooltip" class="thetooltip">
    <p id="tooltiptext" style="margin:0">default</p>
</div>

and when the user mouses over a plotline an event is fired which displays the tooltip along with an attached tooltip

var $tooltip = $('#tooltip');
$tooltip.hide();
var $text = $('#tooltiptext');
displaytooltip = function (text, left) {
    $text.text(text);
    $tooltip.show();
    $tooltip.css('left', parseint(left)+24 + 'px');
};
var timer;
hidetooltip = function (e) {
    cleartimeout(timer);
    timer = settimeout(function () {
        $tooltip.fadeout();
    }, 5000);
};

and then each plotline is defined with an additional option, tooltiptext, and events for displaying the div above.

plotlines: [{
    tooltiptext: 'hello',
    value: date.utc(2011, 2, 28, 0, 1, 1),
    color: '#ff6666',
    dashstyle: 'solid',
    width: 3,
    events: {
        mouseover: function (e) {
            displaytooltip(this.options.tooltiptext, this.svgelem.d.split(' ')[1]);
        },
        mouseout: hidetooltip
    }
}

the tooltiptext is not part of highcharts api, but when defined it is available via the options property on the element.

the css for the tooltip can be defined similar to the default one by highcharts simply by mimicing the css:

.thetooltip {
    border: 1px solid #2f7ed8;
    background-color: #fff;
    opacity: 0.8500000000000001;
    padding: 4px 12px;
    border-radius: 3px;
    position: absolute;
    top:100px;
    box-shadow: 1px 1px 3px #666;
}

Related Query

More Query from same tag