yangfeng
2023-12-05 27fa305bf0c3a4405366a966c28b4d32203106b8
src/views/cockpitPage/components/DeviceChart.vue
@@ -15,37 +15,46 @@
<script>
//引入echart
import * as echarts from "echarts";
let myChart;
export default {
  components: {},
  props: {},
  props: {
    chartData: {
      type: Object,
      require: true,
      default: () => {
        return {
          datax: [],
          datay: [],
        };
      },
    },
  },
  data() {
    return {
      chartData: {
        datax: [
          "设备1",
          "设备2",
          "设备3",
          "设备4",
          "设备5",
          "设备6",
          "设备7",
          "设备8",
          "设备9",
        ],
        datay: [10, 20, 15, 38, 47, 50, 20, 33, 25, 48],
      },
      startValue: 0,
      endValue: 5,
      chartTimer: null,
    };
  },
  mounted() {
    this.pieChart("chart", this.chartData);
  watch: {
    "chartData.datay"(val) {
      this.startValue = 0;
      this.endValue = 5;
      this.pieChart("chart", this.chartData);
    },
  },
  watch: {},
  mounted() {},
  methods: {
    //在职
    pieChart(chartName, data) {
      let that = this;
      clearInterval(this.chartTimer);
      let chartDom = this.$refs[chartName];
      let myChart = echarts.init(chartDom);
      if (myChart != null && myChart != "" && myChart != undefined) {
        myChart.dispose(); //销毁
      }
      myChart = echarts.init(chartDom);
      let option;
      let lineColor = "#35ddc74d";
      if (data) {
@@ -68,8 +77,8 @@
              type: "inside",
              yAxisIndex: 0,
              show: false,
              startValue: 0, // 从头开始
              endValue: 5, // 一次性展示几个
              startValue: that.startValue, // 从头开始
              endValue: that.endValue, // 一次性展示几个
            },
          ],
          legend: {
@@ -104,6 +113,12 @@
                      return colorList[1];
                    }
                  },
                },
                formatter: function (value) {
                  if (value.length > 6) {
                    return `${value.slice(0, 4)}...`;
                  }
                  return value;
                },
              },
              data: data.datax ? data.datax : [],
@@ -176,11 +191,10 @@
          ],
        };
        setInterval(function () {
          // 每次向左滑动一个,最后一个从头开始。
          if (option.dataZoom[0].endValue == that.chartData.datay.length) {
            option.dataZoom[0].startValue = 0;
            option.dataZoom[0].endValue = 5;
        this.chartTimer = setInterval(function () {
          if (option.dataZoom[0].endValue == data.datay.length) {
            option.dataZoom[0].startValue = that.startValue;
            option.dataZoom[0].endValue = that.endValue;
          } else {
            option.dataZoom[0].endValue = option.dataZoom[0].endValue + 1;
            option.dataZoom[0].startValue = option.dataZoom[0].startValue + 1;