pie2.vue 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <template>
  2. <div class="data_body">
  3. <div style="width: 100%; height: 100%">
  4. <div id="charts_canvas" class="echart" style="width: 100%; height: 100%"></div>
  5. </div>
  6. </div>
  7. </template>
  8. <script>
  9. export default {
  10. props: {
  11. loginArray: {
  12. type: Array
  13. },
  14. },
  15. data() {
  16. return {
  17. chartObj: null,
  18. ooption: {
  19. xdata: [],
  20. type: [],
  21. },
  22. option: {
  23. tooltip: {
  24. trigger: 'item'
  25. },
  26. series: [
  27. {
  28. type: 'pie',
  29. radius: ['80%', '100%'],
  30. avoidLabelOverlap: false,
  31. itemStyle: {
  32. borderRadius: 10,
  33. borderColor: '#fff',
  34. borderWidth: 2
  35. },
  36. label: {
  37. show: true,
  38. position: 'center',
  39. // formatter: '{b}\n{c}'
  40. fontSize: 20,
  41. rich: {
  42. a: {
  43. fontSize: 30,
  44. },
  45. b: {
  46. lineHeight: 20,
  47. fontSize: 14,
  48. color: '#00000099'
  49. }
  50. }
  51. // formatter: function (params) {
  52. // var sum = 0;
  53. // for (var i = 0; i < params.data.length; i++) {
  54. // sum += params.data[i].value;
  55. // }
  56. // return sum + '\n表单总数';
  57. // }
  58. },
  59. emphasis: {
  60. label: {
  61. show: true,
  62. fontSize: 20,
  63. rich: {
  64. a: {
  65. fontSize: 30,
  66. },
  67. b: {
  68. lineHeight: 20,
  69. fontSize: 14,
  70. color: '#00000099'
  71. }
  72. }
  73. // formatter: '{c}'
  74. }
  75. },
  76. labelLine: {
  77. show: false
  78. },
  79. hoverOffset: 0, // 设置鼠标悬停时不放大
  80. data: [
  81. { value: 10, name: '未完成', itemStyle: { normal: { color: '#3673e8' } } },
  82. { value: 8, name: '进行中', itemStyle: { normal: { color: '#61b8ff' } } },
  83. { value: 6, name: '已完成', itemStyle: { normal: { color: '#96d8a8' } } },
  84. { value: 7, name: '逾期', itemStyle: { normal: { color: '#f5b763' } } },
  85. ]
  86. }
  87. ]
  88. },
  89. };
  90. },
  91. methods: {
  92. setChart(array) {
  93. // 雷达图显示的标签
  94. let newPromise = new Promise((resolve) => {
  95. resolve();
  96. });
  97. //然后异步执行echarts的初始化函数
  98. newPromise.then(() => {
  99. const chartObj = this.$echarts.init(
  100. this.$el.querySelector("#charts_canvas")
  101. );
  102. // this.option.series[0].data = array;
  103. let _this = this
  104. _this.option.series[0].label.emphasis = _this.option.series[0].label.formatter = function (params) {
  105. console.log(params);
  106. var sum = 0;
  107. for (var i = 0; i < _this.option.series[0].data.length; i++) {
  108. sum += _this.option.series[0].data[i].value;
  109. }
  110. return '{a|' + sum + '}\n{b|表单总数}';
  111. }
  112. // 初始化雷达图
  113. this.chartObj = chartObj;
  114. this.chartObj.setOption(this.option);
  115. });
  116. },
  117. setJson(array) {
  118. this.ooption = {
  119. xdata: [],
  120. sdata: [],
  121. max: 0
  122. }
  123. if (!this.chartObj) {
  124. this.setChart(array);
  125. } else {
  126. this.option.series[0].data = array;
  127. this.chartObj.setOption(this.option);
  128. }
  129. }
  130. },
  131. watch: {
  132. loginArray: {
  133. immediate: true,
  134. deep: true,
  135. handler(newValue, oldValue) {
  136. this.setJson(newValue)
  137. this.$forceUpdate();
  138. },
  139. },
  140. },
  141. mounted() {
  142. this.setJson(this.loginArray)
  143. var _this = this;
  144. window.addEventListener("resize", () => {
  145. if (_this.chartObj) {
  146. _this.chartObj.resize();
  147. }
  148. });
  149. },
  150. };
  151. </script>
  152. <style scoped>
  153. .data_body {
  154. height: 100%;
  155. /* display: flex; */
  156. position: relative;
  157. border-radius: 5px;
  158. /* border: 1px solid #eee; */
  159. margin: 0 auto;
  160. box-sizing: border-box;
  161. padding: 0;
  162. width: 100%;
  163. /* background: #fff; */
  164. }
  165. </style>