<bdo id='TMYXa'></bdo><ul id='TMYXa'></ul>
    <legend id='TMYXa'><style id='TMYXa'><dir id='TMYXa'><q id='TMYXa'></q></dir></style></legend>
  1. <i id='TMYXa'><tr id='TMYXa'><dt id='TMYXa'><q id='TMYXa'><span id='TMYXa'><b id='TMYXa'><form id='TMYXa'><ins id='TMYXa'></ins><ul id='TMYXa'></ul><sub id='TMYXa'></sub></form><legend id='TMYXa'></legend><bdo id='TMYXa'><pre id='TMYXa'><center id='TMYXa'></center></pre></bdo></b><th id='TMYXa'></th></span></q></dt></tr></i><div id='TMYXa'><tfoot id='TMYXa'></tfoot><dl id='TMYXa'><fieldset id='TMYXa'></fieldset></dl></div>

      <tfoot id='TMYXa'></tfoot>

      <small id='TMYXa'></small><noframes id='TMYXa'>

      Chart.js 2.0 甜甜圈工具提示百分比

      时间:2023-11-01
    1. <tfoot id='BtzRM'></tfoot>

      <small id='BtzRM'></small><noframes id='BtzRM'>

          <tbody id='BtzRM'></tbody>

        <legend id='BtzRM'><style id='BtzRM'><dir id='BtzRM'><q id='BtzRM'></q></dir></style></legend>
          <bdo id='BtzRM'></bdo><ul id='BtzRM'></ul>

                <i id='BtzRM'><tr id='BtzRM'><dt id='BtzRM'><q id='BtzRM'><span id='BtzRM'><b id='BtzRM'><form id='BtzRM'><ins id='BtzRM'></ins><ul id='BtzRM'></ul><sub id='BtzRM'></sub></form><legend id='BtzRM'></legend><bdo id='BtzRM'><pre id='BtzRM'><center id='BtzRM'></center></pre></bdo></b><th id='BtzRM'></th></span></q></dt></tr></i><div id='BtzRM'><tfoot id='BtzRM'></tfoot><dl id='BtzRM'><fieldset id='BtzRM'></fieldset></dl></div>
                本文介绍了Chart.js 2.0 甜甜圈工具提示百分比的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我使用过 chart.js 1.0,并且我的圆环图工具提示显示基于数据除以数据集的百分比,但我无法使用 chart 2.0 复制这一点.

                I have worked with chart.js 1.0 and had my doughnut chart tooltips displaying percentages based on data divided by dataset, but I'm unable to replicate this with chart 2.0.

                我搜索了高低,但没有找到可行的解决方案.我知道它会受到选择,但我所尝试的一切都使馅饼功能失调.

                I have searched high and low and have not found a working solution. I know that it will go under options but everything I've tried has made the pie dysfunctional at best.

                <html>
                
                <head>
                    <title>Doughnut Chart</title>
                    <script src="../dist/Chart.bundle.js"></script>
                    <script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
                    <style>
                    canvas {
                        -moz-user-select: none;
                        -webkit-user-select: none;
                        -ms-user-select: none;
                    }
                    </style>
                </head>
                
                <body>
                    <div id="canvas-holder" style="width:75%">
                        <canvas id="chart-area" />
                    </div>
                    <script>
                    var randomScalingFactor = function() {
                        return Math.round(Math.random() * 100);
                    };
                    var randomColorFactor = function() {
                        return Math.round(Math.random() * 255);
                    };
                    var randomColor = function(opacity) {
                        return 'rgba(' + randomColorFactor() + ',' + randomColorFactor() + ',' + randomColorFactor() + ',' + (opacity || '.3') + ')';
                    };
                
                    var config = {
                        type: 'doughnut',
                        data: {
                            datasets: [{
                                data: [
                                    486.5,
                                    501.5,
                                    139.3,
                                    162,
                                    263.7,
                                ],
                                backgroundColor: [
                                    "#F7464A",
                                    "#46BFBD",
                                    "#FDB45C",
                                    "#949FB1",
                                    "#4D5360",
                                ],
                                label: 'Expenditures'
                            }],
                            labels: [
                                "Hospitals: $486.5 billion",
                                "Physicians & Professional Services: $501.5 billion",
                                "Long Term Care: $139.3 billion",
                                "Prescription Drugs: $162 billion",
                                "Other Expenditures: $263.7 billion"
                            ]
                        },
                        options: {
                            responsive: true,
                            legend: {
                                position: 'bottom',
                            },
                            title: {
                                display: false,
                                text: 'Chart.js Doughnut Chart'
                            },
                            animation: {
                                animateScale: true,
                                animateRotate: true
                            }
                
                        }
                    };
                
                    window.onload = function() {
                        var ctx = document.getElementById("chart-area").getContext("2d");
                        window.myDoughnut = new Chart(ctx, config);{
                
                        }
                    };
                
                
                    </script>
                </body>
                
                </html>
                

                推荐答案

                更新: 下面的答案显示了基于总数据的百分比,但 @William Surya Permana 有一个根据显示的数据更新的优秀答案 https://stackoverflow.com/a/49717859/2737978

                Update: The below answer shows a percentage based on total data but @William Surya Permana has an excellent answer that updates based on the shown data https://stackoverflow.com/a/49717859/2737978

                options 中你可以传入一个 tooltips 对象(更多内容可以在 chartjs 文档)

                In options you can pass in a tooltips object (more can be read at the chartjs docs)

                tooltips 的一个字段,为了得到你想要的结果,是一个带有 label 字段的 callbacks 对象.label 将是一个函数,它接收您悬停的工具提示项和构成图表的数据.只需从这个函数返回一个字符串,你想进入工具提示.

                A field of tooltips, to get the result you want, is a callbacks object with a label field. label will be a function that takes in the tooltip item which you have hovered over and the data which makes up your graph. Just return a string, that you want to go in the tooltip, from this function.

                这是一个看起来像这样的示例

                Here is an example of what this can look like

                tooltips: {
                  callbacks: {
                    label: function(tooltipItem, data) {
                      //get the concerned dataset
                      var dataset = data.datasets[tooltipItem.datasetIndex];
                      //calculate the total of this data set
                      var total = dataset.data.reduce(function(previousValue, currentValue, currentIndex, array) {
                        return previousValue + currentValue;
                      });
                      //get the current items value
                      var currentValue = dataset.data[tooltipItem.index];
                      //calculate the precentage based on the total and current item, also this does a rough rounding to give a whole number
                      var percentage = Math.floor(((currentValue/total) * 100)+0.5);
                
                      return percentage + "%";
                    }
                  }
                } 
                

                以及您提供的数据的完整示例

                and a full example with the data you provided

                小提琴

                var randomScalingFactor = function() {
                  return Math.round(Math.random() * 100);
                };
                var randomColorFactor = function() {
                  return Math.round(Math.random() * 255);
                };
                var randomColor = function(opacity) {
                  return 'rgba(' + randomColorFactor() + ',' + randomColorFactor() + ',' + randomColorFactor() + ',' + (opacity || '.3') + ')';
                };
                
                var config = {
                  type: 'doughnut',
                  data: {
                    datasets: [{
                      data: [
                        486.5,
                        501.5,
                        139.3,
                        162,
                        263.7,
                      ],
                      backgroundColor: [
                        "#F7464A",
                        "#46BFBD",
                        "#FDB45C",
                        "#949FB1",
                        "#4D5360",
                      ],
                      label: 'Expenditures'
                    }],
                    labels: [
                      "Hospitals: $486.5 billion",
                      "Physicians & Professional Services: $501.5 billion",
                      "Long Term Care: $139.3 billion",
                      "Prescription Drugs: $162 billion",
                      "Other Expenditures: $263.7 billion"
                    ]
                  },
                  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 percentage = Math.floor(((currentValue/total) * 100)+0.5);         
                          return percentage + "%";
                        }
                      }
                    }
                  }
                };
                
                
                var ctx = document.getElementById("chart-area").getContext("2d");
                window.myDoughnut = new Chart(ctx, config); {
                
                }

                <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.1.3/Chart.bundle.js"></script>
                <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                <div id="canvas-holder" style="width:75%">
                  <canvas id="chart-area" />
                </div>

                这篇关于Chart.js 2.0 甜甜圈工具提示百分比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:ChartJS 3 不显示图例 下一篇:Charts.js 使用货币和千位分隔符格式化 Y 轴

                相关文章

              1. <tfoot id='XjXIQ'></tfoot>
                    <legend id='XjXIQ'><style id='XjXIQ'><dir id='XjXIQ'><q id='XjXIQ'></q></dir></style></legend>

                  1. <small id='XjXIQ'></small><noframes id='XjXIQ'>

                    <i id='XjXIQ'><tr id='XjXIQ'><dt id='XjXIQ'><q id='XjXIQ'><span id='XjXIQ'><b id='XjXIQ'><form id='XjXIQ'><ins id='XjXIQ'></ins><ul id='XjXIQ'></ul><sub id='XjXIQ'></sub></form><legend id='XjXIQ'></legend><bdo id='XjXIQ'><pre id='XjXIQ'><center id='XjXIQ'></center></pre></bdo></b><th id='XjXIQ'></th></span></q></dt></tr></i><div id='XjXIQ'><tfoot id='XjXIQ'></tfoot><dl id='XjXIQ'><fieldset id='XjXIQ'></fieldset></dl></div>
                      <bdo id='XjXIQ'></bdo><ul id='XjXIQ'></ul>