Newer
Older
HuangJiPC / src / pages / views / performance / components / BarChart.vue
@zhangdeliang zhangdeliang on 21 Jun 2 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: "moreBar",
  props: {
    data: Object,
  },
  setup(props) {
    const state = reactive({
      id: guid(),
      chart: null,
    });
    const resizeTheChart = () => {
      if (state.chart) {
        state.chart.resize();
      }
    };
    const init = () => {
      let chartDom = echarts.init(document.getElementById(state.id));
      chartDom.clear();
      let option;
      option = {
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "shadow",
          },
        },
        legend: {
          bottom: '5%'
        },
        grid: {
          top: "10%",
          left: "3%",
          right: "3%",
          bottom: "15%",
          containLabel: true,

        },
        xAxis: {
          type: "value",
          name: '%'
        },
        yAxis: {
          type: "category",
          data: props.data.yData,
        },
        series: [
          {
            name: "工单完成率",
            type: "bar",
            data: props.data.xData,
            barWidth: 20,
            itemStyle: {
              normal: {
                color: new echarts.graphic.LinearGradient(
                  0,
                  1,
                  0,
                  0, // 0,0,1,0表示从左向右    0,0,0,1表示从右向左
                  [
                    { offset: 0, color: "rgba(23, 183, 255, 1)" },
                    { offset: 1, color: "rgba(3, 108, 255, 1)" },
                  ]
                ),
              },
            },
          },
        ],
      };
      option && chartDom.setOption(option, true);
      state.chart = chartDom;
    };
    onMounted(() => {
      init();
      window.addEventListener("resize", resizeTheChart);
    });
    return {
      ...toRefs(state),
      resizeTheChart,
      init,
    };
  },
};
</script>