ring.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /**
  2. * 环形图。
  3. * 作者:tmzdy
  4. * url:https://jx2d.cn
  5. */
  6. let ring = function (render, config={}) {
  7. let defauletcfg = {
  8. tooltip: {
  9. trigger: 'item'
  10. },
  11. color:["#E91E63","#2196F3","#311B92","#FDD835","#BCAAA4"],
  12. legend: {
  13. top: '5%',
  14. left: 'center'
  15. },
  16. series: [{
  17. name: 'Access From',
  18. type: 'pie',
  19. radius: ['20%', '35%'],
  20. label: {
  21. show: false,
  22. position: 'center'
  23. },
  24. emphasis: {
  25. label: {
  26. show: true,
  27. fontSize: '40',
  28. fontWeight: 'bold'
  29. }
  30. },
  31. labelLine: {
  32. show: false
  33. },
  34. data: [{
  35. value: 1048,
  36. name: 'Search Engine'
  37. },
  38. {
  39. value: 735,
  40. name: 'Direct'
  41. },
  42. {
  43. value: 580,
  44. name: 'Email'
  45. },
  46. {
  47. value: 484,
  48. name: 'Union Ads'
  49. },
  50. {
  51. value: 300,
  52. name: 'Video Ads'
  53. }
  54. ]
  55. }]
  56. }
  57. defauletcfg = {...defauletcfg,...config}
  58. const [w, h] = render.area;
  59. //最大圆环厚度。等于圆。
  60. let lineMaxHeight = w / 2;
  61. let lineStoreWidth = (parseFloat(defauletcfg.series[0].radius[1]) - parseFloat(defauletcfg.series[0].radius[0]))/100;
  62. lineStoreWidth =Math.floor( lineStoreWidth*lineMaxHeight);
  63. let r = parseFloat(defauletcfg.series[0].radius[1])/100 * lineMaxHeight;
  64. let data = defauletcfg.series[0].data;
  65. let maxValue = data.map(el => parseInt(el.value));
  66. //总数量。
  67. maxValue = maxValue.reduce((p,c)=>p+c);
  68. //计算各自的比例数量。保留两位小数点。
  69. let rang = Math.PI *2;
  70. //每一个元素的圆弧的长度。
  71. let rangArray = data.map(el=>{
  72. let num = parseInt(el.value)/maxValue;
  73. return rang*num
  74. })
  75. let dataBlv = data.map((el,index) => {
  76. let st = index;
  77. let num = parseFloat(el.value)/maxValue;
  78. let blv = parseFloat(num.toFixed(2));
  79. let startAngle = 0;
  80. let endAngle = 0;
  81. if(st==0){
  82. startAngle = 0;
  83. endAngle = rangArray[st]
  84. }else{
  85. let s = rangArray.slice(0,st)
  86. startAngle = s.reduce((p,c)=>p+c);
  87. endAngle = startAngle+rangArray[st]
  88. }
  89. return {...el,
  90. blv:blv,
  91. startAngle:startAngle,
  92. endAngle:endAngle
  93. };
  94. });
  95. let xf = [];
  96. dataBlv.forEach((el,index)=>{
  97. let cfg = {
  98. name: 'arc',
  99. animationCurve: 'liner',
  100. hover: false,
  101. drag: false,
  102. shape: {
  103. rx: w/2,
  104. ry: h/2,
  105. r: r,
  106. startAngle: el.startAngle,
  107. endAngle: el.endAngle,
  108. },
  109. style: {
  110. stroke: defauletcfg.color[index],
  111. lineWidth: lineStoreWidth,
  112. shadowBlur: 0,
  113. rotate: 0,
  114. shadowColor: '#66eece',
  115. hoverCursor: 'pointer',
  116. },
  117. };
  118. xf.push(cfg)
  119. })
  120. return xf;
  121. }
  122. export default ring;