pieChartMainPage.vue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <template>
  2. <div
  3. class="linechart"
  4. :id="id"
  5. style="min-width: 330px; width: calc(37.5vw - 180px); min-height: 110px; height: calc(42vh - 165px)"
  6. ></div>
  7. </template>
  8. <script>
  9. //import from '';
  10. export default {
  11. name: "pieChartMainPage", // 首页-饼图
  12. components: {},
  13. data() {
  14. return {
  15. charts: "",
  16. colors: [
  17. "#5B8FF9",
  18. "#61DDAA",
  19. "#65789B",
  20. "#F6BD16",
  21. "#FF5C5C",
  22. "#4671C6",
  23. "#7CCE7A",
  24. "#FFC655",
  25. "#4BC2DF",
  26. "#00A476",
  27. "#FF7D46",
  28. "#A45DB2",
  29. ],
  30. };
  31. },
  32. props: {
  33. id: {
  34. default: "pieChartMainPage",
  35. type: String,
  36. },
  37. seriesName: {
  38. default: "统计",
  39. type: String,
  40. },
  41. stateName: {
  42. default: "name",
  43. type: String,
  44. },
  45. dataList: {
  46. default: function () {
  47. return [];
  48. },
  49. type: Array,
  50. },
  51. },
  52. watch: {
  53. dataList(val) {
  54. if (val.length > 0) {
  55. this.initStateList();
  56. }
  57. },
  58. },
  59. methods: {
  60. initStateList() {
  61. let dataList = this.dataList;
  62. let data = [];
  63. for (let index = 0; index < dataList.length; index++) {
  64. const element = dataList[index];
  65. data[index] = {
  66. value: element.num,
  67. name: element[this.stateName] + "\n" + element.num,
  68. itemStyle: {
  69. normal: {
  70. color: this.colors[index],
  71. },
  72. },
  73. };
  74. }
  75. let option = {
  76. title: {
  77. text: "暂无数据",
  78. show: dataList.length === 0,
  79. left: "center",
  80. top: "center",
  81. },
  82. tooltip: {
  83. //提示框,可以在全局也可以在
  84. trigger: "item", //提示框的样式
  85. // formatter: "{a} <br/>{b}: {c} ({d}%)",
  86. formatter: "{a} <br/>{b} ({d}%)",
  87. color: "#000", //提示框的背景色
  88. textStyle: {
  89. //提示的字体样式
  90. color: "#ffffff",
  91. },
  92. confine: true,
  93. },
  94. series: [
  95. {
  96. name: this.seriesName,
  97. type: "pie",
  98. data,
  99. top: 15,
  100. avoidLabelOverlap: true,
  101. label: {
  102. edgeDistance: 15,
  103. distance: 25,
  104. },
  105. },
  106. ],
  107. };
  108. this.charts.setOption(option);
  109. },
  110. },
  111. mounted() {
  112. this.charts = this.$echarts.init(document.getElementById(this.id));
  113. this.initStateList();
  114. this.$nextTick(() => {
  115. window.addEventListener("resize", () => {
  116. this.charts.resize();
  117. });
  118. });
  119. },
  120. destroyed() {
  121. // console.log("pie destroyed");
  122. this.charts.dispose();
  123. },
  124. };
  125. </script>
  126. <style lang='less' scoped>
  127. .linechart {
  128. margin: 0 auto;
  129. overflow: hidden;
  130. }
  131. </style>