Newer
Older
KaiFengPC / src / views / sponeScreen / Echarts / SewageBODEchaer.vue
@zhangdeliang zhangdeliang on 7 Nov 8 KB update
<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,
    });
    const resizeTheChart = () => {
      if (allData.chart) {
        allData.chart.resize();
      }
    };

    const init = () => {
      let markLinedata = [];
      if (props.data.controlMarkLine.length) {
        props.data.controlMarkLine.map(item => {
          let newitem = {
            silent: true, //鼠标悬停事件  true没有,false有
            lineStyle: {
              //警戒线的样式  ,虚实  颜色
              type: 'dashed',
              color: item.color,
              opacity: item.Opacity,
            },
            yAxis: item.value,
            label: {
              formatter: `${item.typeName}:${item.value}` + '(mg/L)',
              color: item.color,
            },
          };

          markLinedata.push(newitem);
        });
      }

      let chartDom = echarts.init(document.getElementById(allData.id));
      var option;
      option = {
        // backgroundColor: '#0e1c47',
        tooltip: {
          trigger: 'axis',
          //   backgroundColor: 'transparent',
          axisPointer: {
            lineStyle: {
              color: {
                // data1,
                type: 'linear',
                x: 0,
                y: 0,
                x2: 0,
                y2: 1,
                colorStops: [
                  {
                    offset: 0,
                    color: 'rgba(126,199,255,0)', // 0% 处的颜色
                  },
                  {
                    offset: 0.5,
                    color: 'rgba(126,199,255,1)', // 100% 处的颜色
                  },
                  {
                    offset: 1,
                    color: 'rgba(126,199,255,0)', // 100% 处的颜色
                  },
                ],
                global: false, // 缺省为 false
              },
            },
          },
        },
        legend: {
          align: 'left',
          right: '1%',
          top: '1%',
          type: 'plain',
          color: '#7ec7ff',
          fontSize: 13,
          // icon:'rect',
          itemGap: 25,
          itemWidth: 18,
          icon: 'path://M0 2a2 2 0 0 1 2 -2h14a2 2 0 0 1 2 2v0a2 2 0 0 1 -2 2h-14a2 2 0 0 1 -2 -2z',

          // data: [
          //   {
          //     name: '水深(cm)',
          //   },
          //   {
          //     name: 'BOD(mg/L)',
          //   },
          // ],
        },
        grid: {
          top: '20%',
          left: '12%',
          right: '3%',
          bottom: '25%',
          //   containLabel: true,
        },
        graphic: {
          type: 'text', // 类型:文本
          left: 'center',
          top: 'middle',
          silent: true, // 不响应事件
          invisible: props.data.XName.length > 0 ? true : false, // 有数据就隐藏, // 有数据就隐藏
          style: {
            fill: '#c6c6c6',
            fontWeight: 'bold',
            text: '暂无数据',
            fontFamily: 'Microsoft YaHei',
            fontSize: '18px',
          },
        },
        xAxis: [
          {
            type: 'category',
            boundaryGap: false,
            axisLine: {
              //坐标轴轴线相关设置。数学上的x轴
              show: true,
              lineStyle: {
                color: 'rgba(83,216,251,0.8)',
              },
            },
            axisLabel: {
              //坐标轴刻度标签的相关设置
              color: '#7ec7ff',
              padding: 14,
              fontSize: 14,
              formatter: function (data) {
                return data;
              },
            },
            splitLine: {
              show: false,
              lineStyle: {
                type: 'dashed',
                color: '#3afff8',
              },
            },
            axisTick: {
              show: false,
            },
            data: props.data.XName,
          },
        ],
        yAxis: [
          {
            name: 'mg/L',
            nameTextStyle: {
              color: '#7ec7ff',
              fontSize: 13,
              padding: 1,
            },
            min: 0,
            splitLine: {
              show: true,
              lineStyle: {
                type: 'dashed',
                color: 'rgba(83,216,251,0.3)',
              },
            },
            axisLine: {
              show: false,
              lineStyle: {
                color: 'rgba(83,216,251,0.3)',
              },
            },
            axisLabel: {
              show: true,
              color: '#7ec7ff',
              padding: 16,
              formatter: function (value) {
                if (value === 0) {
                  return value;
                }
                return value;
              },
            },
            axisTick: {
              show: false,
            },
          },
        ],
        series: [
          {
            name: '',
            type: 'line',
            symbol: 'circle', // 默认是空心圆(中间是白色的),改成实心圆

            showAllSymbol: true,
            symbolSize: 0,
            smooth: true,
            lineStyle: {
              width: 2,
              color: '#FFF21C', // 线条颜色
              borderColor: '#FFF21C',
            },
            itemStyle: {
              color: '#FFF21C',
              borderColor: '#FFF21C',
              borderWidth: 2,
            },
            tooltip: {
              show: true,
            },
            areaStyle: {
              //区域填充样式
              //线性渐变,前4个参数分别是x0,y0,x2,y2(范围0~1);相当于图形包围盒中的百分比。如果最后一个参数是‘true’,则该四个值是绝对像素位置。
              color: new echarts.graphic.LinearGradient(
                0,
                0,
                0,
                1,
                [
                  {
                    offset: 0,
                    color: 'rgba(10,219,250,.3)',
                  },
                  {
                    offset: 1,
                    color: 'rgba(10,219,250, 0)',
                  },
                ],
                false
              ),
              shadowColor: 'rgba(10,219,250, 0.5)', //阴影颜色
              shadowBlur: 20, //shadowBlur设图形阴影的模糊大小。配合shadowColor,shadowOffsetX/Y, 设置图形的阴影效果。
            },
            markLine: {
              symbol: 'none', //去掉警戒线最后面的箭头
              label: {
                position: 'middle', //将警示值放在哪个位置,三个值“start”,"middle","end"  开始  中点 结束
              },
              data: markLinedata,
            },
            data: props.data.data1,
          },
        ],
      };
      option && chartDom.setOption(option, true);
      allData.chart = chartDom;
      animateChart();
    };
    watch(
      () => props.refresh,
      () => {
        if (allData.chart) {
          allData.chart.dispose();
          allData.chart = null;
        }
        setTimeout(() => {
          init();
        }, 0);
      }
    );
    // echarts动画
    function animateChart() {
      let length = props.data.XName.length;
      let i = 0;
      allData.timer = setInterval(() => {
        i++;
        if (i == length) i = 0;
        allData.chart.dispatchAction({
          type: 'showTip',
          seriesIndex: 0,
          dataIndex: i,
        });
      }, 2000);
    }
    onMounted(() => {
      init();
      window.addEventListener('resize', resizeTheChart);
    });
    onUnmounted(() => {
      if (allData.timer) clearInterval(allData.timer);
    });
    return {
      ...toRefs(allData),
      resizeTheChart,
      init,
    };
  },
};
</script>