zxl
5 天以前 934654b44fb2670a7ce08aafa3d1d85fe783ae26
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
<template>
  <div>
    <div class="affix-time">
      <Affix :offset-top="100">
        <div class="flex affix-box">
          <affixTime :closeShop="true" @selected="clickBreadcrumb" />
 
          <InputNumber
            placeholder="展示前N"
            :max="200"
            :min="10"
            v-model="params.top"
          ></InputNumber>
          <Button style="margin-left: 10px" @click="search">搜索</Button>
        </div>
      </Affix>
    </div>
 
    <div id="container"></div>
    <Table border :columns="columns" :data="hotWordsData"></Table>
  </div>
</template>
 
<script>
import { Chart } from "@antv/g2";
import { getHotWordsStatistics } from "@/api/index";
import affixTime from "@/components/affix-time";
 
export default {
  components: {
    affixTime,
  },
  data() {
    return {
      params: {
        // 请求参数
        searchType: "LAST_SEVEN",
        year: "",
        month: "",
        top: 50,
      },
      columns: [
        {
          title: "热词名称",
          key: "keywords",
        },
        {
          title: "搜索次数",
          key: "score",
        },
      ],
      hotWordsChart: "", //图表
      hotWordsData: [], //数据
      orderChart: "",
    };
  },
  computed: {},
  watch: {
    params: {
      handler(val) {
        this.search();
      },
      deep: true,
      immediate: true,
    },
    year(val) {
      this.params.year = new Date(val).getFullYear();
    },
  },
  methods: {
    clickBreadcrumb(val) {
      this.params = { ...this.params, ...val };
    },
    // 初始化图表
    async search() {
      const res = await getHotWordsStatistics(this.params);
      if (res.success) {
        this.hotWordsData = res.result;
        if (!this.hotWordsChart) {
          this.hotWordsChart = new Chart({
            container: "container",
            autoFit: true,
            height: 500,
            padding: [50, 20, 50, 20],
          });
        }
 
        this.init();
      }
    },
    handleClickSearch() {},
    init() {
      if (this.hotWordsChart) {
        this.hotWordsChart.data(this.hotWordsData);
        this.hotWordsChart.scale("score", {
          alias: "搜索次数",
        });
 
        this.hotWordsChart.axis("keywords", {
          tickLine: {
            alignTick: false,
          },
        });
        this.hotWordsChart.axis("score", false);
 
        this.hotWordsChart.tooltip({
          showMarkers: false,
        });
        this.hotWordsChart
          .interval()
          .position("keywords*score")
          .color("#f59b99");
        this.hotWordsChart.interaction("element-active");
 
        // 添加文本标注
        // this.hotWordsData.forEach((item) => {
        //   this.hotWordsChart
        //     .annotation()
        //     .text({
        //       position: [item.keywords, item.score],
        //       content: item.score,
        //       style: {
        //         textAlign: "center",
        //       },
        //       offsetY: -30,
        //     })
        // });
 
        this.hotWordsChart.render();
      }
    },
  },
  mounted() {
    this.init();
  },
};
</script>
 
<style lang="scss" scoped>
.affix-time {
  padding-left: 15px;
}
</style>