<template> <div :id="id" style="width: 100%; height: 100%"></div> </template> <script> import * as echarts from "echarts"; import { guid } from "@/utils/util"; import { reactive, toRefs, onMounted, watch } from "vue"; export default { name: "line-chart", props: { data: Object, }, setup(props) { const state = reactive({ series: [], legend: [], id: guid(), chart: null, }); const resizeTheChart = () => { if (state.chart) { state.chart.resize(); } }; // chartData1: { // xData: ["13:00", "13:20", "13.40", "14:00", "14:20", "14:40", "15:00"],//横坐标 // info: [ // { // y: [3.0, 4.0, 3.7, 1.2, 3.5, 2.6, 6],//纵坐标 // color: "rgba(70, 163, 244, 1)",//折线颜色 // bgColor: [ // { offset: 0.1, color: "rgba(58, 167, 255,0.8)" },//折线下方区域背景颜色 // { offset: 1, color: "rgba(58, 167, 255,0.1)" }, // ], // }, // ], // legendShow:false,//是否显示图例 // yAxisShow: false,//y轴坐标轴 // yLineShow: true,//y轴网格线 // yAxisColor: ["rgba(13, 72, 146, .3)"],/y轴网格线颜色 // yName: "",//y轴坐标名称 // smooth: false,//是否是曲线形式显示 // }, const initSeries = () => { props.data.info.forEach((v, i) => { state.series.push({ name: v.name, data: v.y, type: "line", smooth: props.data.smooth, symbol: "circle", symbolSize: [6, 6], itemStyle: { normal: { color: v.color, lineStyle: { color: v.color, }, }, }, // 填充区域的样式 areaStyle: { normal: { // 填充色渐变 color: new echarts.graphic.LinearGradient(0, 0, 0, 1, v.bgColor), }, }, }); state.legend.push(v.name); }); }; const init = () => { let chartDom = echarts.init(document.getElementById(state.id)); var option; option = { tooltip: { trigger: "axis", axisPointer: { type: "shadow", }, }, grid: { top: "14%", left: "3%", right: "3%", bottom: "5%", containLabel: true, }, legend: { show: props.data.legendShow, itemHeight: 10, itemWidth: 10, data: state.legend, icon: "rect", right: 0, textStyle: { fontSize: 18, //字体大小 color: "rgba(44, 117, 125, 1)", }, }, xAxis: { type: "category", data: props.data.xData, axisLabel: { //y轴文字的配置 textStyle: { color: "rgba(44, 117, 125, 1)", margin: 15, }, // formatter: '{value} %'//y轴的每一个刻度值后面加上‘%’号 }, }, yAxis: { type: "value", name: props.data.yName, axisLine: { show: props.data.yAxisShow, }, splitLine: { show: props.data.yLineShow, lineStyle: { color: props.data.yAxisColor, width: 1, type: "solid", }, }, axisLabel: { //y轴文字的配置 textStyle: { color: "rgba(44, 117, 125, 1)", margin: 15, }, // formatter: '{value} %'//y轴的每一个刻度值后面加上‘%’号 }, }, series: state.series, }; option && chartDom.setOption(option, true); state.chart = chartDom; }; onMounted(() => { initSeries(); init(); window.addEventListener("resize", resizeTheChart); }); return { ...toRefs(state), resizeTheChart, initSeries, init, }; }, }; </script>