<!--
 * @Author: SunTao 328867980@qq.com
 * @Date: 2024-10-21 10:03:08
 * @LastEditors: SunTao 328867980@qq.com
 * @LastEditTime: 2024-10-31 14:34:36
 * @FilePath: \znxjxt-ui\src\views\big-screen\traffic-components\traffic-trend.vue
 * @Description: 交安事件大屏-交安事件趋势
-->

<template>
  <div class="content">
    <div class="traffic-select">
      <el-select
        :popper-append-to-body="false"
        v-model="itemSelect"
        placeholder="请选择"
      >
        <el-option
          v-for="item in trafficList"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        />
      </el-select>
    </div>
    <div ref="trafficChart" class="traffic-echart"></div>
  </div>
</template>
  
<script>
import { getDropList } from "@/api/xj/screen/disease-screen";
import { eventLog } from "@/api/xj/screen/traffic-screen";
import * as echarts from "echarts";
export default {
  name: "TrafficTrend",
  props: {
    select: {
      type: String,
      default: "",
    },
  },
  data() {
    return {
      // 下拉框选中绑定
      itemSelect: "",
      // 下拉框数据
      trafficList: [],
      // echart图表数据
      echartData: [],
      // echart实例
      chart: null,
    };
  },
  watch: {
    select: {
      handler(val) {
        if (val) {
          this.itemSelect = "";
          this.getCurrentList();
          this.getChartData();
        }
      },
    },
    itemSelect: {
      handler() {
        this.getChartData();
      },
    },
  },
  created() {
    this.getChartData();
  },
  methods: {
    /* 获取右上角下拉框数据 */
    getCurrentList() {
      getDropList({ type: this.select }).then(({ data, code }) => {
        if (code === 200) {
          this.trafficList = data;
        }
      });
    },
    /* 获取柱状图数据 */
    getChartData() {
      const data = {
        id: this.itemSelect,
        type: this.select,
      };
      eventLog(data).then(({ data, code }) => {
        if (code === 200) {
          this.echartData = data;
          this.$nextTick(() => {
            this.drawChart();
          });
        }
      });
    },
    /* 绘制echart */
    drawChart() {
      if (this.chart) {
        this.chart.dispose();
      }
      const xData = this.echartData.map((item) => {
        return item.label;
      });
      const yData = this.echartData.map((item, index) => {
        if (index % 2 == 0) {
          return {
            ...item,
            itemStyle: {
              color: {
                x: 0,
                y: 0,
                x2: 0,
                y2: 1,
                type: "linear",
                global: false,
                colorStops: [
                  {
                    //第一节下面
                    offset: 0,
                    color: "#033E43",
                  },
                  {
                    offset: 1,
                    color: "#00FFEA",
                  },
                ],
              },
            },
          };
        } else {
          return {
            ...item,
            itemStyle: {
              color: {
                x: 0,
                y: 0,
                x2: 0,
                y2: 1,
                type: "linear",
                global: false,
                colorStops: [
                  {
                    //第一节下面
                    offset: 0,
                    color: "#034299",
                  },
                  {
                    offset: 1,
                    color: "#35F0FF",
                  },
                ],
              },
            },
          };
        }
      });
      this.chart = echarts.init(this.$refs.trafficChart);
      this.chart.setOption({
        // backgroundColor: "#041139",
        tooltip: {
          trigger: "axis",
          backgroundColor: "rgba(9, 24, 48, 0.5)",
          borderColor: "rgba(75, 253, 238, 0.4)",
          textStyle: {
            fontSize: 12,
            color: "#808C9F",
          },
        },
        legend: {
          show: false,
        },
        grid: {
          top: "20%",
          left: "10%",
          right: 0,
          bottom: "20%",
        },
        xAxis: [
          {
            data: xData,
            axisLabel: {
              color: "#808C9F",
              fontSize: 12,
              interval: 0,
            },
            axisLine: {
              show: false, //不显示x轴
              lineStyle: {
                color: "rgba(53,65,95,1)",
              },
            },
            axisTick: {
              show: false, //不显示刻度
            },
            boundaryGap: true,
            splitLine: {
              show: false,
            },
          },
        ],
        yAxis: [
          {
            name: "数量",
            nameTextStyle: {
              color: "#C7DAF2",
            },
            splitLine: {
              show: false,
            },
            axisTick: {
              show: false,
            },
            axisLine: {
              show: false, //不显示x轴
              lineStyle: {
                color: "rgba(53,65,95,1)",
              },
            },
            axisLabel: {
              show: true,
              color: "#808C9F",
              fontSize: 12,
            },
          },
        ],
        series: [
          // 柱体
          {
            name: "数量",
            type: "bar",
            barWidth: 10,
            itemStyle: {
              borderColor: "#08B4A6",
              borderWidth: 1,
            },
            showBackground: true,
            backgroundStyle: {
              color: "rgba(105,160,231,0.1)",
            },
            label: {
              show: false, //每条柱状图是否显示数字
              formatter: "{c}",
              position: "top",
              color: "#fff",
              fontSize: 14,
            },
            data: yData,
          },
        ],
        dataZoom: [
          {
            // 设置滚动条的隐藏与显示
            show: false,
            // 设置滚动条类型
            type: "slider",
            // 设置背景颜色
            backgroundColor: "rgba(225,225,225,0.2)",
            // 设置选中范围的填充颜色
            fillerColor: "#ccc",
            // 设置边框颜色
            borderColor: "rgba(225,225,225,0.2)",
            // 是否显示detail，即拖拽时候显示详细数值信息
            showDetail: false,
            // 数据窗口范围的起始数值
            startValue: 0,
            // 数据窗口范围的结束数值（一页显示多少条数据）
            endValue: 6,
            // empty：当前数据窗口外的数据，被设置为空。
            // 即不会影响其他轴的数据范围
            filterMode: "empty",
            // 设置滚动条宽度，相对于盒子宽度
            width: "80%",
            // 设置滚动条高度
            height: 5,
            // 设置滚动条显示位置
            left: "center",
            // 是否锁定选择区域（或叫做数据窗口）的大小
            zoomLoxk: true,
            // 控制手柄的尺寸
            handleSize: 10,
            // dataZoom-slider组件离容器下侧的距离
            bottom: 0,
          },
          {
            // 没有下面这块的话，只能拖动滚动条，
            // 鼠标滚轮在区域内不能控制外部滚动条
            type: "inside",
            // 滚轮是否触发缩放
            zoomOnMouseWheel: false,
            // 鼠标滚轮触发滚动
            moveOnMouseMove: true,
            moveOnMouseWheel: true,
          },
        ],
      });
      window.addEventListener("resize", () => {
        this.chart.resize();
      });
    },
  },
};
</script>
  
  <style lang="scss" scoped>
.content {
  width: 100%;
  height: 100%;
  position: relative;

  .traffic-select {
    position: absolute;
    z-index: 1;
    right: 0;
    top: 0.2rem;

    ::v-deep .el-select {
      width: 6.5rem;

      .el-input .el-select__caret {
        line-height: 1.5rem;
      }

      .el-input--medium .el-input__inner {
        height: 1.5rem;
        background-color: transparent;
        color: #89c5e8;
        border-color: #6991cd;
      }

      .el-select-dropdown {
        background-color: #102649;
        border-color: #08204f;
        .el-scrollbar {
          .el-select-dropdown__wrap {
            .el-scrollbar__view {
              .el-select-dropdown__item:hover {
                background-color: #2b4c7e;
              }

              .el-select-dropdown__item.selected {
                background-color: #2b4c7e;
              }

              .el-select-dropdown__item.hover {
                background-color: #2b4c7e;
              }
            }

            .el-select-dropdown__list {
              background-color: #102649;
            }
          }
        }
      }
    }
  }

  .traffic-echart {
    width: 100%;
    height: 100%;
    overflow: hidden;
  }
}
</style>