Add admin analytics trend views

This commit is contained in:
orion committed 2026-06-05 11:13:50 +08:00
1 parent a58fc43d94
commit 105efc40d5
8 files changed
+869 -45

No files matched your search

+309
View File
@@ -0,0 +1,309 @@
<template>
<div class="relative overflow-hidden rounded-lg border" :class="panelClass">
<canvas
ref="canvasRef"
class="block h-80 min-h-80 w-full touch-none select-none md:h-72 md:min-h-72"
@wheel.prevent="handleWheel"
@pointerdown="handlePointerDown"
@pointermove="handlePointerMove"
@pointerup="handlePointerUp"
@pointercancel="handlePointerCancel"
@pointerleave="handlePointerLeave"
@dblclick.prevent="resetWindow"
/>
<div
v-if="tooltip"
class="pointer-events-none absolute z-10 min-w-32 rounded-lg border px-3 py-2 text-xs shadow-lg"
:class="tooltipClass"
:style="{
left: `${tooltip.left}px`,
top: `${tooltip.top}px`,
transform: 'translate(-50%, -112%)'
}"
>
<p class="font-semibold">{{ tooltip.date }}</p>
<p>下载:{{ tooltip.downloads }}</p>
<p>上传:{{ tooltip.uploads }}</p>
</div>
</div>
</template>
<script setup lang="ts">
import { computed, nextTick, onBeforeUnmount, onMounted, ref, watch } from 'vue'
import type { AnalyticsDailyRow } from '@/types'
const props = defineProps<{
rows: AnalyticsDailyRow[]
isDarkMode: boolean
}>()
const emit = defineEmits<{
windowChange: [text: string]
}>()
type DragState = {
lastX: number
time: number
}
type TooltipState = {
left: number
top: number
date: string
downloads: number
uploads: number
}
const canvasRef = ref<HTMLCanvasElement | null>(null)
const offset = ref(0)
const windowSize = ref(30)
const drag = ref<DragState | null>(null)
const hoverX = ref<number | null>(null)
const velocity = ref(0)
const rafId = ref<number | null>(null)
const tooltip = ref<TooltipState | null>(null)
const panelClass = computed(() =>
props.isDarkMode ? 'border-gray-700 bg-gray-900/30' : 'border-gray-100 bg-gray-50'
)
const tooltipClass = computed(() =>
props.isDarkMode
? 'border-gray-700 bg-gray-800 text-gray-100'
: 'border-gray-200 bg-white text-gray-900'
)
const getDefaultWindow = () => {
const width = canvasRef.value?.getBoundingClientRect().width || window.innerWidth
return Math.max(2, Math.min(props.rows.length || 2, width < 640 ? 14 : 60))
}
const clampState = () => {
const total = props.rows.length
windowSize.value = Math.max(
2,
Math.min(windowSize.value || getDefaultWindow(), Math.max(2, total || 2))
)
offset.value = Math.max(0, Math.min(offset.value, Math.max(0, total - windowSize.value)))
}
const scheduleDraw = () => {
if (rafId.value !== null) return
rafId.value = window.requestAnimationFrame(() => {
rafId.value = null
draw()
})
}
const draw = () => {
const canvas = canvasRef.value
if (!canvas) return
const ctx = canvas.getContext('2d')
if (!ctx) return
const rect = canvas.getBoundingClientRect()
if (!rect.width || !rect.height) return
clampState()
const dpr = window.devicePixelRatio || 1
canvas.width = rect.width * dpr
canvas.height = rect.height * dpr
ctx.setTransform(dpr, 0, 0, dpr, 0, 0)
ctx.clearRect(0, 0, rect.width, rect.height)
const total = props.rows.length
const start = Math.max(0, Math.floor(offset.value) - 1)
const end = Math.min(total, Math.ceil(offset.value + windowSize.value) + 2)
const visible = props.rows.slice(start, end)
const mobile = rect.width < 640
const padLeft = mobile ? 34 : 42
const padRight = mobile ? 16 : 20
const padTop = 26
const padBottom = mobile ? 42 : 36
const width = Math.max(1, rect.width - padLeft - padRight)
const height = Math.max(1, rect.height - padTop - padBottom)
const max = Math.max(1, ...visible.map((row) => Number(row.downloads || 0)))
const muted = props.isDarkMode ? '#9ca3af' : '#6b7280'
const border = props.isDarkMode ? '#374151' : '#e5e7eb'
ctx.strokeStyle = border
ctx.lineWidth = 1
ctx.fillStyle = muted
ctx.font = '12px system-ui'
for (let i = 0; i < 4; i += 1) {
const y = padTop + (height * i) / 3
ctx.beginPath()
ctx.moveTo(padLeft, y)
ctx.lineTo(padLeft + width, y)
ctx.stroke()
ctx.fillText(String(Math.round(max * (1 - i / 3))), 4, y + 4)
}
const denominator = Math.max(1, windowSize.value - 1)
const points = visible
.map((row, index) => {
const sourceIndex = start + index
return {
x: padLeft + ((sourceIndex - offset.value) / denominator) * width,
y: padTop + height - (Number(row.downloads || 0) / max) * height,
row
}
})
.filter((point) => point.x >= padLeft - 50 && point.x <= padLeft + width + 50)
ctx.strokeStyle = '#4f46e5'
ctx.lineWidth = 3
ctx.lineCap = 'round'
ctx.lineJoin = 'round'
ctx.beginPath()
points.forEach((point, index) => {
if (index) ctx.lineTo(point.x, point.y)
else ctx.moveTo(point.x, point.y)
})
ctx.stroke()
ctx.fillStyle = '#4f46e5'
points.forEach((point) => {
ctx.beginPath()
ctx.arc(point.x, point.y, mobile ? 4.5 : 3.5, 0, Math.PI * 2)
ctx.fill()
})
const leftRow = props.rows[Math.max(0, Math.round(offset.value))]
const rightRow = props.rows[Math.min(total - 1, Math.round(offset.value + windowSize.value - 1))]
if (total) {
ctx.fillStyle = muted
ctx.fillText(leftRow?.date || '', padLeft, rect.height - 10)
const last = rightRow?.date || ''
ctx.fillText(
last,
Math.max(padLeft, rect.width - padRight - ctx.measureText(last).width),
rect.height - 10
)
}
emit(
'windowChange',
total
? `${Math.floor(offset.value) + 1}-${Math.min(total, Math.ceil(offset.value + windowSize.value))} / ${total}`
: '无数据'
)
if (hoverX.value !== null && points.length) {
const nearest = points.reduce((current, point) =>
Math.abs(point.x - hoverX.value!) < Math.abs(current.x - hoverX.value!) ? point : current
)
tooltip.value = {
left: Math.min(rect.width - 80, Math.max(80, nearest.x)),
top: Math.max(46, nearest.y),
date: nearest.row.date,
downloads: Number(nearest.row.downloads || 0),
uploads: Number(nearest.row.uploads || 0)
}
ctx.fillStyle = props.isDarkMode ? '#111827' : '#fff'
ctx.strokeStyle = '#4f46e5'
ctx.lineWidth = 3
ctx.beginPath()
ctx.arc(nearest.x, nearest.y, mobile ? 8 : 6, 0, Math.PI * 2)
ctx.fill()
ctx.stroke()
} else {
tooltip.value = null
}
}
const pan = (deltaX: number) => {
const rect = canvasRef.value?.getBoundingClientRect()
if (!rect) return
offset.value += (-deltaX / Math.max(1, rect.width)) * Math.max(1, windowSize.value - 1) * 1.15
clampState()
scheduleDraw()
}
const zoom = (factor: number, center = 0.5) => {
clampState()
const minWindow = Math.min(props.rows.length || 2, window.innerWidth < 640 ? 5 : 7)
const next = Math.max(minWindow, Math.min(props.rows.length || 2, windowSize.value * factor))
const anchor = offset.value + windowSize.value * center
windowSize.value = next
offset.value = anchor - next * center
clampState()
scheduleDraw()
}
const applyInertia = () => {
if (Math.abs(velocity.value) < 0.02) return
pan(velocity.value)
velocity.value *= 0.92
window.requestAnimationFrame(applyInertia)
}
const handleWheel = (event: WheelEvent) => {
const rect = canvasRef.value?.getBoundingClientRect()
zoom(event.deltaY > 0 ? 1.16 : 0.86, rect ? (event.clientX - rect.left) / rect.width : 0.5)
}
const handlePointerDown = (event: PointerEvent) => {
canvasRef.value?.setPointerCapture(event.pointerId)
velocity.value = 0
drag.value = { lastX: event.clientX, time: performance.now() }
}
const handlePointerMove = (event: PointerEvent) => {
const rect = canvasRef.value?.getBoundingClientRect()
if (rect) hoverX.value = event.clientX - rect.left
if (!drag.value) {
scheduleDraw()
return
}
const now = performance.now()
const deltaX = event.clientX - drag.value.lastX
const deltaTime = Math.max(16, now - drag.value.time)
velocity.value = deltaX * (16 / deltaTime)
pan(deltaX)
drag.value = { lastX: event.clientX, time: now }
}
const handlePointerUp = () => {
drag.value = null
window.requestAnimationFrame(applyInertia)
}
const handlePointerCancel = () => {
drag.value = null
}
const handlePointerLeave = () => {
drag.value = null
hoverX.value = null
scheduleDraw()
}
const resetWindow = () => {
windowSize.value = getDefaultWindow()
offset.value = Math.max(0, props.rows.length - windowSize.value)
scheduleDraw()
}
const handleResize = () => scheduleDraw()
watch(
() => props.rows,
async () => {
await nextTick()
resetWindow()
},
{ deep: true }
)
onMounted(async () => {
await nextTick()
resetWindow()
window.addEventListener('resize', handleResize)
})
onBeforeUnmount(() => {
window.removeEventListener('resize', handleResize)
if (rafId.value !== null) window.cancelAnimationFrame(rafId.value)
})
defineExpose({ zoom, resetWindow })
</script>