Files
admin-sjzx/src/components/echarts/MyEchart.vue

322 lines
9.2 KiB
Vue
Raw Normal View History

2024-02-19 13:44:32 +08:00
<template>
2024-11-01 11:17:12 +08:00
<div class="chart">
<div ref="chartRef" class="my-chart" />
</div>
2024-02-19 13:44:32 +08:00
</template>
2024-02-28 15:26:31 +08:00
<script setup lang="ts">
2024-11-01 11:17:12 +08:00
import { onBeforeUnmount, onMounted, ref, defineExpose, watch, nextTick } from 'vue'
2024-02-19 13:44:32 +08:00
// import echarts from './echarts'
import * as echarts from 'echarts' // 全引入
2024-02-26 19:17:08 +08:00
import 'echarts-gl'
2024-05-09 18:00:04 +08:00
import 'echarts-liquidfill'
2024-02-19 13:44:32 +08:00
import 'echarts/lib/component/dataZoom'
2024-02-28 15:26:31 +08:00
import { color, gradeColor3 } from './color'
2024-02-29 19:36:29 +08:00
import { useConfig } from '@/stores/config'
2024-11-08 16:30:12 +08:00
const config = useConfig()
2024-11-01 11:17:12 +08:00
// import { nextTick } from 'process'
2025-07-28 08:53:15 +08:00
const emit = defineEmits(['triggerPoint', 'group'])
2024-02-29 19:36:29 +08:00
color[0] = config.layout.elementUiPrimary[0]
2024-02-19 13:44:32 +08:00
const chartRef = ref<HTMLDivElement>()
2024-11-01 11:17:12 +08:00
const props = defineProps(['options', 'isInterVal', 'pieInterVal'])
2024-02-19 13:44:32 +08:00
let chart: echarts.ECharts | any = null
const resizeHandler = () => {
// 不在视野中的时候不进行resize
2024-02-29 10:50:14 +08:00
if (!chartRef.value) return
if (chartRef.value.offsetHeight == 0) return
2024-02-19 13:44:32 +08:00
chart.getZr().painter.getViewportRoot().style.display = 'none'
requestAnimationFrame(() => {
chart.resize()
chart.getZr().painter.getViewportRoot().style.display = ''
})
}
const initChart = () => {
2024-11-01 11:17:12 +08:00
if (!props.isInterVal && !props.pieInterVal) {
chart?.dispose()
}
// chart?.dispose()
2025-03-13 18:26:03 +08:00
chart = echarts.getInstanceByDom(chartRef.value as HTMLDivElement) || echarts.init(chartRef.value as HTMLDivElement)
2024-02-27 14:18:01 +08:00
const options = {
2024-02-26 16:12:09 +08:00
title: {
left: 'center',
2024-11-01 11:17:12 +08:00
// textStyle: {
color: '#000',
fontSize: 18,
// },
2024-03-19 15:53:01 +08:00
...(props.options?.title || null)
2024-02-26 16:12:09 +08:00
},
tooltip: {
trigger: 'axis',
2025-07-10 16:32:49 +08:00
// axisPointer: {
// type: 'shadow',
// label: {
// color: '#fff',
// fontSize: 16
// }
// },
2024-02-26 16:12:09 +08:00
textStyle: {
color: '#fff',
fontStyle: 'normal',
opacity: 0.35,
fontSize: 14
},
2024-12-11 16:14:08 +08:00
backgroundColor: 'rgba(0,0,0,0.55)',
2024-02-26 16:12:09 +08:00
borderWidth: 0,
2024-11-07 15:23:04 +08:00
confine: true,
2025-05-06 16:38:10 +08:00
formatter: function (params: any) {
2025-05-13 15:26:24 +08:00
let tips = `<strong>${params[0]?.name}</strong></br>` // 标题加粗
params?.forEach((item: any) => {
2025-07-10 16:32:49 +08:00
const value =
item.value === 3.14159 || item.value === 0.14159
? '暂无数据'
: Math.round(item.value * 100) / 100 // 处理特殊值
2025-05-06 16:38:10 +08:00
tips += `<div style=" display: flex;justify-content: space-between;">
<span>${item.marker}
${item.seriesName}:
</span> ${value}
</div>` // 统一格式
})
return tips
},
2024-03-19 15:53:01 +08:00
...(props.options?.tooltip || null)
2024-02-26 16:12:09 +08:00
},
2024-11-01 11:17:12 +08:00
toolbox: {
right: 20,
2025-03-13 18:26:03 +08:00
top: 15,
2024-11-01 11:17:12 +08:00
feature: {
saveAsImage: {
2025-03-13 18:26:03 +08:00
title: '', // 按钮标题
icon: 'path://M892.342857 463.238095l-73.142857-68.266666-258.438095 258.438095V29.257143h-97.52381v624.152381L204.8 394.971429 131.657143 463.238095l380.342857 380.342857zM107.27619 897.219048h804.571429v97.523809H107.27619z' // 自定义图标路径
2024-11-01 11:17:12 +08:00
},
2024-12-11 16:14:08 +08:00
2024-11-07 15:23:04 +08:00
...(props.options?.toolbox?.featureProps || null)
2024-11-01 11:17:12 +08:00
},
2024-11-07 15:23:04 +08:00
emphasis: {
2024-12-11 16:14:08 +08:00
iconStyle: {
borderColor: config.layout.elementUiPrimary[0], // 鼠标悬停时的边框颜色
color: config.layout.elementUiPrimary[0] // 鼠标悬停时的图标颜色
}
},
2024-11-01 11:17:12 +08:00
// },
...(props.options?.toolbox || null)
},
2024-02-26 16:12:09 +08:00
legend: {
2025-03-13 18:26:03 +08:00
right: 50,
top: 25,
2024-02-26 16:12:09 +08:00
itemGap: 10,
itemStyle: {},
2024-11-01 11:17:12 +08:00
// textStyle: {
fontSize: 12,
padding: [2, 0, 0, 0], //[上、右、下、左]
// },
2024-02-26 16:12:09 +08:00
itemWidth: 15,
itemHeight: 10,
2024-03-19 15:53:01 +08:00
...(props.options?.legend || null)
2024-02-26 16:12:09 +08:00
},
2024-02-19 13:44:32 +08:00
grid: {
2025-05-28 08:41:29 +08:00
top: '50px',
2024-04-18 16:03:53 +08:00
left: '30px',
right: '70px',
2024-03-19 15:53:01 +08:00
bottom: props.options?.options?.dataZoom === null ? '10px' : '40px',
2024-03-05 16:21:13 +08:00
containLabel: true,
2024-03-19 15:53:01 +08:00
...(props.options?.grid || null)
2024-02-19 13:44:32 +08:00
},
2024-03-19 15:53:01 +08:00
xAxis: props.options?.xAxis ? handlerXAxis() : null,
yAxis: props.options?.yAxis ? handlerYAxis() : null,
dataZoom: props.options?.dataZoom || [
2024-02-19 13:44:32 +08:00
{
type: 'inside',
height: 13,
start: 0,
bottom: '20px',
2024-02-27 14:18:01 +08:00
end: 100
2024-02-19 13:44:32 +08:00
},
{
start: 0,
height: 13,
bottom: '20px',
2024-02-27 14:18:01 +08:00
end: 100
2024-02-19 13:44:32 +08:00
}
],
2024-03-19 15:53:01 +08:00
color: props.options?.color || color,
series: props.options?.series,
...props.options?.options
2024-02-27 14:18:01 +08:00
}
2024-11-07 15:23:04 +08:00
// console.log(options.series,"获取x轴");
2024-02-27 14:18:01 +08:00
handlerBar(options)
// 处理柱状图
2024-11-07 15:23:04 +08:00
chart.setOption(options, true)
2025-07-28 08:53:15 +08:00
// chart.group = 'group'
emit('group',chart)
2025-03-13 18:26:03 +08:00
// 添加点击事件
chart.on('click', function (params) {
if (params.seriesName == '暂态触发点') {
emit('triggerPoint', params.data)
}
})
setTimeout(() => {
chart.resize()
2024-02-26 19:17:08 +08:00
}, 0)
2024-02-19 13:44:32 +08:00
}
2024-02-27 14:18:01 +08:00
const handlerBar = (options: any) => {
if (Array.isArray(options.series)) {
options.series.forEach((item: any) => {
if (item.type === 'bar') {
item.barMinHeight = 10
2024-04-29 16:37:07 +08:00
item.barMaxWidth = 20
2024-02-27 14:18:01 +08:00
item.itemStyle = Object.assign(
{
color: (params: any) => {
2025-07-10 16:32:49 +08:00
if (params.value == 0 || params.value == 3.14159 || params.value == 0.14159) {
2024-02-27 14:18:01 +08:00
return '#ccc'
} else {
2024-03-19 15:53:01 +08:00
return props.options?.color
? props.options?.color[params.seriesIndex]
2024-02-28 15:26:31 +08:00
: color[params.seriesIndex]
2024-02-27 14:18:01 +08:00
}
}
2024-02-27 14:47:45 +08:00
},
item.itemStyle
2024-02-27 14:18:01 +08:00
)
}
})
}
}
2024-02-19 13:44:32 +08:00
const handlerYAxis = () => {
let temp = {
type: 'value',
2024-03-19 15:53:01 +08:00
nameGap: 15,
2024-02-19 13:44:32 +08:00
nameTextStyle: {
color: '#000'
},
2024-10-10 10:46:10 +08:00
splitNumber: 5,
2024-02-19 13:44:32 +08:00
minInterval: 1,
axisLine: {
show: true,
lineStyle: {
color: '#000'
}
},
axisLabel: {
color: '#000',
2024-11-01 11:17:12 +08:00
fontSize: 14,
formatter: function (value) {
2025-04-28 15:52:40 +08:00
return parseFloat(value.toFixed(1)) // 格式化显示为一位小数
2024-11-01 11:17:12 +08:00
}
2024-02-19 13:44:32 +08:00
},
splitLine: {
lineStyle: {
// 使用深浅的间隔色
color: ['#ccc'],
2024-02-19 13:44:32 +08:00
type: 'dashed',
opacity: 0.5
}
}
}
2024-03-19 15:53:01 +08:00
// props.options?.xAxis 是数组还是对象
if (Array.isArray(props.options?.yAxis)) {
return props.options?.yAxis.map((item: any) => {
2024-02-19 13:44:32 +08:00
return {
2024-04-29 16:37:07 +08:00
...temp,
...item
2024-02-19 13:44:32 +08:00
}
})
} else {
return {
...temp,
2024-03-19 15:53:01 +08:00
...props.options?.yAxis
2024-02-19 13:44:32 +08:00
}
}
}
const handlerXAxis = () => {
let temp = {
type: 'category',
axisTick: { show: false },
2024-11-07 15:23:04 +08:00
nameTextStyle: {
color: '#000'
},
2024-02-19 13:44:32 +08:00
axisLine: {
2024-11-01 11:17:12 +08:00
// lineStyle: {
color: '#000'
// }
2024-02-19 13:44:32 +08:00
},
axisLabel: {
2024-11-01 11:17:12 +08:00
// textStyle: {
fontFamily: 'dinproRegular',
color: '#000',
2025-03-13 18:26:03 +08:00
fontSize: '12'
2024-11-01 11:17:12 +08:00
// }
2025-03-13 18:26:03 +08:00
}
2024-11-07 15:23:04 +08:00
// boundaryGap: false,
2024-02-19 13:44:32 +08:00
}
2024-03-19 15:53:01 +08:00
// props.options?.xAxis 是数组还是对象
if (Array.isArray(props.options?.xAxis)) {
return props.options?.xAxis.map((item: any) => {
2024-02-19 13:44:32 +08:00
return {
2024-02-26 10:43:33 +08:00
...temp,
...item
2024-02-19 13:44:32 +08:00
}
})
} else {
return {
...temp,
2024-03-19 15:53:01 +08:00
...props.options?.xAxis
2024-02-19 13:44:32 +08:00
}
}
}
2024-11-01 11:17:12 +08:00
let throttle: ReturnType<typeof setTimeout>
// 动态计算table高度
const resizeObserver = new ResizeObserver(entries => {
for (const entry of entries) {
if (throttle) {
clearTimeout(throttle)
}
throttle = setTimeout(() => {
resizeHandler()
}, 100)
}
})
2025-03-13 18:26:03 +08:00
const setOptions = (options: any) => {
chart.setOption(options)
}
const getChart = () => {
return chart
}
2024-02-19 13:44:32 +08:00
onMounted(() => {
initChart()
resizeObserver.observe(chartRef.value!)
2024-02-19 13:44:32 +08:00
})
2025-03-13 18:26:03 +08:00
defineExpose({ initChart, setOptions, getChart })
2024-02-19 13:44:32 +08:00
onBeforeUnmount(() => {
resizeObserver.unobserve(chartRef.value!)
2024-02-19 13:44:32 +08:00
chart?.dispose()
})
watch(
() => props.options,
(newVal, oldVal) => {
initChart()
}
)
</script>
2024-02-28 15:26:31 +08:00
<style lang="scss" scoped>
2024-11-01 11:17:12 +08:00
.chart {
2024-02-19 13:44:32 +08:00
width: 100%;
2024-11-01 11:17:12 +08:00
height: 100%;
position: relative;
.el-button {
position: absolute;
right: 0px;
top: -60px;
}
.my-chart {
height: 100%;
width: 100%;
}
2024-02-19 13:44:32 +08:00
}
</style>