score:60

Accepted answer

tooltip positioner is much more than just default position. the function arguments contain info about your point position & tooltip dimensions, using which it should be fairly simple to position it to the right.

highchart/stock allows you to define your alternate positioner as follows

tooltip:{
    positioner:function(boxwidth, boxheight, point){
        ...
    }
}

note that you have three arguments (boxwidth, boxheight, point) at your disposal, these seem to be sufficient for most of the use cases to calculate a desired tooltip position. boxwidth and boxheight are the width and height that your tooltip will require, hence you can use them for edge cases to adjust your tooltip and prevent it from spilling out of the chart or even worse getting clipped.

the default tooltip positioner that comes with highstock is as follows (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};
}

with all the above information, i think you have sufficient tools to implement your requirement by simply modifying the function to make float to right instead of the default left.

i will go ahead and give you the simplest implementation of positioning tooltip to right, you should be able to implement the edge cases based on the aftermentioned default tooltip positioner's code

tooltip: {
    positioner: function(boxwidth, boxheight, point) {         
        return {x:point.plotx + 20,y:point.ploty};         
    }
}

read more @ customizing highcharts - tooltip positioning

score:1

the better solution to get your tooltip always on the right side of the cursor is the following:

function (labelwidth, labelheight, point) { 
    return { 
        x: point.plotx + labelwidth / 2 + 20,
        y: point.ploty + labelheight / 2 
    };
}

Related Query

More Query from same tag