ChartPig.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <template>
  2. <div id="ChartPig" style="width: 100%; height: 100%"></div>
  3. </template>
  4. <script>
  5. import { mapState } from "vuex";
  6. export default {
  7. name: "ChartPig",
  8. data() {
  9. return {
  10. myChart: null,
  11. };
  12. },
  13. props: {
  14. data: {
  15. type: Object,
  16. default: function() {
  17. return {};
  18. },
  19. },
  20. },
  21. computed: {
  22. ...mapState(["color"]),
  23. },
  24. watch: {
  25. color(newVal) {
  26. if (newVal) {
  27. this.myChart.clear();
  28. this.init();
  29. }
  30. },
  31. data: {
  32. handler(newVal) {
  33. if (newVal) {
  34. this.myChart.clear();
  35. this.init();
  36. }
  37. },
  38. deep: true,
  39. },
  40. },
  41. methods: {
  42. init() {
  43. let dataAxis = this.data.timeList;
  44. let start = dataAxis.length - 12;
  45. let end = dataAxis.length - 1;
  46. let quantityList = this.data.quantityList;
  47. let moneyListTooltip = this.data.moneyList1;
  48. let options = {
  49. title: {
  50. x: 60,
  51. y: 20,
  52. text: "近期销售情况",
  53. textStyle: {
  54. fontSize: 14,
  55. color: "#6e7079",
  56. },
  57. },
  58. tooltip: {
  59. trigger: "axis",
  60. formatter: (params) => {
  61. var res = "<div>" + params[0].name + "</div>"; // 字符串形式的html标签会被echarts转换渲染成数据,这个res主要是画的tooltip里的上部分的标题部分
  62. const index = this.data.moneyList.findIndex(
  63. (item) => item === params[0].value
  64. );
  65. for (var i = 0; i < params.length; i++) {
  66. //因为是个数组,所以要遍历拿到里面的数据,并加入到tooltip的数据内容部分里面去
  67. res +=
  68. `<div style="color: #fff;font-size: 14px; padding:0 12px;">
  69. <span style="display:inline-block;margin-right:5px;border-radius:5px;width:10px;height:10px;background-color:${[
  70. params[i].color, // 默认是小圆点,我们将其修改成有圆角的正方形,这里用的是模板字符串。并拿到对应颜色、名字、数据
  71. ]};"></span>
  72. ${moneyListTooltip[index]}元
  73. </div>` +
  74. `<div style="color: #fff;font-size: 14px; padding:0 12px;"><span style="display:inline-block;margin-right:5px;border-radius:5px;width:10px;height:10px;background-color:${[
  75. params[i].color,
  76. ]};"></span>${quantityList[index]}头</div>`;
  77. }
  78. return res;
  79. },
  80. },
  81. color: [this.color],
  82. dataZoom: [
  83. {
  84. type: "inside",
  85. startValue: start,
  86. endValue: end,
  87. show: false,
  88. // zoomOnMouseWheel: false,
  89. // zoomLock: true,
  90. },
  91. ],
  92. xAxis: [
  93. {
  94. type: "category",
  95. data: dataAxis,
  96. axisPointer: {
  97. type: "none",
  98. },
  99. axisLine: {
  100. show: false,
  101. lineStyle: {
  102. color: "#6e7079",
  103. },
  104. },
  105. axisTick: {
  106. show: false,
  107. },
  108. },
  109. ],
  110. yAxis: [
  111. {
  112. type: "value",
  113. // name: "元",
  114. axisLabel: {
  115. formatter: "{value} 元",
  116. show: true,
  117. },
  118. axisLine: {
  119. show: false,
  120. lineStyle: {
  121. color: "#6e7079",
  122. },
  123. },
  124. axisTick: {
  125. show: false,
  126. },
  127. },
  128. ],
  129. series: [
  130. {
  131. name: "元",
  132. type: "line",
  133. // stack: 'Total',
  134. smooth: true,
  135. areaStyle: {},
  136. emphasis: {
  137. focus: "series",
  138. },
  139. itemStyle: {
  140. color: this.color,
  141. borderColor: this.color,
  142. normal: {
  143. label: {
  144. show: true,
  145. textStyle: {
  146. fontSize: 14,
  147. },
  148. },
  149. },
  150. },
  151. data: this.data.moneyList,
  152. },
  153. ],
  154. };
  155. this.myChart.setOption(options);
  156. },
  157. },
  158. mounted() {
  159. this.myChart = this.$echarts.init(document.getElementById("ChartPig"));
  160. this.init();
  161. },
  162. };
  163. </script>
  164. <style scoped></style>