Bayanihan 4
Bayanihan 4

Reputation: 201

How can I put sign (%) on data in ChartJS?

I want to add the sign (%) next to the value. The problem is when I concatenate sign (%) the pie chart is not showing data: vm.distributed_per_day.map(item => (item.Amount / sum * 100).toFixed(2) + '%'). How can I add the sign (%) to my pie chart? Can somebody help me with my problem? Here's my code

  retrieveDistributedPerDayByPie : function() {
        var self = this;
        var sum = 0;

        axios.post(this.urlRoot + this.api + "retrieve_transaction_per_day.php")
        .then(response => {
            console.log(response);
            vm.distributed_per_day = response.data
                var ctxChart = self.$refs.myChart2.getContext('2d');

                for(var i = 0; i < this.distributed_per_day.length; i++) {
                    sum += parseFloat(this.distributed_per_day[i].Amount);
                }

                var myChart2 = new Chart(ctxChart, {
                    type: 'pie',
                    data: {
                        labels: vm.distributed_per_day.map(item => item.Day),
                        datasets: [{
                        label: 'Total Amount',
                        data: vm.distributed_per_day.map(item => (item.Amount / sum * 100).toFixed(2) + '%'),
                        backgroundColor: this.poolColors(vm.distributed_per_day.length),
                        borderColor: '#eee',
                        borderWidth: 2
                    }]
                },
                reponsive : true,
                options: {
                    title : {
                        display : true,
                        text : "Distributed Reports per Day",
                        fontFamily: "sans-serif",
                        fontSize: 18,
                    },
                    legend: {
                        display: false
                     },
                     tooltips: {
                        enabled: true
                     }
                }
            });
        }).catch(e => {
            console.log(e)
        });
    },

Upvotes: 3

Views: 1537

Answers (2)

danielm2402
danielm2402

Reputation: 788

This solution worked for me.

tooltips: {
      mode: 'label',
      callbacks: {
        label: function (tooltipItem, data) {
          var indice = tooltipItem.index;
          return data.labels[indice] + ': ' + data.datasets[0].data[indice] + '%';
        }
      }
    }

Upvotes: 1

Se&#225;n Gahan
Se&#225;n Gahan

Reputation: 84

This works on a bar chart, haven't tried it on a pie chart. Hope it works for you.

yAxes: [
    {
        ticks: {
            callback: function (value, index, values) {
                return value + " %";
            }
        }
    }
]

Upvotes: 1

Related Questions