Files
X-Financial/web/src/components/charts/BarChart.vue

172 lines
3.6 KiB
Vue
Raw Normal View History

<template>
<div class="bar-chart">
<div class="rank-labels">
<div v-for="(item, idx) in items" :key="item.name" class="rank-label">
<span class="rank-badge" :class="medalClass(idx)">
<svg v-if="idx < 3" width="18" height="18" viewBox="0 0 18 18">
<circle cx="9" cy="9" r="8" :fill="medalFill(idx)" />
<text x="9" y="13" text-anchor="middle" fill="#fff" font-size="10" font-weight="700">{{ idx + 1 }}</text>
</svg>
<template v-else>{{ idx + 1 }}</template>
</span>
<span class="rank-name">{{ item.name || item.shortName }}</span>
</div>
</div>
<div class="chart-area">
<Bar :data="chartData" :options="chartOptions" />
</div>
</div>
</template>
<script setup>
import { computed } from 'vue'
import { Bar } from 'vue-chartjs'
import {
Chart as ChartJS,
CategoryScale,
LinearScale,
BarElement,
Tooltip
} from 'chart.js'
ChartJS.register(CategoryScale, LinearScale, BarElement, Tooltip)
const props = defineProps({
items: { type: Array, required: true }
})
const medalClass = (idx) => {
if (idx === 0) return 'gold'
if (idx === 1) return 'silver'
if (idx === 2) return 'bronze'
return ''
}
const medalFill = (idx) => {
if (idx === 0) return '#f59e0b'
if (idx === 1) return '#94a3b8'
if (idx === 2) return '#cd7f32'
return '#94a3b8'
}
const formatValue = (value) => {
if (value >= 1_000_000) return `¥${(value / 1_000_000).toFixed(1)}M`
if (value >= 1_000) return `¥${(value / 1_000).toFixed(1)}K`
return `¥${value}`
}
const chartData = computed(() => ({
labels: props.items.map((i) => i.name || i.shortName),
datasets: [{
data: props.items.map((i) => i.value || i.amount),
backgroundColor: props.items.map((i) => i.color),
borderRadius: 6,
borderSkipped: false,
barPercentage: 0.7,
categoryPercentage: 0.85
}]
}))
const chartOptions = {
indexAxis: 'y',
responsive: true,
maintainAspectRatio: false,
layout: {
padding: { left: 0, right: 12 }
},
plugins: {
legend: { display: false },
tooltip: {
backgroundColor: 'rgba(255,255,255,0.96)',
titleColor: '#1e293b',
bodyColor: '#64748b',
borderColor: '#e2e8f0',
borderWidth: 1,
padding: 10,
boxPadding: 4,
cornerRadius: 6,
callbacks: {
title: () => '',
label: (ctx) => ` ${formatValue(ctx.parsed.x)}`
}
}
},
scales: {
x: {
beginAtZero: true,
grid: {
color: '#f1f5f9',
drawTicks: false
},
ticks: {
color: '#94a3b8',
font: { size: 11 },
padding: 4,
callback: (value) => formatValue(value)
},
border: { display: false }
},
y: {
grid: { display: false },
border: { display: false },
ticks: { display: false }
}
}
}
</script>
<style scoped>
.bar-chart {
display: flex;
width: 100%;
gap: 8px;
}
.rank-labels {
flex: 0 0 auto;
display: flex;
flex-direction: column;
justify-content: space-around;
padding: 6px 0;
}
.rank-label {
display: flex;
align-items: center;
gap: 6px;
height: 34px;
white-space: nowrap;
}
.rank-badge {
width: 20px;
height: 20px;
display: grid;
place-items: center;
border-radius: 999px;
font-size: 11px;
font-weight: 600;
color: #fff;
}
.rank-badge:not(.gold):not(.silver):not(.bronze) {
background: #cbd5e1;
}
.rank-badge svg {
display: block;
}
.rank-name {
color: #475569;
font-size: 13px;
font-weight: 500;
}
.chart-area {
flex: 1;
min-width: 0;
position: relative;
height: 240px;
}
</style>