SystemChart.vue 4.45 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import * as echarts from "echarts";
require("echarts/theme/macarons"); // echarts theme
import resize from "./mixins/resize";
import { getDateTimeString } from "@/utils/utils";
import { wsNotify } from "@/utils/eventBus.js";

const seriesData = {
  free_size: [],
  free_space_size: [],
  used_space_size: [],
};

wanli's avatar
wanli committed
18 19
let chart = null

20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: "chart",
    },
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "270px",
    },
    autoResize: {
      type: Boolean,
      default: true,
    },
    chartData: {
      type: Object,
      required: true,
    },
    dataList: {
      type: Array,
      required: false,
      default: () => [],
    },
  },
  data() {
    return {
      series: [
        {
          name: "free_size",
          type: "line",
          showSymbol: false,
          emphasis: {
            scale: false,
            focus: "series",
            blurScope: "coordinateSystem",
          },
          data: seriesData.free_size,
        },
        {
          name: "free_space_size",
          type: "line",
          showSymbol: false,
          emphasis: {
            focus: "series",
            blurScope: "coordinateSystem",
          },
          data: seriesData.free_space_size,
        },
        {
          name: "used_space_size",
          type: "line",
          showSymbol: false,
          emphasis: {
            focus: "series",
            blurScope: "coordinateSystem",
          },
          data: seriesData.used_space_size,
        },
      ],
wanli's avatar
wanli committed
84
      legendData: Object.keys(seriesData),
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
    };
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.handleMessage(val);
      },
    },
    dataList: {
      deep: true,
      handler(val) {
        if (val.length > 0) this.handleData(val);
      },
    },
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart();
    });

106 107 108 109 110 111 112 113 114
    wsNotify.eventBus.$on("exportImg", () => {
      let picInfo = chart.getDataURL({
        type: "png",
        pixelRatio: 1.5,
        backgroundColor: "#fff",
      });
      wsNotify.eventBus.$emit("exported", { type: "system", data: picInfo });
    });

115
    wsNotify.eventBus.$on("resize", () => {
wanli's avatar
wanli committed
116
      if (chart) chart.resize()
117
    });
wanli's avatar
wanli committed
118 119

    wsNotify.eventBus.$on("clear-system-chart", () => {
120
      chart.clear()
wanli's avatar
wanli committed
121 122
      this.setOptions()
    })
123 124
  },
  beforeDestroy() {
wanli's avatar
wanli committed
125
    if (!chart) {
126 127
      return;
    }
wanli's avatar
wanli committed
128 129
    chart.dispose();
    chart = null;
130 131 132 133 134 135 136 137 138
  },
  methods: {
    handleData(data) {
      Object.keys(seriesData).forEach(key => {
        seriesData[key] = []
      });
      this.series.forEach(item => {
        item.data = []
      });
wanli's avatar
wanli committed
139 140
      // chart.dispose();
      chart.setOption({ series: this.series });
141 142 143 144 145 146 147 148 149

      data.forEach((item) => {
        this.handleMessage(item);
      });
    },
    handleMessage(data) {
      Object.keys(data).forEach((k) => {
        var t = getDateTimeString(new Date());
        if (k == "timestamp") t = data[k];
wanli's avatar
wanli committed
150
        if (this.legendData.includes(k)) {
151 152 153 154
          seriesData[k].push({
            name: k,
            value: [t, data[k]],
          });
wanli's avatar
wanli committed
155
        }
156 157 158
      });

      this.$nextTick(() => {
wanli's avatar
wanli committed
159 160
        chart &&
          chart.setOption({
161 162 163 164 165
            series: this.series,
          });
      });
    },
    initChart() {
wanli's avatar
wanli committed
166
      chart = echarts.init(this.$el, "macarons");
167 168 169
      this.setOptions();
    },
    setOptions() {
wanli's avatar
wanli committed
170
      chart.setOption({
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
        title: {
          text: "SYSTEM",
        },
        grid: {
          left: 10,
          right: 10,
          bottom: 10,
          top: 50,
          containLabel: true,
        },
        xAxis: {
          type: "time",
          splitLine: {

          },
          axisLabel: {
            formatter: "{HH}:{mm}:{ss}",
          },
        },
        yAxis: {
          type: "value",
          // boundaryGap: [0, "100%"],
          splitLine: {

          },
        },
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "cross",
            animation: false,
          },
          padding: [5, 10],
        },
        legend: {
          data: this.legendData,
          selected: {
            free_size: true,
            free_space_size: true,
            used_space_size: false
          },
        },
        series: this.series,
      });
    },
  },
};
</script>