ChartPie.vue 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <template>
  2. <div id="chartPie" style="width: 100%; height: 100%;"></div>
  3. </template>
  4. <script>
  5. export default {
  6. name: "ChartPie",
  7. data() {
  8. return {
  9. myChart: null
  10. }
  11. },
  12. props: {
  13. data: {
  14. type: Array,
  15. default: () => []
  16. }
  17. },
  18. watch: {
  19. data: {
  20. handler(newVal) {
  21. if(newVal) {
  22. this.myChart.clear();
  23. this.init();
  24. }
  25. },
  26. deep: true
  27. }
  28. },
  29. methods: {
  30. init() {
  31. let arr = [];
  32. let num = 0;
  33. this.data.forEach(item => {
  34. num+=item.value
  35. })
  36. this.data.forEach(item => {
  37. arr.push({ name: item.name, percent: (parseFloat(item.value) / num * 100).toFixed(2) + '%', number: item.value})
  38. })
  39. let options = {
  40. grid: {
  41. x: 100
  42. },
  43. tooltip: {
  44. trigger: 'item',
  45. formatter: function ({name, value, percent}) {
  46. return name + ':' + value + '条&nbsp;&nbsp;&nbsp;&nbsp;' + percent + '%'
  47. }
  48. },
  49. legend: {
  50. top: '30%',
  51. left: '55%',
  52. orient: 'vertical',
  53. icon: "circle",
  54. selectedMode: false, // 取消右侧项选中
  55. itemGap: 30, // 各项间隔
  56. textStyle: {
  57. fontSize: 13,
  58. color: '#666'
  59. },
  60. formatter: function (name) {
  61. var legendIndex = 0;
  62. // var clientLabels = [
  63. // { name: '环境报警', percent: parseFloat(0.16)*100 + '%', number: 2 },
  64. // { name: '断电报警', percent: parseFloat(0.16)*100 + '%', number: 2 },
  65. // { name: '人员违规', percent: parseFloat(0.16)*100 + '%', number: 2 },
  66. // { name: '环境监测', percent: parseFloat(0.16)*100 + '%', number: 2 },
  67. // { name: '污水排放', percent: parseFloat(0.16)*100 + '%', number: 2 },
  68. // { name: '设备异常', percent: parseFloat(0.16)*100 + '%', number: 2 },
  69. // ]
  70. var clientLabels = arr
  71. clientLabels.forEach(function (value, i) {
  72. if (value.name == name) {
  73. legendIndex = i;
  74. }
  75. });
  76. return name + " (" + clientLabels[legendIndex].percent + ', ' + clientLabels[legendIndex].number + '条' + ") ";
  77. }
  78. },
  79. graphic: [
  80. {
  81. type: 'text',
  82. left: '22%',
  83. top: '43%',
  84. style: {
  85. text: '报警总量',
  86. fontSize: 15,
  87. fill: 'rgb(192, 188, 189)'
  88. }
  89. },
  90. {
  91. type: 'text',
  92. left: '22%',
  93. top: '53%',
  94. style: {
  95. text: num + '条',
  96. fontSize: 22,
  97. fontWeight: 700,
  98. fill: 'rgb(192, 188, 189)'
  99. }
  100. }
  101. ],
  102. series: [
  103. {
  104. name: 'Access From',
  105. type: 'pie',
  106. radius: ['60%', '80%'], // 半径
  107. center: ['30%', '55%'],
  108. avoidLabelOverlap: false,
  109. label: {
  110. show: false,
  111. // position: 'center'
  112. },
  113. emphasis: {
  114. label: {
  115. show: false,
  116. fontSize: '40',
  117. fontWeight: 'bold'
  118. }
  119. },
  120. labelLine: {
  121. show: false
  122. },
  123. itemStyle: {
  124. normal: {
  125. color: function (colors) { // 颜色设置
  126. var colorList = ['#3aa0ff', '#4dcb73', '#fad337', '#f2637b', '#975fe4']
  127. return colorList[colors.dataIndex]
  128. },
  129. borderWidth: 5, // 设置各项空隙
  130. borderColor: '#fff'
  131. }
  132. },
  133. data: this.data
  134. }
  135. ]
  136. };
  137. this.myChart.setOption(options)
  138. }
  139. },
  140. mounted() {
  141. this.myChart = this.$echarts.init(document.getElementById('chartPie'));
  142. this.init()
  143. let that = this;
  144. window.addEventListener("resize", function () {
  145. that.myChart.resize()
  146. })
  147. }
  148. }
  149. </script>
  150. <style scoped>
  151. </style>