pieChart.vue 3.0 KB

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