Newer
Older
HuangJiPC / src / pages / views / server / component / LineChart.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: "line-chart",
  props: {
    xData: Array,
    data: Object,
  },
  setup(props) {
    console.log(props.data.dataList, "uuuuuuuuuuu");
    console.log(props.data.nameList, "uuuuuuuuuuu");
    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));
      var option;
      option = {
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "shadow",
          },
        },
        grid: {
          top: "14%",
          left: "3%",
          right: "3%",
          bottom: "5%",
          containLabel: true,
        },
        legend: {
          show: false,
        },
        xAxis: {
          type: "category",
          data: props.xData,
          show: false,
        },
        yAxis: {
          type: "value",
          splitNumber: 2,
          axisLine: {
            show: false,
          },
          splitLine: {
            show: false,
          },
          axisLabel: {
            color: "#0DB5CA",
          },
        },
        dataZoom: [
          {
            // 第一个 dataZoom 组件
            type: "inside",
            xAxisIndex: 0, // 表示这个 dataZoom 组件控制 第一个 xAxis
            startValue: 0, // 数据窗口范围的起始数值index
            endValue: 15, // 数据窗口范围的结束数值index
          },
        ],
        series: [
          {
            data: props.data.dataList[0],
            type: "line",
            smooth: false,
            symbol: "circle",
            symbolSize: [6, 6],
            itemStyle: {
              normal: {
                color: "#0DB5CA",
                lineStyle: {
                  color: "#ccc",
                },
              },
            },
          },
        ],
      };

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