如何在 Chart.js 中的图形工具提示中附加更多数据

3 jquery graph chart.js

最近,我致力于使用图表显示数据库表中的数据chart.js。在该图形工具提示中,显示了 2 个数据,但现在我还想在该工具提示中合并第三个数据。

喜欢:

2016年1月客户

2016 年月度消费者:-4.5

损失15%(第三次)

下面是当前图表的快照 在此输入图像描述

这也是一个数据库表数据截图

在此输入图像描述

这是我的代码

    $(document).ready(function(){
    $.ajax({
        url: "<?php base_url();?>/charts/getsome",
        method: "GET",
        success: function(data) {
            console.log(data);
            var data = JSON.parse(data);
            var month = [];
            var customers = [];

            for(var i in data) {
                month.push("Customer in " + data[i].apply_month);
                customers.push(data[i].no_customers);
            }
            var chartdata = {
                labels: month,              
                datasets : [
                    {
                        label: 'monthly customers for Year 2016',
                        backgroundColor: 'rgba(200, 200, 200, 0.75)',
                        borderColor: 'rgba(200, 200, 200, 0.75)',
                        hoverBackgroundColor: 'rgba(200, 200, 200, 1)',
                        hoverBorderColor: 'rgba(200, 200, 200, 1)',
                        data: customers,
                        fill: false
                    }

                ]
            };

            // alert(chartdata);

            var frame = $("#mycanvas");

            var barGraph = new Chart(frame, {
                type: 'line',               
                data: chartdata
            });
        },
        error: function(data) {
            console.log(data);
        }
    });
});
Run Code Online (Sandbox Code Playgroud)

现在请建议我,如何在这些工具提示中附加第三个数据。谢谢 :)

ɢʀᴜ*_*ᴜɴᴛ 5

您可以使用afterBody工具提示的回调函数来实现这一点...

options: {
   tooltips: {
      callbacks: {
         afterBody: function(t, d) {
            return 'loss 15%';  // return a string that you wish to append
         }
      }
   },
   ...
}
Run Code Online (Sandbox Code Playgroud)

这是一个工作示例...

options: {
   tooltips: {
      callbacks: {
         afterBody: function(t, d) {
            return 'loss 15%';  // return a string that you wish to append
         }
      }
   },
   ...
}
Run Code Online (Sandbox Code Playgroud)
var ctx = document.getElementById("myChart").getContext('2d');

var myChart = new Chart(ctx, {
   type: 'line',
   data: {
      labels: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun'],
      datasets: [{
         label: 'Standard Rating',
         data: [1, 2, 3, 4, 5, 6],
         backgroundColor: 'rgba(209, 230, 245, 0.5)',
         borderColor: 'rgba(56, 163, 236, 1)',
         borderWidth: 1
      }]
   },
   options: {
      responsive: false,
      tooltips: {
         callbacks: {
            afterBody: function(t, d) {
               return 'loss 15%'; //return a string that you wish to append
            }
         }
      },
      scales: {
         yAxes: [{
            ticks: {
               beginAtZero: true
            }
         }]
      }
   }
});
Run Code Online (Sandbox Code Playgroud)

  • 没关系。感谢您的努力:) (2认同)