Files

230 lines
5.5 KiB
Vue

<template>
<view class="bar-rank-card">
<scroll-view class="bar-scroll" scroll-x :show-scrollbar="false" enhanced>
<view class="bar-scroll__inner" :style="{ width: chartWidth }">
<card_echart :options="chartOptions" />
</view>
</scroll-view>
</view>
</template>
<script setup>
import { computed, nextTick, onUnmounted, ref, watch } from 'vue';
import card_echart from '@/components/card_echart/card_echart.vue';
const props = defineProps({
active: {
type: Boolean,
default: true,
},
list: {
type: Array,
default: () => [],
},
colors: {
type: Array,
default: () => ['#FF6B6B', '#FFB020', '#2ED573', '#1E90FF', '#A66BFF'],
},
maxVisible: {
type: Number,
default: 6,
},
itemWidthRpx: {
type: Number,
default: 120,
},
yMaxFactor: {
type: Number,
default: 1.25,
},
animateOnce: {
type: Boolean,
default: true,
},
});
const animatedOnce = ref(false);
const displayList = ref([]);
let introTimer = null;
const normalizedList = computed(() => {
const raw = Array.isArray(props.list) ? props.list : [];
return raw.map((it, idx) => {
const id = it?.id ?? idx + 1;
const value = Number(it?.value) || 0;
const avatar = it?.avatar || '/static/head_null.png';
const label = String(it?.label || `Top${idx + 1}`);
return { id, value, avatar, label };
});
});
const chartWidth = computed(() => {
const n = normalizedList.value.length;
if (n <= props.maxVisible) return '100%';
return `${n * 96}rpx`;
});
const resetDisplay = () => {
displayList.value = normalizedList.value.map((it) => ({ ...it }));
};
const clearIntroTimer = () => {
if (introTimer) {
clearTimeout(introTimer);
introTimer = null;
}
};
const startIntroAnimation = async () => {
if (!props.active) return;
if (!props.animateOnce) {
resetDisplay();
return;
}
if (animatedOnce.value) {
resetDisplay();
return;
}
clearIntroTimer();
displayList.value = normalizedList.value.map((it) => ({ ...it, value: 0 }));
await nextTick();
introTimer = setTimeout(() => {
resetDisplay();
animatedOnce.value = true;
introTimer = null;
}, 80);
};
watch(
() => normalizedList.value,
() => {
if (!props.active) return;
if (animatedOnce.value && props.animateOnce) {
resetDisplay();
return;
}
startIntroAnimation();
},
{ deep: true, immediate: true },
);
watch(
() => props.active,
(val) => {
if (val) {
startIntroAnimation();
return;
}
clearIntroTimer();
resetDisplay();
},
{ immediate: true },
);
onUnmounted(() => {
clearIntroTimer();
});
const chartOptions = computed(() => {
const list = Array.isArray(displayList.value) ? displayList.value : [];
const xData = list.map((it) => it.label);
const palette =
Array.isArray(props.colors) && props.colors.length ? props.colors : ['#3d5efb'];
const barData = list.map((it, i) => ({
name: String(it?.id ?? ''),
value: Number(it?.value) || 0,
itemStyle: {
color: palette[i % palette.length],
borderRadius: [0, 0, 0, 0],
},
}));
const avatarData = list.map((it) => ({
name: String(it?.id ?? ''),
value: Number(it?.value) || 0,
symbol: `image://${it?.avatar || '/static/head_null.png'}`,
}));
const maxV = Math.max(...list.map((it) => Number(it?.value) || 0), 1);
const yMax = Math.ceil(maxV * (Number(props.yMaxFactor) || 1.25));
return {
animation: true,
animationDuration: 900,
animationEasing: 'cubicOut',
animationDurationUpdate: 900,
animationEasingUpdate: 'cubicOut',
grid: { left: 0, right: 0, top: 46, bottom: 22, containLabel: false },
xAxis: {
type: 'category',
data: xData,
axisLine: {
show: true,
lineStyle: { color: 'rgba(255,255,255,0.45)', width: 1 },
},
axisTick: { show: false },
axisLabel: {
show: true,
color: 'rgba(255,255,255,0.85)',
fontSize: 10,
margin: 10,
},
},
yAxis: { type: 'value', show: false, min: 0, max: yMax },
series: [
{
id: 'bars',
type: 'bar',
data: barData,
barWidth: 20,
barCategoryGap: 8,
universalTransition: true,
label: {
show: true,
position: 'inside',
color: '#ffffff',
fontSize: 10,
fontWeight: 700,
formatter: (p) => {
const v = Number(p?.value) || 0;
return v ? String(v) : '';
},
},
animationDelay: (idx) => idx * 140,
animationDelayUpdate: (idx) => idx * 140,
},
{
id: 'avatars',
type: 'pictorialBar',
data: avatarData,
symbolSize: [24, 24],
symbolPosition: 'end',
symbolOffset: [0, -28],
z: 10,
universalTransition: true,
animationDelay: (idx) => idx * 140,
animationDelayUpdate: (idx) => idx * 140,
},
],
};
});
</script>
<style scoped lang="scss">
.bar-rank-card {
margin-top: 18rpx;
padding: 12rpx 24rpx 12rpx;
height: 240rpx;
}
.bar-scroll {
width: 100%;
height: 100%;
}
.bar-scroll__inner {
height: 100%;
}
</style>