杨磊
2024-07-26 fe7b350b7c551278d8e74c3d34c276d2f71e3f86
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
<template>
  <view style="width: 100%;height: 100%;display: flex;flex-direction: column;">
    <!-- 顶部导航 -->
    <headNav :idIndex="idIndex" text="学术图谱" />
    <view id="line-chart" style="flex: 1;overflow: hidden;padding: 50px;"></view>
  </view>
</template>
 
<script>
import { getDynasty, getDynastyStatistics } from "@/api/index.js";
import * as echarts from "echarts";
import axios from "axios";
export default {
  data() {
    return {
      idIndex: 0,
      dynastyData: [],
      xAxisData: [],
      schoolList: []
    };
  },
  onLoad(options) {
    this.idIndex = options.id;
    // 获取朝代
    this.getDynastyData();
  },
  methods: {
    getDynastyData() {
      getDynasty().then((res) => {
        res.list.sort((a, b) => a.end - b.end);
        this.dynastyData = res.list;
        this.xAxisData = res.list.map((item) => item.dynastyChs);
        this.getData();
      });
    },
    getData() {
      getDynastyStatistics().then((res) => {
        this.schoolList = [];
        const schoolMap = {};
        for (let i = 0; i < res.list.length; i++) {
          const item = res.list[i];
          // 数据合并
          if (schoolMap[item.schoolId]) {
            schoolMap[item.schoolId].data.push({
              dynastyCode: item.dynastyCode,
              personNumber: item.personNumber
            });
          } else {
            schoolMap[item.schoolId] = {
              name: item.schoolName,
              data: [
                {
                  dynastyCode: item.dynastyCode,
                  personNumber: item.personNumber
                }
              ]
            };
          }
        }
 
        for (const key in schoolMap) {
          // 处理数量
          const personNumbers = [];
          for (let z = 0; z < this.dynastyData.length; z++) {
            const dynastyItem = this.dynastyData[z];
            const returnData = schoolMap[key].data.find(
              (item) => item.dynastyCode == dynastyItem.id
            );
            if (returnData) {
              personNumbers.push(returnData.personNumber);
            } else {
              personNumbers.push(0);
            }
          }
          // 处理学派
          this.schoolList.push({
            id: key,
            name: schoolMap[key].name,
            type: "line",
            data: personNumbers,
            smooth: 0.5,
            // 设置线条的颜色
            itemStyle: {
              normal: {
                lineStyle: {
                  width: 2
                }
              }
            },
            // 设置线条右侧的字体颜色等
            label: {
              show: true, // 显示标签
              position: "right", // 标签位置:右侧
              fontSize: 18,
              color: "inherit",
              formatter: (param) => {
                if (param.dataIndex == this.dynastyData.length - 1) {
                  const data = this.schoolList.find(
                    (item) => item.name == param.seriesName
                  ).data;
                  return param.seriesName + " (" + eval(data.join("+")) + ")";
                } else {
                  return "";
                }
              }
            }
          });
        }
 
        this.renderChart();
      });
    },
    renderChart() {
      const chart = echarts.init(document.getElementById("line-chart"));
      const option = {
        tooltip: {
          trigger: "axis"
        },
        legend: {
          data: this.schoolList.map((item) => item.name)
        },
        xAxis: {
          type: "category",
          boundaryGap: false,
          data: this.xAxisData,
          axisLabel: {
            fontSize: 16 // 设置 x 轴上文字的字体大小为 12 像素
          },
          axisLine: {
            lineStyle: {
              width: 2 // 设置x轴线条宽度为2像素
            }
          }
        },
        yAxis: {
          type: "value",
          axisLabel: {
            fontSize: 16 // 设置 x 轴上文字的字体大小为 12 像素
          },
          axisLine: {
            lineStyle: {
              width: 2 // 设置x轴线条宽度为2像素
            }
          }
        },
        series: this.schoolList,
        textStyle: {
          fontSize: 16 // 设置全局文字的字体大小
        }
      };
      chart.setOption(option);
      chart.on("click", (params) => {
        const data = this.schoolList.find(
          (item) => item.name == params.seriesName
        );
        uni.navigateTo({
          url: "/pages/academicGenres/detail?id=" + data.id
        });
      });
    }
  }
};
</script>
 
<style>
#line-chart {
  height: 6rem;
}
</style>