doughnut.vue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <template>
  2. <div style="width:100%;height:100%;" :id="id">
  3. </div>
  4. </template>
  5. <script>
  6. import echarts from "echarts"
  7. export default {
  8. name: "doughnut",
  9. props: {
  10. id: {
  11. type: String,
  12. default: function createRandomId() {
  13. return (Math.random() * 10000000).toString(16).substr(0, 4) + '-' + (new Date()).getTime() + '-' + Math.random().toString().substr(2, 5);
  14. }
  15. },
  16. renderData: {
  17. type: Array,
  18. default: function() {
  19. return []
  20. }
  21. },
  22. type: {},
  23. name: "",
  24. sum: ""
  25. },
  26. data() {
  27. return {
  28. myCharts: null
  29. }
  30. },
  31. created() {},
  32. mounted() {
  33. this.drawDoughnut()
  34. },
  35. methods: {
  36. drawDoughnut() {
  37. console.log(this.renderData, "renderData")
  38. let options = {
  39. tooltip: {
  40. trigger: 'item',
  41. formatter: "{b}: ({d}%)"
  42. },
  43. legend: {
  44. show: this.type != 'left' ? true : false,
  45. orient: 'vertical',
  46. right: 10,
  47. top: 30,
  48. bottom: 20,
  49. data: this.renderData.map(item => {
  50. return item.name
  51. })
  52. },
  53. series: [{
  54. name: '',
  55. type: 'pie',
  56. radius: ['40%', '60%'],
  57. center: this.type != 'left' ? ['90px','70px'] : ['150px', '150px'],
  58. itemStyle: {
  59. normal: {
  60. label: {
  61. show: this.type != 'left' ? false : true,
  62. textStyle: {
  63. color: '#3c4858',
  64. fontSize: "12"
  65. },
  66. formatter: function(val) { //让series 中的文字进行换行
  67. return val.name.split("-")[0];
  68. }
  69. },
  70. title: {
  71. text: 'aaaa'
  72. },
  73. labelLine: {
  74. show: this.type != 'left' ? false : true,
  75. lineStyle: {
  76. // color: '#3c4858'
  77. }
  78. }
  79. },
  80. emphasis: {
  81. shadowBlur: 5,
  82. shadowOffsetX: 0,
  83. shadowColor: 'rgba(0, 0, 0, 0.5)',
  84. textColor: '#000'
  85. }
  86. },
  87. data: this.renderData
  88. }],
  89. color: ['#9DA2D6', '#FBACA1', '#FFE37F']
  90. }
  91. // if(this.type == 'type'){
  92. options.graphic = {
  93. type: 'text',
  94. left: this.type != 'left' ? '70px' : '125px',
  95. top: this.type != 'left' ? '65px' : '145px',
  96. style: {
  97. text: `总数:${this.sum|| ''}`,
  98. textAlign: 'center',
  99. fill: '#000',
  100. width: 30,
  101. height: 30
  102. }
  103. }
  104. // }
  105. this.myCharts = echarts.init(document.getElementById(this.id))
  106. this.myCharts.setOption(options)
  107. },
  108. delName(val){
  109. if(!!val && val.length > 3){
  110. return val.substring(0,3) + '...'
  111. }else{
  112. return ''
  113. }
  114. }
  115. },
  116. watch: {
  117. renderData: {
  118. deep: true,
  119. handler: function() {
  120. this.drawDoughnut()
  121. }
  122. }
  123. }
  124. }
  125. </script>
  126. <style>
  127. </style>