percent-pie-chart.vue 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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: "18.519vh",
  15. },
  16. height: {
  17. type: String,
  18. default: "18.519vh",
  19. },
  20. // 百分比值
  21. value: {
  22. type: Number,
  23. default: 10,
  24. },
  25. max: {
  26. type: Number,
  27. default: 100,
  28. },
  29. // 颜色 green yellow 等 partten支持的颜色
  30. color: {
  31. type: String,
  32. default: "green",
  33. },
  34. },
  35. data() {
  36. return {
  37. id: "",
  38. chart: null,
  39. };
  40. },
  41. computed: {
  42. colorValue() {
  43. return partten.getColor(this.color);
  44. },
  45. },
  46. methods: {
  47. initChart() {
  48. let option = {
  49. calculable: true,
  50. series: [
  51. {
  52. name: "",
  53. type: "pie",
  54. radius: ["0%", "100%"],
  55. center: ["50%", "50%"],
  56. data: [
  57. {
  58. value: this.value,
  59. name: "",
  60. itemStyle: {
  61. color: this.colorValue,
  62. },
  63. },
  64. {
  65. value: this.max - this.value,
  66. name: "",
  67. itemStyle: {
  68. color: "transparent",
  69. },
  70. },
  71. ],
  72. label: {
  73. show: false,
  74. position: "center",
  75. },
  76. },
  77. {
  78. name: "",
  79. type: "pie",
  80. radius: ["0%", "95%"],
  81. center: ["50%", "50%"],
  82. data: [
  83. {
  84. value: this.value,
  85. name: "",
  86. itemStyle: {
  87. color: "transparent",
  88. },
  89. },
  90. {
  91. value: this.max - this.value,
  92. name: "",
  93. itemStyle: {
  94. color: "#5f6769",
  95. },
  96. },
  97. ],
  98. label: {
  99. show: false,
  100. position: "center",
  101. },
  102. },
  103. ],
  104. };
  105. this.chart.setOption(option);
  106. },
  107. },
  108. created() {
  109. this.id = "pie-chart-" + util.newGUID();
  110. },
  111. mounted() {
  112. this.$el.style.width = this.width;
  113. this.$el.style.height = this.height;
  114. this.chart = echarts.init(this.$el);
  115. this.initChart();
  116. },
  117. updated() {
  118. this.initChart();
  119. },
  120. };
  121. </script>
  122. <style lang="less">
  123. .chart {
  124. width: 100%;
  125. height: 100%;
  126. display: inline-block;
  127. }
  128. </style>