RingPie.vue 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <template>
  2. <div :id="id" style="height: 100%;width:100%;"></div>
  3. </template>
  4. <script>
  5. export default {
  6. data() {
  7. return {
  8. mychat: '',
  9. total: 0,
  10. heightlightIndex: 0,
  11. data: []
  12. }
  13. },
  14. props: {
  15. id: {
  16. type: String
  17. },
  18. pieData: {
  19. type: Object,
  20. default: function() {
  21. return {
  22. '1': 0, // 一年級
  23. '2': 0, // 二年級
  24. '3': 0,
  25. '4': 0,
  26. '5': 0
  27. }
  28. }
  29. },
  30. defaultActive: {
  31. type: Boolean,
  32. default: function() {
  33. return false
  34. }
  35. },
  36. singleColor: {
  37. type: Boolean,
  38. default: function() {
  39. return false
  40. }
  41. },
  42. title: {
  43. type: String
  44. },
  45. tooltip: {
  46. type: Boolean,
  47. default: function() {
  48. return false
  49. }
  50. }
  51. },
  52. mounted() {
  53. this.drawLine()
  54. },
  55. created() {
  56. },
  57. watch: {
  58. pieData() {
  59. this.drawLine()
  60. }
  61. },
  62. methods: {
  63. drawLine() {
  64. let _this = this
  65. this.data = []
  66. Object.keys(this.pieData).forEach(function(key) {
  67. _this.data.push({ 'name': key, 'value': _this.pieData[key] })
  68. })
  69. this.data.forEach(item => {
  70. _this.total += item.value
  71. })
  72. // 基于准备好的dom,初始化echarts实例
  73. this.myChart = this.$echarts.init(document.getElementById(this.id))
  74. let color = ['#FF6B6A', '#FF9FF4', '#48DBFC', '#1CD0A1', '#FDC958', '#FFAD76', '#b682f8'] // 大於2
  75. let color2 = ['#FF6B6A', '#48DBFC'] // 小於2
  76. if (Object.keys(this.pieData).length <= 2) {
  77. color = color2
  78. }
  79. this.myChart.setOption({
  80. title: {
  81. show: !!_this.title,
  82. text: _this.title ? _this.title : '',
  83. left: 'center',
  84. top: 'middle',
  85. textStyle: {
  86. color: '#fafafa',
  87. fontWeight: 100
  88. }
  89. },
  90. color: _this.singleColor ? 'rgba(228, 233, 220, 0.9)' : color,
  91. tooltip: {
  92. show: _this.tooltip,
  93. trigger: 'item'
  94. // formatter: function(p){
  95. // // 故意不填可用來觸發HighLight
  96. // }
  97. },
  98. series: [
  99. {
  100. type: 'pie',
  101. hoverOffset: 5,
  102. radius: _this.singleColor ? ['30%', '70%'] : ['50%', '70%'],
  103. avoidLabelOverlap: false,
  104. label: {
  105. normal: {
  106. show: false
  107. },
  108. emphasis: {
  109. show: false
  110. }
  111. },
  112. labelLine: {
  113. normal: {
  114. show: false
  115. }
  116. },
  117. data: _this.data,
  118. itemStyle: _this.singleColor ? {
  119. emphasis: {
  120. color: '#1CD0A1'
  121. }
  122. } : ''
  123. }
  124. ]
  125. })
  126. // mouseover觸發項
  127. this.myChart.on('mouseover', function(params) {
  128. if (_this.data[_this.heightlightIndex].name != params.name) {
  129. _this.myChart.dispatchAction({
  130. type: 'downplay',
  131. dataIndex: _this.heightlightIndex
  132. })
  133. }
  134. params.name = _this.resName(params.name)
  135. _this.$emit('highLightInfo', params)
  136. })
  137. if (this.defaultActive) {
  138. if (this.data.length == 0) return false
  139. // 預設先給第一筆初始值
  140. let now = this.data[0].value
  141. let name = this.resName(this.data[0].name)
  142. let params = { 'value': this.data[0].value, 'name': name, 'percent': Number((now / this.total) * 100).toFixed(2) }
  143. this.$emit('extraInfo', params)
  144. this.$emit('highLightInfo', params)
  145. // 預設第一個
  146. this.myChart.dispatchAction({
  147. type: 'highlight',
  148. dataIndex: _this.heightlightIndex
  149. })
  150. }
  151. },
  152. // 供外部呼叫用
  153. heightlight: function(dName) {
  154. let _this = this
  155. if (this.data[this.heightlightIndex].name != dName) {
  156. this.myChart.dispatchAction({
  157. type: 'downplay',
  158. dataIndex: _this.heightlightIndex
  159. })
  160. }
  161. this.myChart.dispatchAction({
  162. type: 'highlight',
  163. name: dName
  164. })
  165. let now = 0
  166. this.data.forEach(item => {
  167. if (item.name == dName) {
  168. now = item.value
  169. }
  170. })
  171. let params = { 'percent': Number((now / this.total) * 100).toFixed(2) }
  172. this.$emit('extraInfo', params)
  173. },
  174. downplay: function(dname) {
  175. this.myChart.dispatchAction({
  176. type: 'downplay',
  177. name: dname
  178. })
  179. },
  180. resName: function(name) {
  181. switch (name) {
  182. case 'Orther':
  183. return this.$t('classMgmt.Other')
  184. break
  185. case 'UnKnown':
  186. return this.$t('classMgmt.Other')
  187. break
  188. case 'smartClass':
  189. return this.$t('classMgmt.smartClass')
  190. break
  191. case 'sokratesClass':
  192. return this.$t('classMgmt.sokratesClass')
  193. break
  194. default:
  195. return name
  196. break
  197. }
  198. }
  199. }
  200. }
  201. </script>
  202. <style>
  203. </style>