Newer
Older
urbanLifeline_YanAn / src / views / oneMap / Echarts / WarningEcharts.vue
@鲁yixuan 鲁yixuan on 6 Oct 4 KB updata
<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 chartDom = echarts.init(document.getElementById(allData.id));
      var barWidth = 12;
      var option;
      option = {
        color: ['#3FFFC2', '#FFF21C', 'orange'],
        tooltip: {
          trigger: 'axis',
        },
        grid: {
          top: 40,
          bottom: 30,
        },
        legend: {
          data: ['最高流量(m³/h)', '最高水位(m)'],
          textStyle: {
            color: '#fff',
          },
        },
        xAxis: [
          {
            type: 'category',
            axisLine: {
              show: false,
              lineStyle: {
                width: 2,
                color: '#58b2ed',
              },
            },
            splitLine: {
              show: false,
            },
            axisTick: {
              show: false,
            },
            axisLabel: {
              color: 'rgba(255,255,255,1)',
              fontSize: 14,
              fontFamily: 'AlibabaPuHuiTi',
            },
            data: props.data.xAxis,
          },
        ],
        yAxis: [
          {
            name: '单位:(m³/h)',
            type: 'value',
            inverse: true,
            nameLocation: 'start', // 坐标轴名称显示位置
            axisLabel: {
              color: '#fff',
              show: true,
            },
            nameTextStyle: {
              color: '#fff',
            },
            splitLine: {
              lineStyle: {
                type: 'dashed',
                color: '#60C2FF',
              },
            },
          },
          {
            name: '单位:m',
            type: 'value',
            inverse: true,
            nameLocation: 'start', // 坐标轴名称显示位置
            nameTextStyle: {
              color: '#fff',
            },
            axisLabel: {
              show: true,
              color: '#fff',
            },
            splitLine: {
              lineStyle: {
                color: '#D1DEEE',
                type: 'dashed',
                color: '#53D8FB',
              },
            },
          },
        ],
        series: [
          {
            name: '最高流量(m³/h)',
            type: 'bar',
            barWidth: barWidth,
            barGap: '10%', // Make series be overlap
            barCateGoryGap: '10%',
            itemStyle: {
              color: new echarts.graphic.LinearGradient(0, 0, 0, 0.7, [
                {
                  offset: 0,
                  color: 'rgba(24, 255, 255, 1)',
                },
                {
                  offset: 1,
                  color: 'rgba(188, 255, 255, 1)',
                },
              ]),
            },
            yAxisIndex: 0,
            data: props.data.yAxis,
          },
          {
            smooth: true, //变为曲线 默认false折线
            name: '最高水位(m)',
            type: 'line',
            data: props.data.yAxis2,
            yAxisIndex: 1,
            color: '#FFF21C',
          },
          4,
        ],
      };
      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.xAxis.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>