You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

124 lines
3.0 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<template>
<block-box :title="title">
<template #extra>
<el-radio-group v-model="tabActive" size="small" @change="handleTabChange">
<el-radio-button v-for="{ tab, key } in config" :label="tab" :value="key" />
</el-radio-group>
</template>
<echarts-plus :options="getTopOptions(
currentConfig.find((item) => item.key === tabActive)?.data || [],
)
" :onClick="handleClick" style="min-height: 250px; height: 100%" />
</block-box>
</template>
<script setup>
import BlockBox from '@/components/BlockBox.vue';
import { onMounted, ref } from 'vue';
import EchartsPlus from '@/components/Echarts-plus.vue';
import cardApi from '~/vgpu/api/card';
import { cloneDeep } from 'lodash';
import { formatSmartPercentage } from '@/utils';
const props = defineProps({
title: String,
key: String,
config: Array,
onClick: Function,
xName: String,
});
const currentConfig = ref(props.config);
const tabActive = ref('');
const handleTabChange = (key) => {
tabActive.value = key;
};
const handleClick = (params) => {
if (props.onClick) {
props.onClick({ ...params, tabActive: tabActive.value });
}
};
const getTopOptions = () => {
const config = currentConfig.value.find(
(item) => item.key === tabActive.value,
);
const data = cloneDeep(config?.data) || [];
const xName = props?.xName || (props?.title?.slice(4, 7) || '') + '%';
return {
tooltip: {
trigger: 'axis',
axisPointer: {
type: 'shadow',
},
formatter: function (params) {
var res = params[0].name + '<br/>';
for (var i = 0; i < params.length; i++) {
res +=
params[i].marker +
params[i].seriesName +
formatSmartPercentage(params[i].value) +
`${config.unit || '%'}<br/>`;
}
return res;
},
},
legend: {
show: false,
},
grid: {
left: '3%',
right: '4%',
// bottom: '3%',
// top: '0',
containLabel: true,
},
xAxis: {
type: 'value',
boundaryGap: [0, 0.01],
name: xName, // 添加x轴名称
nameLocation: 'end', // 把名字放在轴的末端
nameTextStyle: {
verticalAlign: 'top', // 垂直方向靠上
align: 'right', // 水平方向靠右
padding: [30, 0, 0, 0] // 调整和轴的间距30可根据需要调整
}
},
yAxis: {
type: 'category',
name: '节点',
data: data
.reverse()
.map((item) =>
item.name.length > 15 ? item.name.slice(0, 15) + '...' : item.name,
),
},
series: [
{
name: '',
type: 'bar',
data: data,
},
],
};
};
onMounted(async () => {
tabActive.value = currentConfig.value[0].key;
currentConfig.value.forEach((v, i) => {
cardApi
.getInstantVector({
query: v.query,
})
.then((res) => {
currentConfig.value[i].data = res.data.map((item) => ({
name: item.metric[v.nameKey],
value: item.value,
}));
});
});
});
</script>