Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove the label and show only value in tooltips of a bar chart

I am using ChartJS library to create charts. In the tooltip, I am showing the data value from the dataset I created. It works in case if the chart type is doughnut. Otherwise it doesn't work in case or bar or horizontalbar charts.

Whatever I do it shows the data with the labels.

Working JSFiddle of Doughnut Chart with data in tooltip.

Doughnut Chart


Working JSFiddle of Bar Chart with label + data in tooltip.

Bar Chart

All I want to do is to remove the label shown in the tooltips of the bar chart.

var ctx = document.getElementById("myChart");

var myChart = new Chart(ctx, {
  type: 'bar',
  data: {
    labels: [
      "Men",
      "Women",
      "Unknown"
    ],
    datasets: [{
        label: 'Men',
        data: [60, 40, 20],
        backgroundColor: [
          'rgba(158, 216, 202, 0.75)',
          'rgba(255, 150, 162, 0.75)',
          'rgba(160, 160, 160, 0.75)'
        ]
      },
      {
        label: 'Women',
        data: [40, 70, 10],
        backgroundColor: [
          'rgba(158, 216, 202, 0.5)',
          'rgba(255, 150, 162, 0.5)',
          'rgba(160, 160, 160, 0.5)'
        ]
      },
      {
        label: 'Unknown',
        data: [33, 33, 34],
        backgroundColor: [
          'rgba(158, 216, 202, 0.25)',
          'rgba(255, 150, 162, 0.25)',
          'rgba(160, 160, 160, 0.25)'
        ]
      }
    ]
  },
  options: {
    tooltips: {
      callbacks: {
        label: function(tooltipItem, data) {
          var datasetLabel = '';
          var label = data.labels[tooltipItem.index];
          return data.datasets[tooltipItem.datasetIndex].data[tooltipItem.index];
        }
      }
    }
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.3.0/Chart.bundle.min.js"></script>

<canvas id="myChart" width="400" height="200"></canvas>
like image 476
Suhaib Janjua Avatar asked Feb 17 '17 09:02

Suhaib Janjua


2 Answers

just use the title option with an empty value. Like this:

callbacks: {
        title: function(tooltipItems, data) {
          return '';
        },
        label: function(tooltipItem, data) {
          var datasetLabel = '';
          var label = data.labels[tooltipItem.index];
          return data.datasets[tooltipItem.datasetIndex].data[tooltipItem.index];
        }
      }

see updated jsfiddle

like image 52
pumpkinzzz Avatar answered Oct 12 '22 13:10

pumpkinzzz


this is how to get it done

tooltip: {
    enabled: true,
    displayColors:false,
    callbacks: {
        label:(tooltipItem)=>{
            return tooltipItem.parsed;
        },
    }
},
like image 42
DilanTsasi Avatar answered Oct 12 '22 12:10

DilanTsasi