ChartSalePig.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <template>
  2. <div :id="'chart-sale-pie-' + id" style="width: 100%; height: 100%"></div>
  3. </template>
  4. <script>
  5. import { mapState } from "vuex";
  6. export default {
  7. name: "ChartStark",
  8. computed: {
  9. ...mapState(["color"]),
  10. },
  11. props: {
  12. id: {
  13. type: String,
  14. default: () => "0",
  15. },
  16. starkCount: {
  17. type: String,
  18. default: () => "0",
  19. },
  20. },
  21. data() {
  22. return {
  23. myChart: null,
  24. };
  25. },
  26. watch: {
  27. color(newVal) {
  28. if (newVal) {
  29. this.myChart.clear();
  30. this.init();
  31. }
  32. },
  33. starkCount(newVal) {
  34. if (newVal) {
  35. this.myChart.clear();
  36. this.init();
  37. }
  38. },
  39. },
  40. methods: {
  41. init() {
  42. let data = [{ name: "总重量", value: this.starkCount.toString() }];
  43. let formatNumber = function(num) {
  44. let reg = /(?=(\B)(\d{3})+$)/g;
  45. return num.toString().replace(reg, ",");
  46. };
  47. let total = data.reduce((a, b) => {
  48. return a + b.value * 1;
  49. }, 0);
  50. let options = {
  51. color: [this.color],
  52. title: [
  53. {
  54. text:
  55. "{name|" +
  56. "总重量" +
  57. "}\n{val|" +
  58. formatNumber(total) +
  59. "千克" +
  60. "}",
  61. top: "center",
  62. left: "center",
  63. textStyle: {
  64. rich: {
  65. name: {
  66. fontSize: 14,
  67. fontWeight: "normal",
  68. color: "#666666",
  69. padding: [10, 0],
  70. },
  71. val: {
  72. fontSize: 32,
  73. fontWeight: "bold",
  74. color: "#333333",
  75. },
  76. },
  77. },
  78. },
  79. ],
  80. series: [
  81. {
  82. type: "pie",
  83. radius: ["45%", "60%"],
  84. center: ["50%", "50%"],
  85. data: data,
  86. hoverAnimation: false,
  87. itemStyle: {
  88. normal: {
  89. borderColor: "#fff",
  90. borderWidth: 2,
  91. },
  92. },
  93. labelLine: {
  94. normal: {
  95. length: 20,
  96. length2: 120,
  97. lineStyle: {
  98. color: "#e6e6e6",
  99. },
  100. },
  101. },
  102. label: {
  103. // normal: {
  104. // formatter: (params) => {
  105. // return (
  106. // "{icon|●}{name|" +
  107. // params.name +
  108. // "}{value|" +
  109. // formatNumber(params.value) +
  110. // "}"
  111. // );
  112. // },
  113. // padding: [0, -100, 25, -100],
  114. // rich: {
  115. // icon: {
  116. // fontSize: 16,
  117. // },
  118. // name: {
  119. // fontSize: 14,
  120. // padding: [0, 10, 0, 4],
  121. // color: "#666666",
  122. // },
  123. // value: {
  124. // fontSize: 18,
  125. // fontWeight: "bold",
  126. // color: "#333333",
  127. // },
  128. // },
  129. // },
  130. show: false,
  131. },
  132. },
  133. ],
  134. };
  135. this.myChart.setOption(options);
  136. },
  137. },
  138. mounted() {
  139. this.myChart = this.$echarts.init(
  140. document.getElementById("chart-sale-pie-" + this.id)
  141. );
  142. this.init();
  143. },
  144. };
  145. </script>
  146. <style scoped></style>