normal-pie-chart.vue 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <template>
  2. <div class="chart" :id="id"></div>
  3. </template>
  4. <script>
  5. import util from "@/helper/util.js";
  6. import partten from "@/helper/partten.js";
  7. import * as echarts from "echarts";
  8. export default {
  9. name: "percent-pie",
  10. componentName: "percent-pie",
  11. props: {
  12. width: {
  13. type: String,
  14. default: "100%",
  15. },
  16. height: {
  17. type: String,
  18. default: "18.519vh",
  19. },
  20. // 百分比值
  21. // 外部饼图数据
  22. list: {
  23. type: Array,
  24. default: () => [
  25. {
  26. value: 310,
  27. name: "邮件营销",
  28. },
  29. {
  30. value: 234,
  31. name: "联盟广告",
  32. },
  33. {
  34. value: 335,
  35. name: "视频广告",
  36. },
  37. {
  38. value: 548,
  39. name: "百度",
  40. },
  41. {
  42. value: 351,
  43. name: "谷歌",
  44. },
  45. ],
  46. },
  47. },
  48. data() {
  49. return {
  50. id: "",
  51. chart: null,
  52. };
  53. },
  54. computed: {},
  55. methods: {
  56. initChart() {
  57. let option = {
  58. color: ["#05bb4c", "#4b55ae", "#fa8c16", "#f8de5b", "#1a93cf", "#c531c7", "#bd3338"],
  59. tooltip: {
  60. trigger: "item",
  61. backgroundColor: this.$store.state.themeName === "dark" ? "rgba(0,0,0,0.4)" : "rgba(255,255,255,0.5)",
  62. borderColor: this.$store.state.themeName === "dark" ? partten.getColor("gray") : "#000",
  63. textStyle: {
  64. color: this.$store.state.themeName === "dark" ? "#fff" : "#000",
  65. fontSize: util.vh(16),
  66. },
  67. },
  68. grid: {
  69. top: 8,
  70. left: 8,
  71. right: 8,
  72. bottom: 8,
  73. },
  74. legend: {
  75. icon: "circle",
  76. orient: "vertical",
  77. top: "center",
  78. right: "0",
  79. align: "left",
  80. textStyle: {
  81. color: this.$store.state.themeName === "dark" ? "#fff" : "#000",
  82. },
  83. itemGap: 8,
  84. },
  85. series: [
  86. {
  87. type: "pie",
  88. radius: "70%",
  89. center: ["35%", "50%"],
  90. data: this.list,
  91. labelLine: {
  92. show: false,
  93. },
  94. label: {
  95. show: false,
  96. },
  97. emphasis: {
  98. itemStyle: {
  99. shadowBlur: 10,
  100. shadowOffsetX: 0,
  101. shadowColor: "rgba(0, 0, 0, 0.5)",
  102. },
  103. },
  104. },
  105. ],
  106. };
  107. this.chart.setOption(option);
  108. },
  109. },
  110. created() {
  111. this.id = "pie-chart-" + util.newGUID();
  112. },
  113. mounted() {
  114. this.$el.style.width = this.width;
  115. this.$el.style.height = this.height;
  116. this.chart = echarts.init(this.$el);
  117. this.initChart();
  118. },
  119. updated() {
  120. this.initChart();
  121. },
  122. };
  123. </script>
  124. <style lang="less">
  125. .chart {
  126. width: 100%;
  127. height: 100%;
  128. display: inline-block;
  129. }
  130. </style>