ringPie.vue 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. }
  12. },
  13. props:{
  14. id:{
  15. type: String
  16. },
  17. pieData:{
  18. type: Array,
  19. default: function(){
  20. return [
  21. {value:335, name:'新增檔案'},
  22. {value:310, name:'書面問答'},
  23. {value:234, name:'匯入.pptx'},
  24. {value:135, name:'PowerClick'},
  25. {value:1548, name:'開啟.hte'},
  26. {value:123, name:'其他'},
  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. this.pieData.forEach(item => {
  57. this.total += item.value
  58. });
  59. },
  60. methods:{
  61. drawLine(){
  62. let _this = this
  63. // 基于准备好的dom,初始化echarts实例
  64. this.myChart = this.$echarts.init(document.getElementById(this.id));
  65. this.myChart.setOption({
  66. title:{
  67. show: _this.title ? true : false,
  68. text: _this.title ? _this.title : '',
  69. left: 'center',
  70. top: 'middle',
  71. textStyle:{
  72. color: '#fafafa',
  73. fontWeight: 100
  74. }
  75. },
  76. color: _this.singleColor ? 'rgba(228, 233, 220, 0.9)' : ['#FF6B6A', '#FF9FF4', '#48DBFC', '#1CD0A1', '#FDC958', '#FFAD76'],
  77. tooltip: {
  78. show: _this.tooltip,
  79. trigger: 'item',
  80. // formatter: function(p){
  81. // // 故意不填可用來觸發HighLight
  82. // }
  83. },
  84. series: [
  85. {
  86. type:'pie',
  87. hoverOffset: 5,
  88. radius: _this.singleColor ? ['30%', '70%'] : ['50%', '70%'],
  89. avoidLabelOverlap: false,
  90. label: {
  91. normal: {
  92. show: false,
  93. },
  94. emphasis: {
  95. show: false,
  96. }
  97. },
  98. labelLine: {
  99. normal: {
  100. show: false
  101. }
  102. },
  103. data: _this.pieData,
  104. itemStyle: _this.singleColor ? {
  105. emphasis: {
  106. color: '#1CD0A1',
  107. }
  108. } : ''
  109. }
  110. ]
  111. });
  112. // mouseover觸發項
  113. this.myChart.on('mouseover', function (params) {
  114. if(_this.pieData[_this.heightlightIndex].name != params.name){
  115. _this.myChart.dispatchAction({
  116. type: 'downplay',
  117. dataIndex: _this.heightlightIndex,
  118. });
  119. }
  120. _this.$emit('highLightInfo', params);
  121. });
  122. if(this.defaultActive){
  123. // 預設先給第一筆初始值
  124. let now = this.pieData[0].value;
  125. let params = {'value': this.pieData[0].value, 'name': this.pieData[0].name,'percent': Number((now/this.total)*100).toFixed(2)}
  126. this.$emit('extraInfo', params);
  127. this.$emit('highLightInfo', params);
  128. // 預設第一個
  129. this.myChart.dispatchAction({
  130. type: 'highlight',
  131. dataIndex: _this.heightlightIndex,
  132. });
  133. }
  134. },
  135. // 供外部呼叫用
  136. heightlight: function(dName){
  137. let _this = this;
  138. if(this.pieData[this.heightlightIndex].name != dName){
  139. this.myChart.dispatchAction({
  140. type: 'downplay',
  141. dataIndex: _this.heightlightIndex,
  142. });
  143. }
  144. this.myChart.dispatchAction({
  145. type: 'highlight',
  146. name: dName
  147. });
  148. let now = 0;
  149. this.pieData.forEach(item => {
  150. if(item.name == dName){
  151. now = item.value
  152. }
  153. });
  154. let params = {'percent': Number((now/this.total)*100).toFixed(2)}
  155. this.$emit('extraInfo', params);
  156. },
  157. downplay: function(dname){
  158. this.myChart.dispatchAction({
  159. type: 'downplay',
  160. name: dname
  161. });
  162. }
  163. }
  164. }
  165. </script>
  166. <style>
  167. </style>