Newer
Older
Nanping_sponge_JXKH / src / components / Echarts / lineChart.vue
@liyingjing liyingjing on 25 Oct 4 KB 海绵绩效考个
<template>
  <div :id="id" style="width: 100%; height: 100%"></div>
</template>
<script setup>
import { guid } from "@/utils/ruoyi";
const { proxy } = getCurrentInstance();
const id = guid();
const myChart = shallowRef("");

const props = defineProps({
  //刷新标志
  refresh: {
    type: [String, Number],
    default: 1,
  },
  //标题
  title: {
    type: String,
    default: "",
  },
  //数据
  echartData: {
    type: Object,
    default: {},
  },
});
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));
  //   //获取湿度的最大值和最小值
  //   let shiDuMax = Math.max(...props.data.ydata2);
  //   let shiDuMin = Math.min(...props.data.ydata2);
  //   //温度的最大值和最小值
  //   let temMax = Math.max(...props.data.ydata1);
  //   let temMin = Math.min(...props.data.ydata1);

  // 绘制图表
  myChart.value.setOption({
    tooltip: {
      trigger: "axis",
      position: function (pt) {
        return [pt[0], "10%"];
      },
    },
    title: {
      left: "left",
      text: "",
      top: 10,
      bottom: 10,
      left: 10,
      textStyle: {
        color: "#545E75", //字体颜色
        fontSize: 16, //字体大小
      },
    },
    grid: [
      {
        left: 60,
        right: 50,
        height: "35%",
      },
      {
        left: 60,
        right: 50,
        top: "55%",
        height: "35%",
      },
    ],
    toolbox: {
      feature: {
        // dataZoom: {
        //   yAxisIndex: "none",
        // },
        // restore: {},
        // saveAsImage: {},
      },
    },
    axisPointer: {
      link: [
        {
          xAxisIndex: "all",
        },
      ],
    },
    xAxis: [
      {
        type: "category",
        boundaryGap: false,
        axisLine: { onZero: true },
        data: props.echartData.xAxisData,
        show: false,
      },
      {
        gridIndex: 1,
        type: "category",
        boundaryGap: false,
        axisLine: { onZero: true },
        data: props.echartData.xAxisData,
        position: "bottom",
      },
    ],
    yAxis: [
      {
        name: props.echartData.yAxisName1,
        type: "value",
        nameLocation: "end", // 坐标轴名称显示位置
        nameGap: 15, // 坐标轴名称与轴线之间的距离
        nameTextStyle: {
          color: "#545E75", //字体颜色
          fontSize: 16, //字体大小
          align: "left", // 文字水平对齐方式,默认自动('left','center','right')
          fontWeight: "bold",
        },
      },
      {
        gridIndex: 1,
        name: props.echartData.yAxisName2,
        type: "value",
        nameLocation: "end", // 坐标轴名称显示位置
        nameGap: 15, // 坐标轴名称与轴线之间的距离
        nameTextStyle: {
          color: "#545E75", //字体颜色
          fontSize: 16, //字体大小
          align: "left", // 文字水平对齐方式,默认自动('left','center','right')
          fontWeight: "bold",
        },
      },
    ],
    dataZoom: [
      {
        show: true,
        realtime: true,
        start: 30,
        end: 70,
        xAxisIndex: [0, 1],
      },
      {
        type: "inside",
        realtime: true,
        start: 30,
        end: 70,
        xAxisIndex: [0, 1],
      },
    ],
    series: [
      {
        name: props.echartData.seriesName,
        type: "line",
        symbolSize: 8,
        data: props.echartData.seriesData1,
        itemStyle: {
          color: "rgb(255, 70, 131)",
        },
        areaStyle: {
          color: proxy.echarts.graphic.LinearGradient(0, 0, 0, 1, [
            {
              offset: 0,
              color: "rgb(255, 158, 68)",
            },
            {
              offset: 1,
              color: "rgb(255, 70, 131)",
            },
          ]),
        },
      },
      {
        name: props.echartData.seriesName,
        type: "line",
        xAxisIndex: 1,
        yAxisIndex: 1,
        symbolSize: 8,
        data: props.echartData.seriesData2,
        itemStyle: {
          color: "rgb(255, 70, 131)",
        },
        areaStyle: {
          color: proxy.echarts.graphic.LinearGradient(0, 0, 0, 1, [
            {
              offset: 0,
              color: "rgb(255, 158, 68)",
            },
            {
              offset: 1,
              color: "rgb(255, 70, 131)",
            },
          ]),
        },
      },
    ],
  });
}
onMounted(() => {
  intChart();
  window.addEventListener("resize", resizeTheChart);
});
</script>