percent-pie-chart.vue 2.5 KB

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