[Chartjs]-How do I type string in the tooltip in Chart.js?

0👍

This one should work properly:

Create a canvas for your chart:

<canvas id="myChart"></canvas>

This is the required javascript code:

var ctx = document.getElementById("myChart").getContext('2d');
var myChart = new Chart(ctx, {
  type: 'pie',
  data: {
    labels: ["Green", "Blue", "Gray", "Purple", "Yellow", "Red", "Black"],
    datasets: [{
      backgroundColor: [
        "#2ecc71",
        "#3498db",
        "#95a5a6",
        "#9b59b6",
        "#f1c40f",
        "#e74c3c",
        "#34495e"
      ],
      data: [12, 19, 3, 17, 28, 24, 7]
    }]
  },
  options: {
      tooltips : {

      callbacks : { 
        label : function(tooltipItem, data) {
          var dataset = data.datasets[tooltipItem.datasetIndex];
                  return  dataset.data[tooltipItem.index] + ' %';
                },
        },
      },
  }
});

And this is the Charts.js cdn that i have used.

Here is the result:

https://imgur.com/gallery/3KszJQY

This is a working js fiddle.

Hope it helps.

1👍

Something like this should solve your problem:

<script>
    var ctx = document.getElementById('myChart').getContext('2d');
    var myChart = new Chart(ctx, {
        type: 'pie',
        data: {
            labels: ['@lang('home/main.korea')', '@lang('home/main.japan')'],
            datasets: [{
                label: [

                ],
                data: [
                    {!! $koreaPercent !!} ,
                    {!! $japanPercent !!} ,
                ],
                backgroundColor: [
                    'red',
                    'blue',
                ],
            }]
        },
        options: {
            tooltips: {
                enabled: true,
                mode: 'single',
                callbacks: {
                    label: function(tooltipItems, data) { 
                        return tooltipItems.yLabel + tooltipItems.xLabel + " %";
            }
        }
    });

</script>

here is a working example with different data, since i don’t know how are u using your chart: https://jsfiddle.net/mateusjunges/f8x394va/2/

Hope it helps.

Leave a comment