line01.vue 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <template>
  2. <div class="chart-box">
  3. <div class="title-top">
  4. <span>每月店铺订单量日趋势</span>
  5. </div>
  6. <div class="chart" :id="id"></div>
  7. </div>
  8. </template>
  9. <script lang="ts">
  10. import { Component, Prop, Vue, Watch } from 'vue-property-decorator'
  11. import * as echarts from 'echarts';
  12. type EChartsOption = echarts.EChartsOption;
  13. @Component
  14. export default class Line01 extends Vue {
  15. id=this.randomString();
  16. mounted(){
  17. this.getData()
  18. }
  19. randomString(){
  20. const str = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  21. let result = '';
  22. for (let i = 10; i > 0; --i)
  23. result += str[Math.floor(Math.random() * str.length)];
  24. return result;
  25. }
  26. initChart(list:Array<any>){
  27. if(!list) return
  28. let chartDom:any = document.getElementById(this.id);
  29. let myChart = echarts.init(chartDom);
  30. let option: EChartsOption;
  31. let dataX:Array<any>=this.getDaysList()
  32. let dataY:Array<any>=[]
  33. for(const item of list) {
  34. let obj:any={
  35. name: item[0].shop_name,
  36. type: 'line',
  37. smooth: true,
  38. data: []
  39. }
  40. if(item){
  41. for(const arr of dataX) {
  42. let isAdd = false
  43. for(const item2 of item){
  44. if(arr == item2.day) {
  45. obj.data.push(item2.total)
  46. isAdd = true
  47. break
  48. }
  49. }
  50. if(!isAdd) {
  51. obj.data.push(0)
  52. }
  53. }
  54. }
  55. dataY.push(obj)
  56. }
  57. option = {
  58. tooltip: {
  59. trigger: 'axis'
  60. },
  61. legend:{
  62. show:true,
  63. top:0
  64. },
  65. grid: {
  66. left: '3%',
  67. right: '4%',
  68. bottom: '3%',
  69. containLabel: true
  70. },
  71. toolbox: {
  72. feature: {
  73. // saveAsImage: {}
  74. }
  75. },
  76. xAxis: {
  77. type: 'category',
  78. boundaryGap: false,
  79. data: dataX
  80. },
  81. yAxis: {
  82. type: 'value'
  83. },
  84. series: dataY
  85. };
  86. option && myChart.setOption(option);
  87. }
  88. getDaysList() {
  89. let daysHandle = (n:number) => {
  90. let dy = new Date()
  91. dy.setTime(dy.getTime() - 3600 * 1000 * 24 * n)
  92. let y = dy.getFullYear();
  93. let m:any = dy.getMonth() + 1;
  94. let d:any = dy.getDate();
  95. m = m < 10 ? '0' + m : m;
  96. d = d < 10 ? '0' + d : d;
  97. return y + '-' + m + '-' + d;
  98. }
  99. let data:Array<any> = []
  100. for(let i = 29; i >= 0; i--) {
  101. data.push(daysHandle(i))
  102. }
  103. return data;
  104. }
  105. getData() {
  106. (this as any).$request({
  107. url:'/omsOrder/report/shopDaylyCount',
  108. method: 'GET',
  109. }).then((res:any) => {
  110. if(res.data) {
  111. this.initChart(res.data)
  112. }
  113. }).catch(() =>{})
  114. }
  115. }
  116. </script>
  117. <style lang="scss" scoped>
  118. .chart-box{
  119. height: 100%;
  120. width: 100%;
  121. .title-top{
  122. height: 40px;
  123. display: flex;
  124. justify-content: center;
  125. align-items: center;
  126. position: relative;
  127. width: 100%;
  128. >span{
  129. font-size: 16px;
  130. font-weight: 700;
  131. }
  132. }
  133. }
  134. .chart{
  135. height: calc(100% - 40px);
  136. width: 100%;
  137. }
  138. </style>