reportDoughnut.vue 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <template>
  2. <div style="width:100%;height:100%;text-align:center;" :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. text: {
  23. type: String,
  24. },
  25. type: {},
  26. name: "",
  27. sum: ""
  28. },
  29. data() {
  30. return {
  31. myCharts: null
  32. }
  33. },
  34. created() { },
  35. mounted() {
  36. this.drawDoughnut()
  37. },
  38. methods: {
  39. drawDoughnut() {
  40. let options = {
  41. tooltip: {
  42. trigger: 'item',
  43. formatter: "{b}"
  44. },
  45. legend: {
  46. show: false,
  47. orient: 'vertical',
  48. right: 10,
  49. top: 30,
  50. bottom: 20,
  51. data: this.renderData.map(item => {
  52. return item.name
  53. })
  54. },
  55. series: [{
  56. name: '',
  57. type: 'pie',
  58. radius: ['40%', '60%'],
  59. itemStyle: {
  60. normal: {
  61. label: {
  62. show: this.type != 'left' ? false : true,
  63. textStyle: {
  64. color: '#3c4858',
  65. fontSize: "12"
  66. },
  67. formatter: function (val) { //让series 中的文字进行换行
  68. return val.name.split("-")[0];
  69. }
  70. },
  71. title: {
  72. text: 'aaaa'
  73. },
  74. labelLine: {
  75. show: this.type != 'left' ? false : true,
  76. lineStyle: {
  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: ['#67C23A', 'rgb(225, 243, 216)', 'rgb(244, 244, 245)']
  90. }
  91. options.graphic = {
  92. type: 'text',
  93. left: 'center',
  94. top: 'center',
  95. style: {
  96. text: `${this.text|| ''}`,
  97. textAlign: 'center',
  98. fill: '#000',
  99. width: 30,
  100. height: 30
  101. }
  102. }
  103. this.myCharts = echarts.init(document.getElementById(this.id))
  104. this.myCharts.setOption(options)
  105. }
  106. },
  107. watch: {
  108. 'renderData': {
  109. deep: true,
  110. handler(n,o) {
  111. this.drawDoughnut()
  112. }
  113. }
  114. }
  115. }
  116. </script>
  117. <style>
  118. </style>