pieChartA.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <template>
  2. <div
  3. class="linechart"
  4. :id="id"
  5. style="width: calc(37.5vw - 180px); height: calc(42vh - 165px)"
  6. ></div>
  7. </template>
  8. <script>
  9. //import from '';
  10. export default {
  11. name: "pieChart", //
  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: "a",
  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. tooltip: {
  77. //提示框,可以在全局也可以在
  78. trigger: "item", //提示框的样式
  79. // formatter: "{a} <br/>{b}: {c} ({d}%)",
  80. formatter: "{a} <br/>{b} ({d}%)",
  81. color: "#000", //提示框的背景色
  82. textStyle: {
  83. //提示的字体样式
  84. color: "#ffffff",
  85. },
  86. },
  87. series: [
  88. {
  89. name: this.seriesName,
  90. type: "pie",
  91. // radius: '55%',
  92. data,
  93. top: 15,
  94. label: {
  95. // alignTo: "edge",
  96. // formatter: "{name|{b}}\n{time|{c} 小时}",
  97. // minMargin: 5,
  98. edgeDistance: 15,
  99. distance: 25,
  100. // lineHeight: 15,
  101. // rich: {
  102. // time: {
  103. // fontSize: 10,
  104. // color: "#999",
  105. // },
  106. // },
  107. },
  108. },
  109. ],
  110. };
  111. this.charts.setOption(option);
  112. },
  113. },
  114. mounted() {
  115. this.charts = this.$echarts.init(document.getElementById(this.id));
  116. this.$nextTick(() => {
  117. window.addEventListener("resize", () => {
  118. this.charts.resize();
  119. });
  120. });
  121. },
  122. destroyed() {
  123. // console.log("destroyed");
  124. this.charts.dispose();
  125. },
  126. };
  127. </script>
  128. <style lang='less' scoped>
  129. .linechart {
  130. margin: 0 auto;
  131. }
  132. </style>