Newer
Older
HuangJiPC / src / pages / views / WatershedOneMap / components / barChart.vue
@zhangdeliang zhangdeliang on 21 Jun 3 KB update
<template>
  <div :id="id" style="width: 100%; height: 100%"></div>
</template>
<script>
import * as echarts from "echarts";
import { guid } from "@/utils/util";
import { reactive, toRefs, onMounted, watch } from "vue";
export default {
  name: "bar-chart",
  props: {
    // XData: Array,
    // SData: Array,
    // DataName: String,
    // DataUnit: String,
  },
  setup(props) {
    console.log(props.XData);
    const state = reactive({
      id: guid(),
      chart: null,
    });
    const resizeTheChart = () => {
      if (state.chart) {
        state.chart.resize();
      }
    };
    const init = (XData, SData, DataName, DataUnit) => {
      let chartDom = echarts.init(document.getElementById(state.id));
      var option;
      option = {
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "shadow",
          },
        },
        grid: {
          top: "14%",
          left: "3%",
          right: "3%",
          bottom: "5%",
          containLabel: true,
        },
        xAxis: {
          type: "category",
          data: XData,
          splitLine: {
            show: true,
          },
          axisLabel: {
            //y轴文字的配置
            textStyle: {
              color: "rgba(44, 117, 125, 1)",
              margin: 15,
            },
            // formatter: '{value} %'//y轴的每一个刻度值后面加上‘%’号
          },
        },
        yAxis: {
          type: "value",
          name: DataUnit,
          axisLabel: {
            //y轴文字的配置
            textStyle: {
              color: "rgba(44, 117, 125, 1)",
              margin: 15,
            },
            // formatter: '{value} %'//y轴的每一个刻度值后面加上‘%’号
          },
        },
        series: [
          {
            name: DataName,
            data: SData,
            type: "line",
            // barWidth: 1,
            itemStyle: {
              normal: {
                barBorderRadius: [10, 10, 0, 0],
                color: new echarts.graphic.LinearGradient(
                  0,
                  1,
                  0,
                  0, // 0,0,1,0表示从左向右    0,0,0,1表示从右向左
                  [
                    { offset: 1, color: "rgba(44, 254, 252, 1)" },
                    { offset: 0, color: "rgba(44, 254, 252, 0)" },
                  ]
                ),
              },
            },
          },
          // {
          //   name: "瞬时出水量",
          //   data: [111, 202, 140, 92, 60, 130],
          //   type: "bar",
          //   barWidth: 10,
          //   itemStyle: {
          //     normal: {
          //       barBorderRadius: [10, 10, 0, 0],
          //       color: new echarts.graphic.LinearGradient(
          //         0,
          //         1,
          //         0,
          //         0, // 0,0,1,0表示从左向右    0,0,0,1表示从右向左
          //         [
          //           { offset: 1, color: "rgba(233, 15, 15, 1)" },
          //           { offset: 0, color: "rgba(233, 15, 15, 0)" },
          //         ]
          //       ),
          //     },
          //   },
          // },
        ],
      };

      option && chartDom.setOption(option, true);
      state.chart = chartDom;
    };
    onMounted(() => {
      init();
      window.addEventListener("resize", resizeTheChart);
    });
    return {
      ...toRefs(state),
      resizeTheChart,
      init,
    };
  },
};
</script>