<template> <div :id="id" style="width: 100%; height: 100%"></div> </template> <script> import * as echarts from 'echarts'; import { guid } from '@/utils/ruoyi'; import { reactive, toRefs, onMounted, watch, onUnmounted } from 'vue'; export default { name: 'line-chart', props: { data: Object, refresh: Number, }, setup(props) { const allData = reactive({ series: [], legend: [], id: guid(), chart: null, timer: null, anIndex: 0, }); const resizeTheChart = () => { if (allData.chart) { allData.chart.resize(); } }; const init = () => { let chartDom = echarts.init(document.getElementById(allData.id)); var barWidth = 12; var option; option = { color: ['#FFB049', '#FFB049'], tooltip: { trigger: 'axis', }, grid: { top: 30, bottom: 30, right: 50, }, xAxis: [ { type: 'category', splitLine: { show: false, }, axisTick: { show: false, }, axisLabel: { color: 'rgba(255,255,255,1)', fontSize: 14, fontFamily: 'AlibabaPuHuiTi', }, data: props.data.xAxis, }, ], yAxis: [ { name: props.data.yAxisName1 || '', type: 'value', axisLabel: { color: '#FFB049', show: true, }, nameTextStyle: { color: '#FFB049', }, splitLine: { lineStyle: { type: 'dashed', color: '#60C2FF', }, }, }, { name: props.data.yAxisName2 || '', type: 'value', nameTextStyle: { color: '#1BFFF7', }, axisLabel: { show: true, color: '#1BFFF7', }, splitLine: { lineStyle: { color: '#1D5D81', type: 'dashed', }, }, }, ], series: [ { // smooth: true, //变为曲线 默认false折线 name: '水深(cm)', type: 'line', data: props.data.yAxis1, yAxisIndex: 0, color: '#FFB049', }, { // smooth: true, //变为曲线 默认false折线 name: 'ss(mg/L)', type: 'line', data: props.data.yAxis2, yAxisIndex: 1, color: '#1BFFF7', }, ], }; option && chartDom.setOption(option, true); allData.chart = chartDom; animateChart(); }; watch( () => props.refresh, () => { if (allData.chart) { allData.chart.dispose(); allData.chart = null; } if (allData.timer) clearInterval(allData.timer); setTimeout(() => { init(); allData.anIndex = 0; }, 0); } ); // echarts动画 function animateChart() { let length = props.data.xAxis.length; allData.timer = setInterval(() => { allData.anIndex++; if (allData.anIndex == length) allData.anIndex = 0; allData.chart.dispatchAction({ type: 'showTip', seriesIndex: 0, dataIndex: allData.anIndex, }); }, 2000); } onMounted(() => { init(); window.addEventListener('resize', resizeTheChart); }); onUnmounted(() => { if (allData.timer) clearInterval(allData.timer); }); return { ...toRefs(allData), resizeTheChart, init, }; }, }; </script>