|
|
|
|
<template>
|
|
|
|
|
<div ref="chartContainer" class="bar-chart-minute-container"></div>
|
|
|
|
|
</template>
|
|
|
|
|
|
|
|
|
|
<script setup lang="ts">
|
|
|
|
|
import { ref, onMounted, onBeforeUnmount, watch } from 'vue'
|
|
|
|
|
import * as echarts from 'echarts'
|
|
|
|
|
|
|
|
|
|
// 定义组件属性
|
|
|
|
|
interface Props {
|
|
|
|
|
chartData?: Array<{ name: string; value: number }>
|
|
|
|
|
title?: string
|
|
|
|
|
color?: string
|
|
|
|
|
magnifyMode?: boolean
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const props = withDefaults(defineProps<Props>(), {
|
|
|
|
|
chartData: () => [],
|
|
|
|
|
title: '',
|
|
|
|
|
color: '#1296DB', // 默认使用蓝色
|
|
|
|
|
magnifyMode: false
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// 图表容器引用和实例
|
|
|
|
|
const chartContainer = ref<HTMLDivElement | null>(null)
|
|
|
|
|
let chartInstance: echarts.ECharts | null = null
|
|
|
|
|
|
|
|
|
|
// 初始化图表
|
|
|
|
|
const initChart = () => {
|
|
|
|
|
if (chartContainer.value) {
|
|
|
|
|
chartInstance = echarts.init(chartContainer.value)
|
|
|
|
|
updateChart()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 更新图表
|
|
|
|
|
const updateChart = () => {
|
|
|
|
|
if (!chartInstance || !props.chartData.length) return
|
|
|
|
|
|
|
|
|
|
const option = {
|
|
|
|
|
title: {
|
|
|
|
|
// text: props.title,
|
|
|
|
|
textStyle: {
|
|
|
|
|
color: '#fff',
|
|
|
|
|
fontSize: 14
|
|
|
|
|
},
|
|
|
|
|
left: 'center'
|
|
|
|
|
},
|
|
|
|
|
tooltip: {
|
|
|
|
|
trigger: 'axis',
|
|
|
|
|
axisPointer: {
|
|
|
|
|
type: 'shadow'
|
|
|
|
|
},
|
|
|
|
|
backgroundColor: 'rgba(0, 0, 0, 0.7)',
|
|
|
|
|
borderColor: 'rgba(30, 120, 255, 0.4)',
|
|
|
|
|
borderWidth: 1,
|
|
|
|
|
textStyle: {
|
|
|
|
|
color: '#fff'
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
grid: {
|
|
|
|
|
left: '1%',
|
|
|
|
|
right: '1%',
|
|
|
|
|
top: '10%',
|
|
|
|
|
bottom: '1%',
|
|
|
|
|
containLabel: true
|
|
|
|
|
},
|
|
|
|
|
xAxis: {
|
|
|
|
|
type: 'category',
|
|
|
|
|
data: props.chartData.map(item => item.name),
|
|
|
|
|
axisLine: {
|
|
|
|
|
lineStyle: {
|
|
|
|
|
color: 'rgba(255, 255, 255, 0.3)'
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
axisLabel: {
|
|
|
|
|
color: 'rgba(255, 255, 255, 0.7)',
|
|
|
|
|
rotate: 0,
|
|
|
|
|
interval: Math.ceil(props.chartData.length / 24), // 每小时显示一个标签
|
|
|
|
|
formatter: (value: string) => {
|
|
|
|
|
// 只显示小时:00:00的标签
|
|
|
|
|
if (value.endsWith(':00:00')) {
|
|
|
|
|
return value.split(':')[0] + '时'
|
|
|
|
|
}
|
|
|
|
|
return ''
|
|
|
|
|
},
|
|
|
|
|
margin: 10
|
|
|
|
|
},
|
|
|
|
|
axisTick: {
|
|
|
|
|
interval: Math.ceil(props.chartData.length / 24)
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
yAxis: {
|
|
|
|
|
type: 'value',
|
|
|
|
|
axisLine: {
|
|
|
|
|
lineStyle: {
|
|
|
|
|
color: 'rgba(255, 255, 255, 0.3)'
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
splitLine: {
|
|
|
|
|
lineStyle: {
|
|
|
|
|
color: 'rgba(255, 255, 255, 0.1)'
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
axisLabel: {
|
|
|
|
|
color: 'rgba(255, 255, 255, 0.7)'
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
series: [
|
|
|
|
|
{
|
|
|
|
|
type: 'bar',
|
|
|
|
|
data: props.chartData.map(item => item.value),
|
|
|
|
|
barWidth: props.magnifyMode ? 100 : 25, // 大屏模式下增大柱子宽度
|
|
|
|
|
itemStyle: {
|
|
|
|
|
color: {
|
|
|
|
|
type: 'linear',
|
|
|
|
|
x: 0,
|
|
|
|
|
y: 0,
|
|
|
|
|
x2: 0,
|
|
|
|
|
y2: 1,
|
|
|
|
|
colorStops: [
|
|
|
|
|
{
|
|
|
|
|
offset: 0,
|
|
|
|
|
color: props.color // 使用传入的颜色
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
offset: 1,
|
|
|
|
|
color: props.color + '80' // 传入的颜色带透明度
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
borderRadius: [1, 1, 0, 0]
|
|
|
|
|
},
|
|
|
|
|
label: {
|
|
|
|
|
show: true,
|
|
|
|
|
position: 'top',
|
|
|
|
|
color: 'rgba(255, 255, 255, 0.7)',
|
|
|
|
|
fontSize: props.magnifyMode ? 32 : 8,
|
|
|
|
|
formatter: '+{c}',
|
|
|
|
|
labelLayout: {
|
|
|
|
|
hideOverlap: true
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
emphasis: {
|
|
|
|
|
itemStyle: {
|
|
|
|
|
color: props.color // 使用传入的颜色高亮
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
chartInstance.setOption(option)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 监听数据变化
|
|
|
|
|
watch(
|
|
|
|
|
() => props.chartData,
|
|
|
|
|
() => {
|
|
|
|
|
updateChart()
|
|
|
|
|
},
|
|
|
|
|
{ deep: true }
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// 窗口大小改变时重绘图表
|
|
|
|
|
const handleResize = () => {
|
|
|
|
|
if (chartInstance) {
|
|
|
|
|
chartInstance.resize()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 组件挂载时初始化图表
|
|
|
|
|
onMounted(() => {
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
initChart()
|
|
|
|
|
}, 200)
|
|
|
|
|
window.addEventListener('resize', handleResize)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// 组件销毁前清理资源
|
|
|
|
|
onBeforeUnmount(() => {
|
|
|
|
|
window.removeEventListener('resize', handleResize)
|
|
|
|
|
if (chartInstance) {
|
|
|
|
|
chartInstance.dispose()
|
|
|
|
|
chartInstance = null
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
<style scoped>
|
|
|
|
|
.bar-chart-minute-container {
|
|
|
|
|
width: 100%;
|
|
|
|
|
height: 100%;
|
|
|
|
|
/* min-height: 200px; */
|
|
|
|
|
}
|
|
|
|
|
</style>
|