495 lines
11 KiB
Vue
495 lines
11 KiB
Vue
<template>
|
|
<div class="statistics-chart">
|
|
<div class="chart-header">
|
|
<div class="chart-title">{{ title }}</div>
|
|
<div class="chart-actions">
|
|
<el-radio-group v-model="currentChartType" @change="handleChartTypeChange">
|
|
<el-radio-button label="bar">柱状图</el-radio-button>
|
|
<el-radio-button label="pie">饼图</el-radio-button>
|
|
<el-radio-button label="line">折线图</el-radio-button>
|
|
<el-radio-button label="doughnut">环形图</el-radio-button>
|
|
</el-radio-group>
|
|
</div>
|
|
</div>
|
|
<div class="chart-container">
|
|
<div ref="chartRef" class="chart-wrapper" v-loading="loading"></div>
|
|
<div v-if="!data || data.length === 0" class="chart-empty">
|
|
<el-empty description="暂无数据">
|
|
<el-icon class="empty-icon"><DataAnalysis /></el-icon>
|
|
</el-empty>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</template>
|
|
|
|
<script setup lang="ts">
|
|
import { ref, onMounted, onUnmounted, watch, nextTick } from 'vue'
|
|
import * as echarts from 'echarts'
|
|
|
|
// Props
|
|
interface ChartData {
|
|
label: string
|
|
value: number
|
|
percentage: number
|
|
}
|
|
|
|
interface Props {
|
|
title?: string
|
|
data: ChartData[]
|
|
chartType?: 'bar' | 'pie' | 'line' | 'doughnut'
|
|
loading?: boolean
|
|
height?: number
|
|
}
|
|
|
|
const props = withDefaults(defineProps<Props>(), {
|
|
title: '统计图表',
|
|
data: () => [],
|
|
chartType: 'bar',
|
|
loading: false,
|
|
height: 400
|
|
})
|
|
|
|
// Emits
|
|
const emit = defineEmits<{
|
|
'chart-click': [data: ChartData]
|
|
'chart-type-change': [type: string]
|
|
}>()
|
|
|
|
// 响应式数据
|
|
const chartRef = ref<HTMLElement>()
|
|
const chartInstance = ref<echarts.ECharts>()
|
|
const currentChartType = ref(props.chartType)
|
|
|
|
// 图表配色方案
|
|
const colorScheme = [
|
|
'#5d87ff', '#49beff', '#ff9a56', '#ffad56',
|
|
'#4facfe', '#00f2fe', '#a8edea', '#fed6e3',
|
|
'#667eea', '#764ba2', '#f093fb', '#f5576c',
|
|
'#4facfe', '#00f2fe', '#43e97b', '#38f9d7'
|
|
]
|
|
|
|
// 监听器
|
|
watch(() => props.data, () => {
|
|
renderChart()
|
|
}, { deep: true })
|
|
|
|
watch(() => props.loading, (newVal) => {
|
|
if (!newVal) {
|
|
nextTick(() => {
|
|
renderChart()
|
|
})
|
|
}
|
|
})
|
|
|
|
// 方法定义
|
|
const initChart = () => {
|
|
if (!chartRef.value) return
|
|
|
|
chartInstance.value = echarts.init(chartRef.value)
|
|
|
|
// 监听图表点击事件
|
|
chartInstance.value.on('click', (params: any) => {
|
|
const clickedData = props.data.find(item => item.label === params.name)
|
|
if (clickedData) {
|
|
emit('chart-click', clickedData)
|
|
}
|
|
})
|
|
|
|
// 监听窗口大小变化
|
|
window.addEventListener('resize', handleResize)
|
|
}
|
|
|
|
const handleResize = () => {
|
|
if (chartInstance.value) {
|
|
chartInstance.value.resize()
|
|
}
|
|
}
|
|
|
|
const handleChartTypeChange = (type: string) => {
|
|
currentChartType.value = type as any
|
|
emit('chart-type-change', type)
|
|
renderChart()
|
|
}
|
|
|
|
const renderChart = () => {
|
|
if (!chartInstance.value || !props.data || props.data.length === 0) {
|
|
return
|
|
}
|
|
|
|
const option = generateChartOption()
|
|
chartInstance.value.setOption(option, true)
|
|
}
|
|
|
|
const generateChartOption = (): echarts.EChartsCoreOption => {
|
|
const labels = props.data.map(item => item.label)
|
|
const values = props.data.map(item => item.value)
|
|
|
|
const baseOption = {
|
|
tooltip: {
|
|
trigger: 'item',
|
|
backgroundColor: 'rgba(0, 0, 0, 0.8)',
|
|
borderColor: 'rgba(0, 0, 0, 0.8)',
|
|
textStyle: {
|
|
color: '#fff'
|
|
}
|
|
},
|
|
legend: {
|
|
show: currentChartType.value === 'pie' || currentChartType.value === 'doughnut',
|
|
orient: 'vertical',
|
|
right: 10,
|
|
top: 20,
|
|
bottom: 20,
|
|
textStyle: {
|
|
color: '#606266'
|
|
}
|
|
},
|
|
grid: {
|
|
left: '3%',
|
|
right: '4%',
|
|
bottom: '3%',
|
|
containLabel: true
|
|
}
|
|
}
|
|
|
|
switch (currentChartType.value) {
|
|
case 'pie':
|
|
return {
|
|
...baseOption,
|
|
tooltip: {
|
|
...baseOption.tooltip,
|
|
formatter: '{a} <br/>{b}: {c} ({d}%)'
|
|
},
|
|
series: [{
|
|
name: props.title,
|
|
type: 'pie',
|
|
radius: '60%',
|
|
center: ['40%', '50%'],
|
|
data: props.data.map((item, index) => ({
|
|
name: item.label,
|
|
value: item.value,
|
|
itemStyle: {
|
|
color: colorScheme[index % colorScheme.length]
|
|
}
|
|
})),
|
|
emphasis: {
|
|
itemStyle: {
|
|
shadowBlur: 10,
|
|
shadowOffsetX: 0,
|
|
shadowColor: 'rgba(0, 0, 0, 0.5)'
|
|
}
|
|
},
|
|
label: {
|
|
show: true,
|
|
formatter: '{b}: {d}%'
|
|
}
|
|
}]
|
|
}
|
|
|
|
case 'doughnut':
|
|
return {
|
|
...baseOption,
|
|
tooltip: {
|
|
...baseOption.tooltip,
|
|
formatter: '{a} <br/>{b}: {c} ({d}%)'
|
|
},
|
|
series: [{
|
|
name: props.title,
|
|
type: 'pie',
|
|
radius: ['40%', '70%'],
|
|
center: ['40%', '50%'],
|
|
data: props.data.map((item, index) => ({
|
|
name: item.label,
|
|
value: item.value,
|
|
itemStyle: {
|
|
color: colorScheme[index % colorScheme.length]
|
|
}
|
|
})),
|
|
emphasis: {
|
|
itemStyle: {
|
|
shadowBlur: 10,
|
|
shadowOffsetX: 0,
|
|
shadowColor: 'rgba(0, 0, 0, 0.5)'
|
|
}
|
|
},
|
|
label: {
|
|
show: true,
|
|
formatter: '{b}: {d}%'
|
|
}
|
|
}]
|
|
}
|
|
|
|
case 'line':
|
|
return {
|
|
...baseOption,
|
|
tooltip: {
|
|
trigger: 'axis',
|
|
backgroundColor: 'rgba(0, 0, 0, 0.8)',
|
|
borderColor: 'rgba(0, 0, 0, 0.8)',
|
|
textStyle: {
|
|
color: '#fff'
|
|
}
|
|
},
|
|
xAxis: {
|
|
type: 'category',
|
|
data: labels,
|
|
axisLabel: {
|
|
color: '#606266',
|
|
interval: 0,
|
|
rotate: labels.length > 6 ? 45 : 0
|
|
},
|
|
axisLine: {
|
|
lineStyle: {
|
|
color: '#e4e7ed'
|
|
}
|
|
}
|
|
},
|
|
yAxis: {
|
|
type: 'value',
|
|
axisLabel: {
|
|
color: '#606266'
|
|
},
|
|
axisLine: {
|
|
lineStyle: {
|
|
color: '#e4e7ed'
|
|
}
|
|
},
|
|
splitLine: {
|
|
lineStyle: {
|
|
color: '#f0f2f5'
|
|
}
|
|
}
|
|
},
|
|
series: [{
|
|
name: props.title,
|
|
type: 'line',
|
|
data: values,
|
|
smooth: true,
|
|
symbol: 'circle',
|
|
symbolSize: 6,
|
|
lineStyle: {
|
|
color: colorScheme[0],
|
|
width: 3
|
|
},
|
|
itemStyle: {
|
|
color: colorScheme[0]
|
|
},
|
|
areaStyle: {
|
|
color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
|
|
{ offset: 0, color: colorScheme[0] + '40' },
|
|
{ offset: 1, color: colorScheme[0] + '10' }
|
|
])
|
|
}
|
|
}]
|
|
}
|
|
|
|
default: // bar
|
|
return {
|
|
...baseOption,
|
|
tooltip: {
|
|
trigger: 'axis',
|
|
backgroundColor: 'rgba(0, 0, 0, 0.8)',
|
|
borderColor: 'rgba(0, 0, 0, 0.8)',
|
|
textStyle: {
|
|
color: '#fff'
|
|
}
|
|
},
|
|
xAxis: {
|
|
type: 'category',
|
|
data: labels,
|
|
axisLabel: {
|
|
color: '#606266',
|
|
interval: 0,
|
|
rotate: labels.length > 6 ? 45 : 0
|
|
},
|
|
axisLine: {
|
|
lineStyle: {
|
|
color: '#e4e7ed'
|
|
}
|
|
}
|
|
},
|
|
yAxis: {
|
|
type: 'value',
|
|
axisLabel: {
|
|
color: '#606266'
|
|
},
|
|
axisLine: {
|
|
lineStyle: {
|
|
color: '#e4e7ed'
|
|
}
|
|
},
|
|
splitLine: {
|
|
lineStyle: {
|
|
color: '#f0f2f5'
|
|
}
|
|
}
|
|
},
|
|
series: [{
|
|
name: props.title,
|
|
type: 'bar',
|
|
data: values.map((value, index) => ({
|
|
value,
|
|
itemStyle: {
|
|
color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
|
|
{ offset: 0, color: colorScheme[index % colorScheme.length] },
|
|
{ offset: 1, color: colorScheme[index % colorScheme.length] + '80' }
|
|
])
|
|
}
|
|
})),
|
|
barWidth: '60%',
|
|
emphasis: {
|
|
itemStyle: {
|
|
shadowBlur: 10,
|
|
shadowOffsetX: 0,
|
|
shadowColor: 'rgba(0, 0, 0, 0.5)'
|
|
}
|
|
}
|
|
}]
|
|
}
|
|
}
|
|
}
|
|
|
|
const exportChart = (type: 'png' | 'jpg' | 'svg' = 'png') => {
|
|
if (!chartInstance.value) return null
|
|
|
|
return chartInstance.value.getDataURL({
|
|
type: type,
|
|
pixelRatio: 2,
|
|
backgroundColor: '#fff'
|
|
})
|
|
}
|
|
|
|
// 暴露方法给父组件
|
|
defineExpose({
|
|
exportChart,
|
|
resize: handleResize
|
|
})
|
|
|
|
// 生命周期
|
|
onMounted(() => {
|
|
nextTick(() => {
|
|
initChart()
|
|
renderChart()
|
|
})
|
|
})
|
|
|
|
onUnmounted(() => {
|
|
if (chartInstance.value) {
|
|
chartInstance.value.dispose()
|
|
}
|
|
window.removeEventListener('resize', handleResize)
|
|
})
|
|
</script>
|
|
|
|
<style scoped>
|
|
.statistics-chart {
|
|
width: 100%;
|
|
height: 100%;
|
|
}
|
|
|
|
.chart-header {
|
|
display: flex;
|
|
justify-content: space-between;
|
|
align-items: center;
|
|
margin-bottom: 16px;
|
|
}
|
|
|
|
.chart-title {
|
|
font-size: 16px;
|
|
font-weight: 600;
|
|
color: #1e293b;
|
|
display: flex;
|
|
align-items: center;
|
|
}
|
|
|
|
.chart-title::before {
|
|
content: '';
|
|
width: 4px;
|
|
height: 16px;
|
|
background: linear-gradient(135deg, rgb(93, 135, 255), rgb(73, 190, 255));
|
|
border-radius: 2px;
|
|
margin-right: 8px;
|
|
}
|
|
|
|
.chart-actions {
|
|
display: flex;
|
|
align-items: center;
|
|
}
|
|
|
|
.chart-container {
|
|
position: relative;
|
|
width: 100%;
|
|
height: 400px;
|
|
}
|
|
|
|
.chart-wrapper {
|
|
width: 100%;
|
|
height: 100%;
|
|
}
|
|
|
|
.chart-empty {
|
|
position: absolute;
|
|
top: 0;
|
|
left: 0;
|
|
right: 0;
|
|
bottom: 0;
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: center;
|
|
background: rgba(255, 255, 255, 0.9);
|
|
}
|
|
|
|
.empty-icon {
|
|
font-size: 48px;
|
|
color: #c0c4cc;
|
|
margin-bottom: 16px;
|
|
}
|
|
|
|
/* 响应式设计 */
|
|
@media (max-width: 768px) {
|
|
.chart-header {
|
|
flex-direction: column;
|
|
gap: 12px;
|
|
align-items: flex-start;
|
|
}
|
|
|
|
.chart-actions {
|
|
width: 100%;
|
|
overflow-x: auto;
|
|
}
|
|
|
|
.chart-container {
|
|
height: 300px;
|
|
}
|
|
}
|
|
|
|
/* 图表类型切换按钮样式 */
|
|
.chart-actions :deep(.el-radio-button__inner) {
|
|
border-radius: 6px;
|
|
border: 1px solid #e4e7ed;
|
|
background: #ffffff;
|
|
color: #606266;
|
|
transition: all 0.3s ease;
|
|
}
|
|
|
|
.chart-actions :deep(.el-radio-button__inner:hover) {
|
|
border-color: rgba(93, 135, 255, 0.6);
|
|
color: rgb(93, 135, 255);
|
|
}
|
|
|
|
.chart-actions :deep(.el-radio-button.is-active .el-radio-button__inner) {
|
|
background: linear-gradient(135deg, rgb(93, 135, 255), rgb(73, 190, 255));
|
|
border-color: rgb(93, 135, 255);
|
|
color: white;
|
|
box-shadow: 0 2px 8px rgba(93, 135, 255, 0.3);
|
|
}
|
|
|
|
.chart-actions :deep(.el-radio-group) {
|
|
display: flex;
|
|
gap: 4px;
|
|
}
|
|
|
|
.chart-actions :deep(.el-radio-button) {
|
|
margin: 0;
|
|
}
|
|
</style>
|