1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
<template>
  <!--
          作者:luoyiming
          时间:2019-10-24
          描述:统计-会员统计-访问趋势分析
      -->
  <div class="ww100 mt30">
    <el-tabs v-model="activeName" @tab-click="handleClick">
      <el-tab-pane label="访问趋势分析" name="new"></el-tab-pane>
      <!-- <el-tab-pane label="访问趋势分析" name="pay"></el-tab-pane> -->
    </el-tabs>
    <div class="d-b-c">
      <div>
        <el-date-picker
          size="small"
          v-model="datePicker"
          type="daterange"
          align="right"
          unlink-panels
          format="yyyy-MM-dd"
          value-format="yyyy-MM-dd"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          @change="changeDate"
          :picker-options="pickerOptions"
        ></el-date-picker>
      </div>
    </div>
    <div class="">
      <div class="Echarts"><div id="LineChart"></div></div>
    </div>
  </div>
</template>
 
<script>
import StatisticsApi from '@/api/statistics.js';
import { formatDate } from '@/utils/DateTime.js'
export default {
  data() {
    let endDate=new Date();
    let startDate=new Date();
    startDate.setTime(startDate.getTime()- 3600 * 1000 * 24 * 7);
    return {
      /*是否正在加载*/
      loading: true,
      /*类别*/
      activeName: 'new',
      /*时间快捷选项*/
      pickerOptions: {
        shortcuts: [
          {
            text: '最近一周',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit('pick', [start, end]);
            }
          },
          {
            text: '最近一个月',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit('pick', [start, end]);
            }
          },
          {
            text: '最近三个月',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit('pick', [start, end]);
            }
          }
        ]
      },
      datePicker: [formatDate(startDate,'yyyy-MM-dd'),formatDate(endDate,'yyyy-MM-dd')],
      /*数据对象*/
      dataList: null,
      /*交易统计图表对象*/
      myChart: null,
      /*图表数据*/
      option: {
        title: {
          //text: 'ECharts 入门示例'
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true
        },
        tooltip: {
          trigger: 'axis'
        },
        yAxis: {}
      }
    };
  },
  created() {
 
  },
  mounted() {
    this.myEcharts();
  },
  methods: {
 
    /*切换*/
    handleClick(e) {
      this.getData();
    },
 
    /*选择时间*/
    changeDate() {
      this.getData();
    },
 
    /*创建图表对象*/
    myEcharts() {
      // 基于准备好的dom,初始化echarts实例
      this.myChart = this.$echarts.init(document.getElementById('LineChart'));
      /*获取列表*/
      this.getData();
    },
 
    /*格式数据*/
    createOption() {
      if (!this.loading) {
        let names = [];
        let xAxis = this.dataList.days;
        let series1 = [];
        let series2 = [];
        let series3 = [];
        let series4 = [];
        this.dataList.data.forEach(item => {
          series1.push(item.fav_store);
          series2.push(item.fav_product);
          series3.push(item.visit_user);
          series4.push(item.visit_total);
        });
          names = ['店铺收藏数','商品收藏数','访客数','访客量'];
 
        // 指定图表的配置项和数据
        this.option.xAxis = {
          type: 'category',
          boundaryGap: false,
          data: xAxis
        };
        this.option.color=["red", "#409EFF",'#E6A23C'];
 
        this.option.legend = {
          data: [{ name: names[0], color: '#ccc' },{ name: names[1]},{ name: names[2]},{ name: names[3]}]
        };
        this.option.series = [
          {
            name: names[0],
            type: 'line',
            data: series1,
            lineStyle: {
              color: 'red'
            }
          },
          {
            name: names[1],
            type: 'line',
            data: series2,
            lineStyle: {
              color: '#409EFF'
            }
          },
          {
            name: names[2],
            type: 'line',
            data: series3,
            lineStyle: {
              color: '#E6A23C'
            }
          },
          {
            name: names[3],
            type: 'line',
            data: series4,
            lineStyle: {
              color: '#E6A23C'
            }
          }
        ];
 
        this.myChart.setOption(this.option);
        this.myChart.resize();
      }
    },
 
    /*获取列表*/
    getData() {
      let self = this;
      self.loading = true;
 
      StatisticsApi.getVisit(
        {
          search_time: self.datePicker
        },
        true
      )
        .then(res => {
          self.dataList = res.data;
          self.loading = false;
          self.createOption();
        })
        .catch(error => {});
    }
  }
};
</script>
 
<style scoped="scoped">
.Echarts {
  box-sizing: border-box;
}
.Echarts > div {
  width: 100%;
  height: 360px;
  box-sizing: border-box;
}
</style>