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

294 lines
8.0 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">
<el-button v-if="isExport" type="primary" size="small" icon="el-icon-Download" @click="handleExport">
导出
</el-button>
<div ref="chartRef" class="my-chart" />
</div>
2023-12-26 14:15:05 +08:00
</template>
<script setup lang="ts">
2023-12-27 15:06:23 +08:00
import { onBeforeUnmount, onMounted, ref, defineExpose, watch } 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-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
2024-06-24 14:38:42 +08:00
const props = defineProps(['options', 'isExport'])
const ExportOptions: any = ref({})
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 = () => {
2023-12-27 15:06:23 +08:00
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',
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',
axisPointer: {
2023-12-27 15:06:23 +08:00
type: 'shadow',
label: {
color: '#fff',
fontSize: 16
}
2023-12-26 14:15:05 +08:00
},
// textStyle: {
2024-06-24 14:38:42 +08:00
color: '#fff',
fontStyle: '14px',
opacity: 0.35,
fontSize: 14,
// },
2023-12-27 15:06:23 +08:00
backgroundColor: 'rgba(0,0,0,0.35)',
2023-12-29 16:25:26 +08:00
borderWidth: 0,
...(props.options?.tooltip || null)
2023-12-27 15:06:23 +08:00
},
legend: {
2023-12-28 14:23:22 +08:00
right: 20,
2023-12-27 15:06:23 +08:00
top: 0,
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,
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: {
2023-12-27 15:06:23 +08:00
top: '50px',
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,
bottom: '20px',
end: 100
2023-12-26 14:15:05 +08:00
},
{
start: 0,
height: 13,
bottom: '20px',
end: 100
2023-12-26 14:15:05 +08:00
}
],
color: props.options?.color || color,
series: props.options?.series,
...props.options?.options
}
handlerBar(options)
// 处理柱状图
chart.setOption(options)
2024-06-24 14:38:42 +08:00
ExportOptions.value = options
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'
},
minInterval: 1,
axisLine: {
show: true,
lineStyle: {
color: '#000'
}
},
axisLabel: {
color: '#000',
fontSize: 14
},
splitLine: {
lineStyle: {
// 使用深浅的间隔色
color: ['#ccc'],
2024-01-11 16:06:05 +08:00
type: 'dashed',
opacity: 0.5
}
}
}
// 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',
fontSize: '12'
// }
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
}
}
}
let throttle: ReturnType<typeof setTimeout>
// 动态计算table高度
const resizeObserver = new ResizeObserver(entries => {
for (const entry of entries) {
if (throttle) {
clearTimeout(throttle)
}
throttle = setTimeout(() => {
resizeHandler()
}, 100)
}
})
2024-06-24 14:38:42 +08:00
const ExportChart: any = ref(null)
const handleExport = () => {
// 使用ECharts的getOption方法获取当前图表的配置
const option = ExportOptions.value
console.log(option, '00000000000')
const seriesData = option?.series // 假设我们只导出第一个系列的数据
if (seriesData && seriesData.length != 0) {
// 转换为CSV格式
let csvContent = 'data1,data2\n'
seriesData.forEach((item: any) => {
item.data.map((vv: any) => {
csvContent += `${vv.name},${vv.value}\n`
})
})
// 创建Blob对象并使用a标签下载
const blob = new Blob([csvContent], { type: 'text/csv;charset=utf-8;' })
const link = document.createElement('a')
if (link.download !== undefined) {
// 支持下载属性
const url = URL.createObjectURL(blob)
link.setAttribute('href', url)
link.setAttribute('download', 'data.csv')
link.style.visibility = 'hidden'
document.body.appendChild(link)
link.click()
document.body.removeChild(link)
}
}
}
2023-12-26 14:15:05 +08:00
onMounted(() => {
2024-01-02 13:45:20 +08:00
initChart()
2024-06-24 14:38:42 +08:00
ExportChart.value = echarts.init(chartRef.value)
resizeObserver.observe(chartRef.value!)
2023-12-26 14:15:05 +08:00
})
2023-12-27 15:06:23 +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-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;
.el-button {
position: absolute;
right: 0px;
top: -60px;
}
.my-chart {
height: 100%;
width: 100%;
}
2023-12-26 14:15:05 +08:00
}
</style>