user13423578
user13423578

Reputation:

Javascript and chart.js, how to add euro currency to y label

I have the following jquery code to add a chart.js bar chart:

var config = {
                            type: 'bar',
                            data: {
                              datasets: [{
                                {% for key, value in iva_saldo_totale_trimestrale.items %}
                                data: {{ value|safe }},
                                {% endfor %}
                                backgroundColor: 'blue',
                                label: 'IVA'

                              }],

I have tried to add € to my label, but the following code does not work. why?

yAxes: [{                         
ticks: {
        callback: function(value, index, values) {
             return '€' + value;
       } 

Upvotes: 0

Views: 955

Answers (1)

Brijesh Prasad
Brijesh Prasad

Reputation: 579

In order to add custom label, you should attach a callback as below :

var chart = new Chart(ctx, {
    type: 'line',
    data: data,
    options: {
        scales: {
            yAxes: [{
                ticks: {
                    // Include a dollar sign in the ticks
                    callback: function(value, index, values) {
                        return '$' + value;
                    }
                }
            }]
        }
    }
});

Important thing to remember here is that, if the callback returns null or undefined, the associated grid line will be hidden

Upvotes: 1

Related Questions