简体   繁体   English

Chart.js如何在工具提示上显示%

[英]Chart.js how to display % on tooltip

I am new to Chart.js. 我是Chart.js的新手。 I am making a line chart but I am stuck. 我正在制作折线图,但卡住了。 I need to display % in a tooltip, but when I add %, the result is not as expected. 我需要在工具提示中显示%,但是当我添加%时,结果与预期不符。 I have gone to other posts, and none of the solutions helped me out. 我去过其他职位,没有任何解决方案可以帮助我。

HTML: HTML:

 <canvas id="myChart2"></canvas>

JS: JS:

  window.onload = function() {
          var ctx2 = document.getElementById('myChart2').getContext('2d');
          var data = [
          {date:'08-05-2017', run_rate: 50},
          {date:'08-06-2017', run_rate: 40},
          {date:'08-07-2017', run_rate: 30},
          {date:'08-08-2017', run_rate: 10},
          {date:'08-09-2017', run_rate: 6},
          {date:'08-10-2017', run_rate: 78},
          {date:'08-11-2017', run_rate: 32},
          {date:'08-12-2017', run_rate: 24}
        ];
          var dates = data.map(function(obj){
          return obj.date;
          });

          var count = data.map(function(obj){
          return obj.run_rate;
          });

          var myChart = new Chart(ctx2, {
          type: 'line',
          data: {
            labels: dates,
            datasets: [{
              label: '%',
              data: count,
              backgroundColor: "rgba(38, 82, 123, 0.5)"
            }]
          },
          options: {
              legend: {
                  display: false
              },
            maintainAspectRatio: false,
            responsive: true,
            tooltips: {titleFontSize:12, bodyFontSize:12},
            scales: {
              xAxes: [{
                display: true,
                gridLines: {
                  display: true
                },
                ticks: {
                 fontColor: '#000000'
               },
                scaleLabel: {
                  display: false,
                  labelString: 'Date',
                  fontColor: '#000000'
                }
              }],
              yAxes: [{
                display: true,
                gridLines: {
                  display: true
                },
                ticks: {
                 fontColor: '#000000',
                 callback: function(value){
                   return value + "%"
                 }
                },
                scaleLabel: {
                  display: false,
                  labelString: '',
                  fontColor: '#000000'
                }
              }]
            }
            //Scales Object Ends
          }
          //options object ends
        });
        //End of function
      }()

I am able to display a number in the Y axis as num % as you can see in the callback function I made on YAxes, but when I try in a tooltip is not the same result. 如我在YAxes上执行的回调函数中所见,我能够在Y轴上将num %显示为num % ,但是当我尝试使用工具提示时,结果并不相同。 I need to be able to display num % in a tooltip as well. 我还需要能够在工具提示中显示num % Any knowledge will be appreciated. 任何知识将不胜感激。 Thanks. 谢谢。

You can use a callback function for tooltips label, to display a % sign along with the data value, as such : 您可以对工具提示标签使用回调函数,以将%符号和数据值一起显示,如下所示:

tooltips: {
   callbacks: {
      label: function(t, d) {
         var xLabel = d.datasets[t.datasetIndex].label;
         var yLabel = t.yLabel;
         return xLabel + ': ' + yLabel + '%';
      }
   }
}

ᴡᴏʀᴋɪɴɢ ᴇxᴀᴍᴘʟᴇ ᴡᴏʀᴋɪɴɢᴇxᴀᴍᴘʟᴇ

 var ctx2 = document.getElementById('myChart2').getContext('2d'); var data = [{ date: '08-05-2017', run_rate: 50 }, { date: '08-06-2017', run_rate: 40 }, { date: '08-07-2017', run_rate: 30 }, { date: '08-08-2017', run_rate: 10 }, { date: '08-09-2017', run_rate: 6 }, { date: '08-10-2017', run_rate: 78 }, { date: '08-11-2017', run_rate: 32 }, { date: '08-12-2017', run_rate: 24 }]; var dates = data.map(function(obj) { return obj.date; }); var count = data.map(function(obj) { return obj.run_rate; }); var myChart = new Chart(ctx2, { type: 'line', data: { labels: dates, datasets: [{ label: 'Line', data: count, backgroundColor: "rgba(38, 82, 123, 0.5)" }] }, options: { legend: { display: false }, maintainAspectRatio: false, responsive: true, tooltips: { titleFontSize: 12, bodyFontSize: 12, callbacks: { label: function(t, d) { var xLabel = d.datasets[t.datasetIndex].label; var yLabel = t.yLabel; return xLabel + ': ' + yLabel + '%'; } } }, scales: { xAxes: [{ display: true, gridLines: { display: true }, ticks: { fontColor: '#000000' }, scaleLabel: { display: false, labelString: 'Date', fontColor: '#000000' } }], yAxes: [{ display: true, gridLines: { display: true }, ticks: { fontColor: '#000000', callback: function(value) { return value + "%" } }, scaleLabel: { display: false, labelString: '', fontColor: '#000000' } }] } //Scales Object Ends } //options object ends }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.6.0/Chart.min.js"></script> <canvas id="myChart2"></canvas> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM