Newer
Older
urbanLifeline_YanAn / src / views / oneMap / components / chartLogging.vue
@鲁yixuan 鲁yixuan on 12 Oct 5 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,
      timeS: null,
      anIndex: 0,
    });
    const resizeTheChart = () => {
      if (allData.chart) {
        allData.chart.resize();
      }
    };

    const init = () => {
      let chartDom = echarts.init(document.getElementById(allData.id));
      var option;
      option = {
        color: ['#FFB049', '#FFB049'],
        tooltip: {
          trigger: 'axis',
        },
        grid: {
          top: 30,
          bottom: 30,
          right: 50,
        },
        legend: {
          data: ['降雨量(mm)', '积水深(cm)'],
          textStyle: {
            color: '#fff',
          },
        },

        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 || '',
            inverse: props.data.inverse || false, //翻转
            type: 'value',
            axisLabel: {
              color: 'rgba(73, 197, 255, 1)',
              show: true,
            },
            nameTextStyle: {
              color: 'rgba(73, 197, 255, 1)',
            },
            splitLine: {
              lineStyle: {
                type: 'dashed',
                color: '#60C2FF',
              },
            },
          },

          {
            name: props.data.yAxisName2 || '',
            inverse: false,
            nameLocation: 'start', // 坐标轴名称显示位置
            type: 'value',
            nameTextStyle: {
              color: '#FFB049',
            },
            axisLabel: {
              show: true,
              color: '#FFB049',
            },
            splitLine: {
              lineStyle: {
                color: '#1D5D81',
                type: 'dashed',
              },
            },
          },
        ],
        // 时间轴滑动选择器
        dataZoom: [
          {
            show: true,
            realtime: true,
            start: 30,
            end: 70,
            xAxisIndex: [0, 1],
          },
        ],
        series: [
          {
            name: '降雨量(mm)',
            type: 'bar',
            barGap: '10%', // Make series be overlap
            barCateGoryGap: '10%',
            itemStyle: {
              color: new echarts.graphic.LinearGradient(0, 0, 0, 0.7, [
                {
                  offset: 0,
                  color: 'rgba(169, 210, 255, 1)',
                },
                {
                  offset: 1,
                  color: 'rgba(73, 197, 255, 1)',
                },
              ]),
              borderRadius: [0, 0, 10, 10],
            },
            yAxisIndex: 0,
            data: props.data.yAxis,
          },
          {
            // smooth: true, //变为曲线 默认false折线
            name: '积水深(cm)',
            type: 'line',
            data: props.data.yAxis2,
            yAxisIndex: 1,
            color: '#FFB049',
          },
          {
            type: 'line',
            yAxisIndex: 1,
            markLine: {
              symbol: 'none',
              label: {
                show: false,
                position: 'middle',
                formatter: '轻微内涝警戒线:{c}',
                color: '#FFFF80',
                fontWeight: 'bold',
              },
              lineStyle: {
                color: '#FFFF80',
                width: 1,
                type: 'solid',
              },
              data: [
                {
                  name: '轻微内涝警戒线',
                  yAxis: '15',
                },
              ],
            },
          },
          {
            type: 'line',
            yAxisIndex: 1,
            markLine: {
              symbol: 'none',
              label: {
                show: false,
                position: 'middle',
                formatter: '严重内涝警戒线:{c}',
                color: '#E20F36',
                fontWeight: 'bold',
              },
              lineStyle: {
                color: '#E20F36',
                width: 1,
                type: 'solid',
              },
              data: [
                {
                  name: '严重内涝警戒线',
                  yAxis: '30',
                },
              ],
            },
          },
        ],
      };
      option && chartDom.setOption(option, true);
      allData.chart = chartDom;
      animateChart();
    };
    watch(
      () => props.refresh,
      () => {
        if (allData.chart) {
          allData.chart.dispose();
          allData.chart = null;
        }
        if (allData.timeS) clearInterval(allData.timeS);
        setTimeout(() => {
          init();
          allData.anIndex = 0;
        }, 0);
      }
    );
    // echarts动画
    function animateChart() {
      let length = props.data.xAxis.length;
      allData.timeS = setInterval(() => {
        allData.anIndex++;
        if (allData.anIndex == length) allData.anIndex = 0;
        allData.chart.dispatchAction({
          type: 'showTip',
          seriesIndex: 0,
          dataIndex: allData.anIndex,
        });
      }, 2000);
    }
    onMounted(() => {
      window.addEventListener('resize', resizeTheChart);
    });
    onUnmounted(() => {
      if (allData.timeS) clearInterval(allData.timeS);
    });
    return {
      ...toRefs(allData),
      resizeTheChart,
      init,
    };
  },
};
</script>