ChartAmountPie.vue 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <!--
  2. * @Author: your name
  3. * @Date: 2021-10-26 09:15:22
  4. * @LastEditTime: 2021-10-26 10:54:54
  5. * @LastEditors: Please set LastEditors
  6. * @Description: 存栏结构图
  7. * @FilePath: \hyyfScreen\src\views\Production\board\ChartAmountPie.vue
  8. -->
  9. <template>
  10. <div class="chart-inventory-pie">
  11. <div id="chartAmountPie"></div>
  12. </div>
  13. </template>
  14. <script>
  15. export default {
  16. data() {
  17. return {
  18. myChart: null
  19. }
  20. },
  21. props: {
  22. data: {
  23. type: Array,
  24. required: true
  25. }
  26. },
  27. methods: {
  28. init() {
  29. const _this = this
  30. let total = this.data[1].stockQuantity + this.data[2].stockQuantity + this.data[3].stockQuantity + this.data[4].stockQuantity + this.data[5].stockQuantity
  31. let options = {
  32. // title: {
  33. // text: '存栏结构',
  34. // x: 20,
  35. // y: 20
  36. // },
  37. grid: {
  38. // x: 10,
  39. // y: 10
  40. },
  41. tooltip: {
  42. trigger: 'item',
  43. formatter: function ({name, value, percent}) {
  44. return name + ':' + value + '头&nbsp;&nbsp;&nbsp;&nbsp;' + percent + '%'
  45. }
  46. },
  47. legend: {
  48. top: '11%',
  49. left: '55%',
  50. orient: 'vertical',
  51. icon: "circle",
  52. selectedMode: false, // 取消右侧项选中
  53. itemGap: 20, // 各项间隔
  54. textStyle: {
  55. fontSize: 12,
  56. color: '#eff'
  57. },
  58. formatter: function (name) {
  59. var legendIndex = 0;
  60. var clientLabels = [
  61. { name: '母猪存栏', percent: parseFloat(_this.data[1].stockQuantity / total)*100 + '%', number: _this.data[1].stockQuantity },
  62. { name: '公猪存栏', percent: parseFloat(_this.data[2].stockQuantity / total)*100 + '%', number: _this.data[2].stockQuantity },
  63. { name: '哺乳仔猪存栏', percent: parseFloat(_this.data[3].stockQuantity / total)*100 + '%', number: _this.data[3].stockQuantity },
  64. { name: '保育猪存栏', percent: parseFloat(_this.data[4].stockQuantity / total)*100 + '%', number: _this.data[4].stockQuantity },
  65. { name: '育肥猪存栏', percent: parseFloat(_this.data[5].stockQuantity / total)*100 + '%', number: _this.data[5].stockQuantity },
  66. ]
  67. clientLabels.forEach(function (value, i) {
  68. if (value.name == name) {
  69. legendIndex = i;
  70. }
  71. });
  72. return name + " (" + clientLabels[legendIndex].percent + ', ' + clientLabels[legendIndex].number + '头' + ") ";
  73. }
  74. },
  75. graphic: [
  76. {
  77. type: 'text',
  78. left: '23%',
  79. top: '30%',
  80. style: {
  81. text: '总存栏',
  82. fontSize: 15,
  83. fontWeight: 700,
  84. fill: '#fff'
  85. }
  86. },
  87. {
  88. type: 'text',
  89. left: '23%',
  90. top: '40%',
  91. style: {
  92. text: total,
  93. fontSize: 24,
  94. fontWeight: 700,
  95. fill: '#fff'
  96. }
  97. }
  98. ],
  99. series: [
  100. {
  101. name: 'Access From',
  102. type: 'pie',
  103. radius: ['45%', '60%'], // 半径
  104. center: ['28%', '38%'],
  105. avoidLabelOverlap: false,
  106. label: {
  107. show: false,
  108. // position: 'center'
  109. },
  110. emphasis: {
  111. label: {
  112. show: false,
  113. fontSize: '40',
  114. fontWeight: 'bold'
  115. }
  116. },
  117. labelLine: {
  118. show: false
  119. },
  120. itemStyle: {
  121. normal: {
  122. color: function (colors) { // 颜色设置
  123. var colorList = ['#3aa0ff', '#4dcb73', '#fad337', '#f2637b', '#975fe4']
  124. return colorList[colors.dataIndex]
  125. },
  126. borderWidth: 3, // 设置各项空隙,白边
  127. borderColor: '#fff'
  128. }
  129. },
  130. data: [
  131. { value: this.data[1].stockQuantity, name: '母猪存栏' },
  132. { value: this.data[2].stockQuantity, name: '公猪存栏' },
  133. { value: this.data[3].stockQuantity, name: '哺乳仔猪存栏' },
  134. { value: this.data[4].stockQuantity, name: '保育猪存栏' },
  135. { value: this.data[5].stockQuantity, name: '育肥猪存栏' }
  136. ]
  137. }
  138. ]
  139. };
  140. this.myChart.setOption(options)
  141. }
  142. },
  143. mounted() {
  144. this.myChart = this.$echarts.init(document.getElementById('chartAmountPie'));
  145. this.init()
  146. console.log(this.data)
  147. },
  148. watch: {
  149. data(newValue) {
  150. console.log(newValue)
  151. this.init()
  152. }
  153. }
  154. }
  155. </script>
  156. <style scoped>
  157. .chart-inventory-pie {
  158. width: 100%;
  159. height: 300px;
  160. }
  161. #chartAmountPie {
  162. width: 100%;
  163. height: 100%;
  164. }
  165. </style>