How to display “%” sign on mouse hover in Pie-Chart

后端 未结 1 430
故里飘歌
故里飘歌 2021-01-15 05:05

I am drawing graph on UI using ChartJS 2.0. And I am able to render a Pie Chart. But I want the mouse-hover to show the data along with a \"%

相关标签:
1条回答
  • 2021-01-15 05:40

    You can edit what is displayed in your tooltip with the callbacks.label method in your chart options, and then simply add a "%" to the default string using :

    • tooltipItems -- See documentation for more information (scroll up a bit to "Tooltip Item Interface")
    • data -- Where the datasets and labels are stored.

    var ctx = document.getElementById("canvas");
    var data = {
        datasets: [{
            data: [93, 4, 3],
            backgroundColor: [
                "#455C73",
                "#BDC3C7",
                "#26B99A",
            ],
            label: 'My dataset' // for legend
        }],
        labels: [
            "Rented",
            "Vacant",
            "Unavailable",
        ]
    };
    var pieChart = new Chart(ctx, {
        type: 'pie',
        data: data,
        options: {
            tooltips: {
                callbacks: {
                    label: function(tooltipItems, data) {
                        return data.labels[tooltipItems.index] + 
                        " : " + 
                        data.datasets[tooltipItems.datasetIndex].data[tooltipItems.index] +
                        ' %';
                    }
                }
            }
        }
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.2.1/Chart.min.js"></script>
    <canvas id="canvas" height="150"></canvas>

    0 讨论(0)
提交回复
热议问题