0

Herewith I used JavaScript line chart within Laravel framework. Mouse hover tool tip to display point values working perfectly with label:function(...). I need to display only y-axis value on every point at chart on create (Without mouse hovering). To perform this I used drawDatasetPointsLabels() method from How to display Line Chart dataset point labels with Chart.js? and call it on chart options. But unfortunately it is not working. Below is the code.

chart.blade

monthlabels={.......};
salesqty = {......};
stockqty = {......};

document.getElementById("history_canvas_holder").innerHTML = ' ';
document.getElementById("history_canvas_holder").innerHTML = '<canvas id="historyChart" width="500" height="350"></canvas>';
var ctx = document.getElementById("historyChart").getContext('2d');
var myLineChart = new Chart(ctx, 
{
  type: 'line',
  data: 
  {
      labels: monthlabels,
      datasets: [{
          label: 'Sales',
          data: salesqty,
          borderColor: [
              'rgba(255,99,132,1)'
          ],
          borderWidth: 2
      },
      {
          label: 'Stock',
          data: stockqty,
          borderColor: [
              'rgba(0,161,232)'
          ],
          borderWidth: 2
      }
      ]
  },
  options: {
      tooltips: {
          callbacks: {
              label: function(tooltipItem, data) {
                  var label = data.datasets[tooltipItem.datasetIndex].label || '';

                  if (label) {
                      label += ': ';
                  }
                  label += Math.round(tooltipItem.yLabel * 100) / 100;
                  return label;
              },
              onAnimationProgress: function() { drawDatasetPointsLabels(ctx) },
              onAnimationComplete: function() { drawDatasetPointsLabels(ctx) } 
          }
      }
  }                  
});

function drawDatasetPointsLabels(ctx) {
  ctx.font = '.9rem "ABCD",sans-serif';
  ctx.fillStyle = '#AAA';
  ctx.textAlign="center";
  $(historyChart.datasets).each(function(idx,dataset){
      $(dataset.points).each(function(pdx,pointinfo){
          if ( pointinfo.value !== null ) { 
              ctx.fillText(pointinfo.value,pointinfo.x,pointinfo.y - 15);
          }
      });
  });         
}
Nuwan Withanage
  • 393
  • 7
  • 19
  • That is a very old question (over 4 years old) and probably is no longer relavant. If you are needing to have labels shown for each of your points, maybe you should look at using the popular Chart.js plugin for labels: https://github.com/chartjs/chartjs-plugin-datalabels – Daniel W Strimpel Sep 03 '19 at 21:34

1 Answers1

0

In the type of v2.5.0

`options: {
    tooltips: {
         enabled: false
    }
}`

or

`tooltips :{
            custom : function(tooltipModel) {
                tooltipModel.opacity = 0;
            }
 }`

type of v2.1.4

`Chart.defaults.global.tooltips.enabled = false;`

people said those ways are work, but not in all charts type.

EZone Lai
  • 106
  • 6