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

前端之家收集整理的这篇文章主要介绍了javascript – Chart.js 2.0甜甜圈工具提示百分比前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我已经使用chart.js 1.0,并且我的甜甜圈图表工具提示基于数据分割的数据显示百分比,但是我无法用图表2.0复制这个数据.

搜索过高低,还没找到工作解决方案.我知道这将是选择,但我尝试过的一切都使得馅饼功能失调.

  1. <html>
  2.  
  3. <head>
  4. <title>Doughnut Chart</title>
  5. <script src="../dist/Chart.bundle.js"></script>
  6. <script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
  7. <style>
  8. canvas {
  9. -moz-user-select: none;
  10. -webkit-user-select: none;
  11. -ms-user-select: none;
  12. }
  13. </style>
  14. </head>
  15.  
  16. <body>
  17. <div id="canvas-holder" style="width:75%">
  18. <canvas id="chart-area" />
  19. </div>
  20. <script>
  21. var randomScalingFactor = function() {
  22. return Math.round(Math.random() * 100);
  23. };
  24. var randomColorFactor = function() {
  25. return Math.round(Math.random() * 255);
  26. };
  27. var randomColor = function(opacity) {
  28. return 'rgba(' + randomColorFactor() + ',' + randomColorFactor() + ',' + (opacity || '.3') + ')';
  29. };
  30.  
  31. var config = {
  32. type: 'doughnut',data: {
  33. datasets: [{
  34. data: [
  35. 486.5,501.5,139.3,162,263.7,],backgroundColor: [
  36. "#F7464A","#46BFBD","#FDB45C","#949FB1","#4D5360",label: 'Expenditures'
  37. }],labels: [
  38. "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"
  39. ]
  40. },options: {
  41. responsive: true,legend: {
  42. position: 'bottom',},title: {
  43. display: false,text: 'Chart.js Doughnut Chart'
  44. },animation: {
  45. animateScale: true,animateRotate: true
  46. }
  47.  
  48. }
  49. };
  50.  
  51. window.onload = function() {
  52. var ctx = document.getElementById("chart-area").getContext("2d");
  53. window.myDoughnut = new Chart(ctx,config);{
  54.  
  55. }
  56. };
  57.  
  58.  
  59. </script>
  60. </body>
  61.  
  62. </html>

解决方法

在选项中,您可以传入一个工具提示对象(更多可以在 chartjs docs读取)

一个工具提示字段,以获取您想要的结果,是一个带有标签字段的回调对象.标签将是一个功能,它接收您已经悬停的工具提示项目以及构成图形的数据.从这个函数返回一个字符串,你想要进入工具提示.

这是一个这样的例子

  1. tooltips: {
  2. callbacks: {
  3. label: function(tooltipItem,data) {
  4. //get the concerned dataset
  5. var dataset = data.datasets[tooltipItem.datasetIndex];
  6. //calculate the total of this data set
  7. var total = dataset.data.reduce(function(prevIoUsValue,currentValue,currentIndex,array) {
  8. return prevIoUsValue + currentValue;
  9. });
  10. //get the current items value
  11. var currentValue = dataset.data[tooltipItem.index];
  12. //calculate the precentage based on the total and current item,also this does a rough rounding to give a whole number
  13. var precentage = Math.floor(((currentValue/total) * 100)+0.5);
  14.  
  15. return precentage + "%";
  16. }
  17. }
  18. }

并提供您提供的数据的完整示例

fiddle

  1. var randomScalingFactor = function() {
  2. return Math.round(Math.random() * 100);
  3. };
  4. var randomColorFactor = function() {
  5. return Math.round(Math.random() * 255);
  6. };
  7. var randomColor = function(opacity) {
  8. return 'rgba(' + randomColorFactor() + ',' + (opacity || '.3') + ')';
  9. };
  10.  
  11. var config = {
  12. type: 'doughnut',data: {
  13. datasets: [{
  14. data: [
  15. 486.5,backgroundColor: [
  16. "#F7464A",label: 'Expenditures'
  17. }],labels: [
  18. "Hospitals: $486.5 billion","Other Expenditures: $263.7 billion"
  19. ]
  20. },options: {
  21. responsive: true,legend: {
  22. position: 'bottom',title: {
  23. display: false,text: 'Chart.js Doughnut Chart'
  24. },animation: {
  25. animateScale: true,animateRotate: true
  26. },tooltips: {
  27. callbacks: {
  28. label: function(tooltipItem,data) {
  29. var dataset = data.datasets[tooltipItem.datasetIndex];
  30. var total = dataset.data.reduce(function(prevIoUsValue,array) {
  31. return prevIoUsValue + currentValue;
  32. });
  33. var currentValue = dataset.data[tooltipItem.index];
  34. var precentage = Math.floor(((currentValue/total) * 100)+0.5);
  35. return precentage + "%";
  36. }
  37. }
  38. }
  39. }
  40. };
  41.  
  42.  
  43. var ctx = document.getElementById("chart-area").getContext("2d");
  44. window.myDoughnut = new Chart(ctx,config); {
  45.  
  46. }
  1. <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.1.3/Chart.bundle.js"></script>
  2. <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
  3. <div id="canvas-holder" style="width:75%">
  4. <canvas id="chart-area" />
  5. </div>

猜你在找的JavaScript相关文章