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

289 lines
7.5 KiB
Vue
Raw Normal View History

2023-12-26 14:15:05 +08:00
<template>
2024-06-24 14:38:42 +08:00
<div class="chart">
<div ref="chartRef" class="my-chart" />
</div>
2023-12-26 14:15:05 +08:00
</template>
<script setup lang="ts">
2024-06-27 09:39:53 +08:00
import { onBeforeUnmount, onMounted, ref, defineExpose, watch, nextTick } from 'vue'
2024-01-12 09:55:55 +08:00
// import echarts from './echarts'
2024-01-18 20:29:38 +08:00
import * as echarts from 'echarts' // 全引入
import 'echarts-gl'
import 'echarts-liquidfill'
2023-12-26 14:15:05 +08:00
import 'echarts/lib/component/dataZoom'
import { color, gradeColor3 } from './color'
import { useConfig } from '@/stores/config'
2024-06-27 09:39:53 +08:00
// import { nextTick } from 'process'
2024-01-11 16:06:05 +08:00
const config = useConfig()
color[0] = config.layout.elementUiPrimary[0]
2023-12-29 11:50:53 +08:00
const chartRef = ref<HTMLDivElement>()
2023-12-26 14:15:05 +08:00
const props = defineProps(['options', 'isInterVal', 'pieInterVal'])
2024-01-02 13:45:20 +08:00
let chart: echarts.ECharts | any = null
2023-12-26 14:15:05 +08:00
const resizeHandler = () => {
// 不在视野中的时候不进行resize
if (!chartRef.value) return
if (chartRef.value.offsetHeight == 0) return
2024-01-05 16:32:18 +08:00
chart.getZr().painter.getViewportRoot().style.display = 'none'
2024-01-02 13:45:20 +08:00
requestAnimationFrame(() => {
chart.resize()
2024-01-05 16:32:18 +08:00
chart.getZr().painter.getViewportRoot().style.display = ''
2024-01-02 13:45:20 +08:00
})
2023-12-26 14:15:05 +08:00
}
const initChart = () => {
if (!props.isInterVal && !props.pieInterVal) {
chart?.dispose()
}
// chart?.dispose()
2023-12-26 14:15:05 +08:00
chart = echarts.init(chartRef.value as HTMLDivElement)
const options = {
2023-12-27 15:06:23 +08:00
title: {
left: 'center',
// textStyle: {
2024-06-24 14:38:42 +08:00
color: '#000',
2024-11-13 09:21:26 +08:00
textStyle: {
color: '#000',
fontSize: '18'
},
// },
...(props.options?.title || null)
2023-12-27 15:06:23 +08:00
},
2023-12-26 14:15:05 +08:00
tooltip: {
trigger: 'axis',
2024-09-13 18:35:02 +08:00
textStyle: {
color: '#fff',
fontStyle: 'normal',
opacity: 0.35,
fontSize: 14
},
2024-12-11 16:33:23 +08:00
backgroundColor: 'rgba(0,0,0,0.55)',
2023-12-29 16:25:26 +08:00
borderWidth: 0,
// confine: true,
...(props.options?.tooltip || null)
2023-12-27 15:06:23 +08:00
},
2024-10-22 10:50:47 +08:00
toolbox: {
2024-11-13 09:21:26 +08:00
right: 10,
top: 0,
2024-10-22 10:50:47 +08:00
feature: {
saveAsImage: {
2024-11-01 11:21:12 +08:00
title: '保存图片'
2024-10-22 10:50:47 +08:00
},
2024-11-01 11:21:12 +08:00
...(props.options?.toolbox?.featureProps || null)
2024-10-22 10:50:47 +08:00
},
// },
...(props.options?.toolbox || null)
},
2023-12-27 15:06:23 +08:00
legend: {
2024-11-13 09:21:26 +08:00
right: 40,
top: 10,
2023-12-28 14:23:22 +08:00
itemGap: 10,
2023-12-27 15:06:23 +08:00
itemStyle: {},
// textStyle: {
2024-06-24 14:38:42 +08:00
fontSize: 12,
2024-11-28 16:26:21 +08:00
// padding: [2, 0, 0, 0], //[上、右、下、左]
// },
2023-12-28 14:23:22 +08:00
itemWidth: 15,
2023-12-29 16:25:26 +08:00
itemHeight: 10,
...(props.options?.legend || null)
2023-12-26 14:15:05 +08:00
},
grid: {
2024-10-22 10:50:47 +08:00
top: '60px',
left: '30px',
right: '70px',
bottom: props.options?.options?.dataZoom === null ? '10px' : '40px',
containLabel: true,
...(props.options?.grid || null)
2023-12-26 14:15:05 +08:00
},
xAxis: props.options?.xAxis ? handlerXAxis() : null,
yAxis: props.options?.yAxis ? handlerYAxis() : null,
dataZoom: props.options?.dataZoom || [
2023-12-26 14:15:05 +08:00
{
type: 'inside',
height: 13,
start: 0,
2023-12-26 14:15:05 +08:00
bottom: '20px',
end: 100
2023-12-26 14:15:05 +08:00
},
{
start: 0,
height: 13,
bottom: '20px',
end: 100
}
2025-04-28 15:51:30 +08:00
// {
// show: true,
// yAxisIndex: 0,
// width: 12,
// handleSize: 8,
// showDataShadow: false,
// right: 12
// }
2023-12-26 14:15:05 +08:00
],
color: props.options?.color || color,
series: props.options?.series,
...props.options?.options
}
2024-11-01 11:21:12 +08:00
// console.log(options.series,"获取x轴");
handlerBar(options)
// 处理柱状图
2024-11-01 11:21:12 +08:00
chart.setOption(options, true)
setTimeout(() => {
chart.resize()
}, 0)
}
const handlerBar = (options: any) => {
if (Array.isArray(options.series)) {
options.series.forEach((item: any) => {
if (item.type === 'bar') {
item.barMinHeight = 10
item.barMaxWidth = 20
item.itemStyle = Object.assign(
{
color: (params: any) => {
if (params.value == 0 || params.value == 3.14159) {
return '#ccc'
} else {
return props.options?.color
? props.options?.color[params.seriesIndex]
: color[params.seriesIndex]
}
}
},
item.itemStyle
)
}
})
}
2023-12-26 14:15:05 +08:00
}
2024-01-11 16:06:05 +08:00
const handlerYAxis = () => {
let temp = {
type: 'value',
nameGap: 15,
2024-01-11 16:06:05 +08:00
nameTextStyle: {
color: '#000'
},
splitNumber: 5,
2024-01-11 16:06:05 +08:00
minInterval: 1,
axisLine: {
show: true,
lineStyle: {
color: '#000'
}
},
axisLabel: {
color: '#000',
2024-09-29 18:15:54 +08:00
fontSize: 14,
formatter: function (value) {
2025-04-28 15:51:30 +08:00
return parseFloat(value.toFixed(1)) // 格式化显示为一位小数
2024-09-29 18:15:54 +08:00
}
2024-01-11 16:06:05 +08:00
},
splitLine: {
lineStyle: {
// 使用深浅的间隔色
color: ['#ccc'],
2024-01-11 16:06:05 +08:00
type: 'dashed',
opacity: 0.5
}
}
2024-01-11 16:06:05 +08:00
}
// props.options?.xAxis 是数组还是对象
if (Array.isArray(props.options?.yAxis)) {
return props.options?.yAxis.map((item: any) => {
2024-01-11 16:06:05 +08:00
return {
...temp,
...item
2024-01-11 16:06:05 +08:00
}
})
} else {
return {
2024-01-18 20:29:38 +08:00
...temp,
...props.options?.yAxis
2024-01-11 16:06:05 +08:00
}
}
}
const handlerXAxis = () => {
let temp = {
type: 'category',
axisTick: { show: false },
axisLine: {
2024-06-18 16:35:53 +08:00
// lineStyle: {
2024-06-24 14:38:42 +08:00
color: '#000'
2024-06-18 16:35:53 +08:00
// }
2024-01-11 16:06:05 +08:00
},
axisLabel: {
// textStyle: {
2024-06-24 14:38:42 +08:00
fontFamily: 'dinproRegular',
color: '#000',
2025-04-28 15:51:30 +08:00
fontSize: '12'
// }
2025-04-28 15:51:30 +08:00
}
2024-11-01 11:21:12 +08:00
// boundaryGap: false,
2024-01-11 16:06:05 +08:00
}
// props.options?.xAxis 是数组还是对象
if (Array.isArray(props.options?.xAxis)) {
return props.options?.xAxis.map((item: any) => {
2024-01-11 16:06:05 +08:00
return {
...temp,
...item
2024-01-11 16:06:05 +08:00
}
})
} else {
return {
2024-01-18 20:29:38 +08:00
...temp,
...props.options?.xAxis
2024-01-11 16:06:05 +08:00
}
}
}
2024-10-22 10:50:47 +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)
}
})
2023-12-26 14:15:05 +08:00
onMounted(() => {
2024-01-02 13:45:20 +08:00
initChart()
resizeObserver.observe(chartRef.value!)
2023-12-26 14:15:05 +08:00
})
2024-07-03 19:31:29 +08:00
defineExpose({ initChart })
2023-12-26 14:15:05 +08:00
onBeforeUnmount(() => {
resizeObserver.unobserve(chartRef.value!)
2023-12-26 14:15:05 +08:00
chart?.dispose()
})
2023-12-27 15:06:23 +08:00
watch(
() => props.options,
(newVal, oldVal) => {
initChart()
2023-12-27 15:06:23 +08:00
}
)
2023-12-26 14:15:05 +08:00
</script>
<style lang="scss" scoped>
2024-06-24 14:38:42 +08:00
.chart {
2023-12-26 14:15:05 +08:00
width: 100%;
2024-06-24 14:38:42 +08:00
height: 100%;
position: relative;
2024-09-29 18:15:54 +08:00
2024-06-24 14:38:42 +08:00
.el-button {
position: absolute;
right: 0px;
top: -60px;
}
2024-09-29 18:15:54 +08:00
2024-06-24 14:38:42 +08:00
.my-chart {
height: 100%;
width: 100%;
}
2023-12-26 14:15:05 +08:00
}
</style>