Newer
Older
urbanLifeline_YanAn / src / views / oneMap / floodPrevention / barChart.vue
@zhangqy zhangqy on 13 Nov 3 KB 性能优化
<template>
  <div class="chartBox" :id="id"></div>
</template>
<script setup name="pieChartGszl">
import { guid } from "@/utils/ruoyi";
const { proxy } = getCurrentInstance();
import { nowSize } from "@/utils/util.js";
import moment from "moment";
const props = defineProps({
  //刷新标志
  refresh: {
    type: [String, Number],
    default: 1,
  },
  //数据
  echartData: {
    type: Object,
    default: {},
  },
});

const id = guid();
const myChart = shallowRef("");

watch(
  () => props.refresh,
  (value) => {
    //先销毁实例
    myChart.value && myChart.value.dispose();
    intChart();
  }
);
//自适应
function resizeTheChart() {
  if (myChart.value) {
    myChart.value.resize();
  }
}
//初始化
function intChart() {
  myChart.value = proxy.echarts.init(document.getElementById(id));
  myChart.value.clear();
  // 绘制图表
  myChart.value.setOption({
    tooltip: {
      trigger: "axis",
      axisPointer: {
        type: "shadow",
        label: {
          show: true,
        },
      },
    },
    grid: {
      top: nowSize(30, 1920),
      bottom: nowSize(30, 1920), //也可设置left和right设置距离来控制图表的大小
      left: nowSize(50, 1920),
      right: nowSize(10, 1920),
    },
    xAxis: {
      data: props.echartData.dateRange,
      axisLine: {
        show: true, //隐藏X轴轴线
        lineStyle: {
          color: "rgba(44, 110, 189, 1)",
        },
      },
      axisTick: {
        show: false, //隐藏X轴刻度
        lineStyle: {
          color: "rgba(44, 110, 189, 1)",
        },
      },
      axisLabel: {
        show: true,
        color: "rgba(179, 194, 209, 1)",
        fontSize: nowSize(12, 1920),
        formatter: (value) => {
          return moment(value).format("HH:mm");
        },
      },
    },
    yAxis: {
      type: "value",
      name: "单位:mm",
      nameTextStyle: {
        color: "rgba(179, 194, 209, 1)",
        fontSize: nowSize(14, 1920),
      },
      axisLine: {
        show: false,
      },
      axisLabel: {
        show: true,
        color: "rgba(179, 194, 209, 1)",
        fontSize: nowSize(12, 1920),
      },
      splitLine: {
        //网格线
        lineStyle: {
          type: "dashed",
          color: "rgba(44, 110, 189, 1)", //设置网格线类型 dotted:虚线   solid:实线
        },
        show: true, //隐藏或显示
      },
    },
    series: {
      type: "bar",
      name: "雨量",
      data: props.echartData.totalData,
      barWidth: nowSize(8, 1920),
      itemStyle: {
        borderRadius: nowSize(15, 1920),
        color: (parms) => {
          let color1 = new proxy.echarts.graphic.LinearGradient(0, 0, 0, 1, [
            {
              offset: 0,
              color: "#48a0fb",
            },
            {
              offset: 1,
              color: "rgba(57, 247, 255, 0.2)",
            },
          ]);
          let color2 = new proxy.echarts.graphic.LinearGradient(0, 0, 0, 1, [
            {
              offset: 0,
              color: "rgba(254,203,21, 1)",
            },
            {
              offset: 1,
              color: "rgba(254,203,21, 0.2)",
            },
          ]);
          if (parms.dataIndex < props.echartData.history.length) {
            return color1;
          } else {
            return color2;
          }
        },
      },
    },
  });
}
onMounted(() => {
  intChart();
  window.addEventListener("resize", resizeTheChart);
});
onBeforeUnmount(() => {
  myChart.value && myChart.value.dispose();
});
</script>

<style lang="scss" scoped>
.chartBox {
  width: 100%;
  height: 100%;
}
</style>