如何从ChartJS收集的数据中获得百分比?

时间:2017-06-15 04:01:05

标签: php jquery chart.js

我遇到百分比问题。

这是我数据库中的数据:
Jobstreet - 2 (选择此作为职位申请来源的用户总数)
Facebook - 1 (选择此作为职位申请来源的用户总数)
Indeed.com - 1 (选择此作为职位申请来源的用户总数)

myGraph
我的代码似乎只是附加了百分号。

现在,我期待得到这样的结果:
Jobstreet - 50%| Facebook - 25%| Indeed.com - 25%

如果你总结一下,你就会得到100%。

我不知道该怎么做。请帮我解决我的问题。

这是我的代码:

$(document).ready(function(){
    $.ajax({
        url: "/public/ajax/generateChart/",
        method: "GET",
        success: function(data) {
            var source = [];
            var count = [];

            for(var i in data) {
                source.push(data[i].source);
                count.push(data[i].number);
            }

            var config = {
                type: 'doughnut',
                data: {
                    datasets: [{
                        data: count,
                        backgroundColor: [
                            'rgba(59, 89, 152, 1)',
                            'rgba(54, 162, 235, 1)',
                            'rgba(255, 206, 86, 1)',
                            'rgba(75, 192, 192, 1)',
                            'rgba(153, 102, 255,1)',
                            'rgba(255, 159, 64, 1)'
                        ]
                    }],
                    labels: source
                },
                options: {
                    responsive: true,
                    legend: {
                        position: 'bottom',
                    },
                    title: {
                        display: false,
                        text: 'Chart.js Doughnut Chart'
                    },
                    animation: {
                        animateScale: true,
                        animateRotate: true
                    },
                    tooltips: {
                        callbacks: {
                            label: function(tooltipItem, data) {
                                var dataset = data.datasets[tooltipItem.datasetIndex];
                                var total = dataset.data.reduce(function(previousValue, currentValue, currentIndex, array) {
                                    return previousValue + currentValue;
                                });
                                var currentValue = dataset.data[tooltipItem.index];
                                var precentage = Math.floor(((currentValue/total) * 100)+0.5);
                                return ": " + precentage + "%";
                            }
                        }
                    }
                }
            };

            var ctx = $('#chartCanvas');
            window.myDoughnut = new Chart(ctx, config);
        },
        error: function(data) {
            console.log(data);
        }
    });
});

2 个答案:

答案 0 :(得分:3)

您可以使用以下工具提示标签回调函数和图表插件来实现...

tooltips: {
   callbacks: {
      label: function(tooltipItem, data) {
         var label = data.labels[tooltipItem.index];
         var _data = data.datasets[tooltipItem.datasetIndex].data[tooltipItem.index];
         var data = /\./.test(_data) ? _data.toFixed(2) : _data;
         return label + ' - ' + data + '%';
      }
   }
}

ᴄʜᴀʀᴛᴘʟᴜɢɪɴ

plugins: [{
   beforeInit: function(c) {
      var data = c.data.datasets[0].data
      var data_sum = data.reduce((a, b) => a + b, 0);
      var each_one = 100 / data_sum;
      c.data.datasets[0].data = data.map(e => e * each_one);
   }
}]

添加插件,然后添加图表选项

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

$(document).ready(function() {
   $.ajax({
      url: "https://istack.000webhostapp.com/json/t5.json",
      method: "GET",
      success: function(data) {
         var source = [];
         var count = [];

         for (var i in data) {
            source.push(data[i].source);
            count.push(+data[i].number);
         }

         var config = {
            type: 'doughnut',
            data: {
               datasets: [{
                  data: count,
                  backgroundColor: [
                     'rgba(59, 89, 152, 1)',
                     'rgba(54, 162, 235, 1)',
                     'rgba(255, 206, 86, 1)',
                     'rgba(75, 192, 192, 1)',
                     'rgba(153, 102, 255,1)',
                     'rgba(255, 159, 64, 1)'
                  ]
               }],
               labels: source
            },
            options: {
               responsive: false,
               legend: {
                  position: 'bottom',
               },
               title: {
                  display: false,
                  text: 'Chart.js Doughnut Chart'
               },
               animation: {
                  animateScale: true,
                  animateRotate: true
               },
               tooltips: {
                  callbacks: {
                     label: function(tooltipItem, data) {
                        var label = data.labels[tooltipItem.index];
                        var _data = data.datasets[tooltipItem.datasetIndex].data[tooltipItem.index];
                        var data = /\./.test(_data) ? _data.toFixed(2) : _data;
                        return label + ' - ' + data + '%';
                     }
                  }
               }
            },
            plugins: [{
               beforeInit: function(c) {
                  var data = c.data.datasets[0].data
                  var data_sum = data.reduce((a, b) => a + b, 0);
                  var each_one = 100 / data_sum;
                  c.data.datasets[0].data = data.map(e => e * each_one);
               }
            }]
         }
         var ctx = $('#chartCanvas');
         window.myDoughnut = new Chart(ctx, config);
      },
      error: function(data) {
         console.log(data);
      }
   });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.6.0/Chart.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<canvas id="chartCanvas" height="200"></canvas>

答案 1 :(得分:0)

$(document).ready(function() {
   $.ajax({
      url: "https://istack.000webhostapp.com/json/t5.json",
      method: "GET",
      success: function(data) {
         var source = [];
         var count = [];

         for (var i in data) {
            source.push(data[i].source);
            count.push(+data[i].number);
         }

         var config = {
            type: 'doughnut',
            data: {
               datasets: [{
                  data: count,
                  backgroundColor: [
                     'rgba(59, 89, 152, 1)',
                     'rgba(54, 162, 235, 1)',
                     'rgba(255, 206, 86, 1)',
                     'rgba(75, 192, 192, 1)',
                     'rgba(153, 102, 255,1)',
                     'rgba(255, 159, 64, 1)'
                  ]
               }],
               labels: source
            },
            options: {
               responsive: false,
               legend: {
                  position: 'bottom',
               },
               title: {
                  display: false,
                  text: 'Chart.js Doughnut Chart'
               },
               animation: {
                  animateScale: true,
                  animateRotate: true
               },
               tooltips: {
                  callbacks: {
                     label: function(tooltipItem, data) {
                        var label = data.labels[tooltipItem.index];
                        var _data = data.datasets[tooltipItem.datasetIndex].data[tooltipItem.index];
                        var data = /\./.test(_data) ? _data.toFixed(2) : _data;
                        return label + ' - ' + data + '%';
                     }
                  }
               }
            },
            plugins: [{
               beforeInit: function(c) {
                  var data = c.data.datasets[0].data
                  var data_sum = data.reduce((a, b) => a + b, 0);
                  var each_one = 100 / data_sum;
                  c.data.datasets[0].data = data.map(e => e * each_one);
               }
            }]
         }
         var ctx = $('#chartCanvas');
         window.myDoughnut = new Chart(ctx, config);
      },
      error: function(data) {
         console.log(data);
      }
   });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.6.0/Chart.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<canvas id="chartCanvas" height="200"></canvas>