<template> <!-- 天气温度平滑折线图 --> <div :id="id" style="width: 100%; height: 100%"></div> </template> <script setup> import { guid } from '@/utils/ruoyi'; import { nextTick } from 'vue'; const id = guid(); const myChart = shallowRef(''); import * as echarts from 'echarts'; const props = defineProps({ //刷新标志 refresh: { type: [String, Number], default: 1, }, //x轴数据 XAxis: { type: Array, default: () => [], }, //y轴数据 YAxis: { type: Array, default: () => [], }, //类型 typeName: { type: String, default: '', }, }); watch( () => props.refresh, value => { //先销毁实例 myChart.value && myChart.value.dispose(); init(); }, { deep: true, } ); function init() { myChart.value = echarts.init(document.getElementById(id)); let option = { tooltip: { trigger: 'axis', backgroundColor: '#004284', borderColor: '#0B9BFF', borderRadius: 6, // 设置圆角大小 textStyle: { // 字体颜色 color: 'white', // 字体大小 fontSize: 14, }, formatter: params => { // console.log(params); var relVal = '' + params[0].name; for (var i = 0, l = params.length; i < l; i++) { if (params[i].seriesName) { let unit = ''; relVal += '<br/>' + params[i].marker + params[i].seriesName + ' ' + params[i].value + unit; } } return relVal; }, }, grid: { left: '8%', right: '4%', bottom: '3%', top: '10%', containLabel: true, }, xAxis: { boundaryGap: true, type: 'category', data: props.XAxis, axisLabel: { show: true, color: '#fff', // interval: 0, formatter: function (params) { let one = params.substring(0, 3); let two = params.substring(3, 6); let three = params.substring(6, 8); let four = params.substring(8) ? '...' : ''; let newParamsName = one + '\n' + two + '\n' + three + four; return newParamsName; }, }, axisLine: { show: true, lineStyle: { show: true, //是否显示坐标轴轴线, color: 'rgba(255,255,255,0.4)', //x轴轴线的颜色 width: 1, //x轴粗细 }, }, axisTick: { show: false, //是否显示刻度 }, }, yAxis: { type: 'value', name: props.typeName, nameLocation: 'start', nameTextStyle: { color: '#AED2E0', padding: [-5, 28, 0, 0], // 四个数字分别为上右下左与原位置距离 }, axisLabel: { show: true, color: '#AED2E0', formatter: function (value) { return value.toFixed(0); // 显示小数点后两位 }, }, axisLine: { show: true, lineStyle: { show: true, //是否显示坐标轴轴线, color: 'rgba(255,255,255,0.4)', //x轴轴线的颜色 width: 1, //x轴粗细 }, }, splitLine: { show: true, lineStyle: { color: 'rgba(255,255,255,0.2)', }, }, }, series: [ { name: props.typeName, type: 'bar', barWidth: 15, itemStyle: { borderRadius: [6, 6, 0, 0], color: function () { return new echarts.graphic.LinearGradient( 0, 1, 0, 0, [ { offset: 0, color: 'rgba(43, 174, 227,0.4)', }, { offset: 1, color: 'rgba(43, 174, 227,1)', }, ], false ); }, }, // showBackground: true, backgroundStyle: { color: 'transparent', borderWidth: 1, borderColor: 'rgba(148, 250, 65, 0.2)', }, data: props.YAxis, }, { data: props.YAxis, name: '', // 系列名称, 用于tooltip的显示, legend 的图例筛选 type: 'pictorialBar', // 系列类型 symbolSize: [15, 8], // 标记的大小 symbolOffset: [0, 0], // 标记相对于原本位置的偏移 symbolPosition: 'end', // 图形的定位位置。可取值:start、end、center // 图例的图形样式 itemStyle: { color: { type: 'linear', x: 0, y: 0, x2: 1, y2: 0, colorStops: [ { offset: 0, color: '#63ccf8', // 0% 处的颜色 }, { offset: 1, color: '#63ccf8', // 100% 处的颜色 }, ], }, }, z: 100, // 组件的所有图形的z值。控制图形的前后顺序。z值小的图形会被z值大的图形覆盖 }, ], }; option && myChart.value.setOption(option); } function resizeTheChart() { if (myChart.value) { myChart.value.resize(); } } onMounted(() => { nextTick(() => { init(); window.addEventListener('resize', resizeTheChart); }); }); </script>