[Chartjs]-Chart.js: Place tooltip for stacked bar charts on top of bar

2👍

Thank you for replying, I found a way but its a hack and might not work for you. consider the following:

//register custom positioner
Chart.Tooltip.positioners.custom = function(elements, position) {
  //debugger;
  return {
    x: position.x,
    y: elements[0]._view.base - (elements[0].height() + elements[1].height())
  }
}


//Individual chart config
var ctx = "myChart";
var myChart = new Chart(ctx, {
  type: 'bar',
  options: {
    title: {
      display: true,
      text: 'Precision-Recall Curve',
    },
    layout: {
      padding: 32
    },
    tooltips: {
      mode: 'index',
      intersect: true,
      position: 'custom',
      yAlign: 'bottom'
    },
    scales: {
      xAxes: [{
        stacked: true
      }],
      yAxes: [{
        stacked: true
      }]
    }
  },
  data: {
    labels: ['0%', '10%', '20%', '30%', '40%', '50%', '60%', '70%', '80%', '90%', '100%'],
    datasets: [{
      label: 'data1',
      data: [5, 56, 90, 6, 42, 67, 32, 24, 20, 18, 56],
      borderColor: '#1acc1c',
      backgroundColor: 'rgba(26, 10, 55, .1)',
      pointBorderColor: "#4Bd1C0",
      pointBackgroundColor: "#fff",
      pointHitRadius: 10
    }, {
      label: 'data2',
      data: [2, 12, 24, 30, 39, 58, 10, 82, 34, 89, 5],
      borderColor: '#34315a',
      backgroundColor: 'rgba(132, 2, 34, .7)',
      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>

Leave a comment