zhitong.yu
8 天以前 378d781e6f35f89652aa36e079a8b7fc44cea77e
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
<template>
  <el-tabs v-model="tabActive" class="demo-tabs" @tab-change="handleTabChange">
    <el-tab-pane v-for="item in tab" :key="item.name" :label="item.label" :name="item.name"></el-tab-pane>
  </el-tabs>
  <div class="echarts">
    <ECharts :option="option" />
  </div>
</template>
 
<script setup lang="ts" name="cure">
import { ECOption } from "@/components/ECharts/config";
import ECharts from "@/components/ECharts/index.vue";
import { ref, onMounted, computed } from "vue";
import { useI18n } from "vue-i18n";
import { FindDayWarningCount } from "@/api/modules/hxzk/count/count";
 
const { t } = useI18n();
const loading = ref(false);
const tabActive = ref(1);
 
// 使用计算属性来获取翻译后的标签
const tab = computed(() => [
  { label: t("curve.last7Days"), name: 1 },
  { label: t("curve.lastMonth"), name: 2 },
  { label: t("curve.last3Months"), name: 3 },
  { label: t("curve.lastHalfYear"), name: 4 },
  { label: t("curve.lastYear"), name: 5 }
]);
 
// 将对象格式的数据转换为ECharts需要的格式
const transformData = (data: Record<string, number>) => {
  return Object.entries(data).map(([spotName, value]) => ({
    spotName,
    value
  }));
};
 
const option = ref<ECOption>({
  xAxis: { type: "category", data: [] },
  yAxis: { type: "value" },
  series: [{ type: "bar", data: [] }]
});
 
// 获取数据的方法
const fetchData = async (timeRange: number) => {
  try {
    loading.value = true;
    const params = {
      time: timeRange
    };
    const res = await FindDayWarningCount(params);
 
    // 转换数据格式
    const transformedData = transformData(res || {});
    console.log(transformedData);
    updateChartOption(transformedData);
  } catch (error) {
    console.error(t("curve.fetchDataError"), error);
    updateChartOption([]); // 出错时传入空数组
  } finally {
    loading.value = false;
  }
};
 
// 更新图表配置的方法
const updateChartOption = (data: Array<{ spotName: string; value: number }>) => {
  option.value = {
    tooltip: {
      trigger: "axis",
      backgroundColor: "transparent",
      axisPointer: {
        type: "none"
      },
      padding: 0,
      formatter: (p: any) => {
        let dom = `<div style="width:100%; height: 70px !important; display:flex;flex-direction: column;justify-content: space-between;padding:10px;box-sizing: border-box;
        color:#fff; background: #6B9DFE;border-radius: 4px;font-size:14px; ">
          <div style="display: flex; align-items: center;"> <div style="width:5px;height:5px;background:#ffffff;border-radius: 50%;margin-right:5px"></div>${t("curve.platform")} :  ${p[0].name}</div>
          <div style="display: flex;align-items: center;"><div style="width:5px;height:5px;background:#ffffff;border-radius: 50%;margin-right:5px"></div>${t("curve.dataVolume")} :  ${p[0].value}</div>
        </div>`;
        return dom;
      }
    },
    toolbox: {
      show: true,
      orient: "horizontal"
    },
    grid: {
      left: "0",
      right: "0"
    },
    dataZoom: [
      {
        show: false,
        height: 10,
        xAxisIndex: [0],
        bottom: 0,
        startValue: 0,
        endValue: 9,
        handleStyle: {
          color: "#6b9dfe"
        },
        textStyle: {
          color: "transparent"
        }
      },
      {
        type: "inside",
        show: true,
        height: 0,
        zoomLock: true
      }
    ],
    xAxis: [
      {
        type: "category",
        data: data.map(item => item.spotName),
        axisTick: {
          show: false
        },
        axisLabel: {
          margin: 20,
          interval: 0,
          color: "#a1a1a1",
          fontSize: 14,
          formatter: function (name: string) {
            return name.length > 8 ? name.slice(0, 8) + "..." : name;
          }
        },
        axisLine: {
          lineStyle: {
            color: "#c0c0c0"
          }
        }
      }
    ],
    yAxis: [
      {
        min: 0,
        axisLine: {
          show: false
        },
        axisTick: {
          show: false
        },
        splitLine: {
          show: true,
          lineStyle: {
            color: "#c0c0c0"
          }
        },
        axisLabel: {
          color: "#a1a1a1",
          fontSize: 16,
          fontWeight: 400,
          formatter: function (value: number) {
            if (value === 0) {
              return value + "";
            } else if (value >= 10000) {
              return value / 10000 + "w";
            }
            return value + "";
          }
        }
      }
    ],
    series: [
      {
        name: "Direct",
        type: "bar",
        data: data.map(item => item.value),
        barWidth: "45px",
        itemStyle: {
          color: "#C5D8FF",
          borderRadius: [12, 12, 0, 0]
        },
        emphasis: {
          itemStyle: {
            color: "#6B9DFE"
          }
        }
      }
    ]
  };
};
 
// 标签切换事件处理
const handleTabChange = (name: number) => {
  fetchData(name);
};
 
// 初始化时加载数据
onMounted(() => {
  fetchData(tabActive.value);
});
</script>
 
<style lang="scss" scoped>
.echarts {
  width: 100%;
  height: 100%;
}
</style>