score:8

Accepted answer

i have something close, tooltips can accept a position which is an alias for a function stored in chart.tooltip.positioners. this function returns the x and y position for the tooltip.

you can add a custom one to adjust the x at an offset.

the only issue is that by adjust the x the layout (left/right direction) of the tooltip can change meaning that even after working out if the tool tip is below half way or above half way bu adjusting the x it then switches its layout meaning on tooltip in the middle will look odd as it is offset in the wrong direction.

this could be fixed by knowing the width of the tooltip and taking this into account but looking through the data provided to the function this is not given.

anyway leaving this as an answer it gets you most of the way there and you/someone might be able to figure out how to get rid of that annoying last part

//global chart.js options
chart.defaults.global.defaultfontfamily = 'lato';
chart.defaults.global.elements.responsive = true;
chart.defaults.global.tooltips.xpadding = 10;
chart.defaults.global.tooltips.ypadding = 10;
chart.defaults.global.tooltips.titlemarginbottom = 10;
chart.defaults.global.tooltips.position = 'average';

//register custome positioner
chart.tooltip.positioners.custom = function(elements, position) {
    if (!elements.length) {
      return false;
    }
    var offset = 0;
    //adjust the offset left or right depending on the event position
    if (elements[0]._chart.width / 2 > position.x) {
      offset = 20;
    } else {
      offset = -20;
    }
    return {
      x: position.x + offset,
      y: position.y
    }
  }
  //individual chart config
var ctx = "mychart";
var mychart = new chart(ctx, {
  type: 'line',
  options: {
    title: {
      display: true,
      text: 'precision-recall curve',
    },
    layout: {
      padding: 32
    },
    tooltips: {
      //use our new custom position
      position: 'custom'
    },
  },
  data: {
    labels: ['0%', '10%', '20%', '30%', '40%', '50%', '60%', '70%', '80%', '90%', '100%'],
    datasets: [{
      label: 'precision',
      data: [2, 42, 55, 50, 42, 38, 32, 24, 20, 18, 18],
      bordercolor: '#1abc9c',
      backgroundcolor: 'rgba(26, 188, 156, .4)',
      pointbordercolor: "#4bc0c0",
      pointbackgroundcolor: "#fff",
      pointhitradius: 10
    }, {
      label: 'recall',
      data: [2, 12, 24, 30, 39, 58, 70, 82, 86, 89, 93],
      bordercolor: '#34495e',
      backgroundcolor: 'rgba(52, 73, 94, .3)',
      pointbordercolor: "#34495e",
      pointbackgroundcolor: "#fff",
      pointhitradius: 10
    }]
  }
});
<div class="container">
  <div>
    <canvas id="mychart"></canvas>
  </div>
</div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/chart.js/2.4.0/chart.bundle.min.js"></script>

score:2

i think it's simpler by change the number of caretpadding. we can increase the distance from tooltip to data point by caretpadding

option: {
   tooltip: {
       caretpadding: 20,
   }
}

caretpadding-image


Related Query

More Query from same tag