JavaScript Chart.js - Custom data formatting to display on tooltip

前端 未结 11 2048
面向向阳花
面向向阳花 2020-12-07 20:13

I have looked at various documentation and similar questions on here, but cannot seem to find the particular solution. Apologies if I have missed anything obvious or have re

11条回答
  •  無奈伤痛
    2020-12-07 20:58

    This is what my final options section looks like for chart.js version 2.8.0.

            options: {
            legend: {
                display: false //Have this or else legend will display as undefined
            },
            scales: {
                //This will show money for y-axis labels with format of $xx.xx
                yAxes: [{
                  ticks: {
                    beginAtZero: true,
                    callback: function(value) {
                        return (new Intl.NumberFormat('en-US', {
                            style: 'currency',
                            currency: 'USD',
                        })).format(value);
                    }
                  }
                }]
            },
            //This will show money in tooltip with format of $xx.xx
            tooltips: {
                callbacks: {
                    label: function (tooltipItem) {
                        return (new Intl.NumberFormat('en-US', {
                            style: 'currency',
                            currency: 'USD',
                        })).format(tooltipItem.value);
                    }
                }
            }
        }
    

    I wanted to show money values for both the y-axis and the tooltip values that show up when you hover over them. This works to show $49.99 and values with zero cents (ex: $50.00)

提交回复
热议问题