LvglChart.vue 5.31 KB
Newer Older
wanli's avatar
wanli committed
1 2 3 4 5 6 7 8 9
<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";
wanli's avatar
wanli committed
10
import { wsNotify } from "@/utils/eventBus.js";
wanli's avatar
wanli committed
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34

const seriesData = {
  frag_pct: [],
  free_biggest_size: [],
  free_cnt: [],
  free_size: [],
  total_size: [],
  used_cnt: [],
  used_pct: [],
};

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: "chart",
    },
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
wanli's avatar
wanli committed
35
      default: "270px",
wanli's avatar
wanli committed
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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
    },
    autoResize: {
      type: Boolean,
      default: true,
    },
    chartData: {
      type: Object,
      required: true,
    },
    dataList: {
      type: Array,
      required: false,
      default: () => [],
    },
  },
  data() {
    return {
      chart: null,
      series: [
        {
          name: "frag_pct",
          type: "line",
          showSymbol: false,
          emphasis: {
            scale: false,
            focus: "series",
            blurScope: "coordinateSystem",
          },
          data: seriesData.frag_pct,
        },
        {
          name: "free_biggest_size",
          type: "line",
          showSymbol: false,
          emphasis: {
            focus: "series",
            blurScope: "coordinateSystem",
          },
          data: seriesData.free_biggest_size,
        },
        {
          name: "free_cnt",
          type: "line",
          showSymbol: false,
          emphasis: {
            focus: "series",
            blurScope: "coordinateSystem",
          },
          data: seriesData.free_cnt,
        },
        {
          name: "free_size",
          type: "line",
          showSymbol: false,
          emphasis: {
            focus: "series",
            blurScope: "coordinateSystem",
          },
          data: seriesData.free_size,
        },
        {
          name: "total_size",
          type: "line",
          showSymbol: false,
          emphasis: {
            focus: "series",
            blurScope: "coordinateSystem",
          },
          data: seriesData.total_size,
        },
        {
          name: "used_cnt",
          type: "line",
          showSymbol: false,
          emphasis: {
            focus: "series",
            blurScope: "coordinateSystem",
          },
          data: seriesData.used_cnt,
        },
        {
wanli's avatar
wanli committed
117
          name: "used_pct",
wanli's avatar
wanli committed
118 119 120 121 122 123
          type: "line",
          showSymbol: false,
          emphasis: {
            focus: "series",
            blurScope: "coordinateSystem",
          },
wanli's avatar
wanli committed
124
          data: seriesData.used_pct,
wanli's avatar
wanli committed
125 126 127 128 129 130 131 132 133
        },
      ],
      legendData: [
        "frag_pct",
        "free_biggest_size",
        "free_cnt",
        "free_size",
        "total_size",
        "used_cnt",
wanli's avatar
wanli committed
134
        "used_pct",
wanli's avatar
wanli committed
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
      ],
    };
  },
  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();
    });
wanli's avatar
wanli committed
156 157 158 159

    wsNotify.eventBus.$on("resize", () => {
      if (this.chart) this.chart.resize()
    });
wanli's avatar
wanli committed
160 161 162 163 164 165 166 167 168 169 170 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
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    handleData(data) {
      Object.keys(seriesData).forEach(key => {
        seriesData[key] = []
      });
      this.series.forEach(item => {
        item.data = []
      });
      this.chart.setOption({ series: this.series });

      data.forEach((item) => {
        this.handleMessage(item);
      });
    },
    handleMessage(data) {
      Object.keys(data).forEach((k) => {
        var t = getDateTimeString(new Date());
        if (k == "timestamp") t = data[k];
        if (this.legendData.includes(k))
          seriesData[k].push({
            name: k,
            value: [t, data[k]],
          });
      });

      this.$nextTick(() => {
        this.chart &&
          this.chart.setOption({
            series: this.series,
          });
      });
    },
    initChart() {
      this.chart = echarts.init(this.$el, "macarons");
      this.setOptions();
    },
    setOptions() {
      this.chart.setOption({
        title: {
          text: "LVGL",
        },
wanli's avatar
wanli committed
209 210 211 212 213 214 215
        grid: {
          left: 10,
          right: 10,
          bottom: 10,
          top: 50,
          containLabel: true,
        },
wanli's avatar
wanli committed
216 217 218
        xAxis: {
          type: "time",
          splitLine: {
wanli's avatar
wanli committed
219

wanli's avatar
wanli committed
220 221 222 223 224 225 226 227 228
          },
          axisLabel: {
            formatter: "{HH}:{mm}:{ss}",
          },
        },
        yAxis: {
          type: "value",
          // boundaryGap: [0, "100%"],
          splitLine: {
wanli's avatar
wanli committed
229

wanli's avatar
wanli committed
230 231 232 233 234 235 236 237 238 239 240 241
          },
        },
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "cross",
            animation: false,
          },
          padding: [5, 10],
        },
        legend: {
          data: this.legendData,
wanli's avatar
wanli committed
242 243 244 245 246 247 248
          selected: {
            frag_pct: false,
            free_biggest_size: false,
            free_cnt: false,
            free_size: false,
            total_size: false,
          },
wanli's avatar
wanli committed
249 250 251 252 253 254 255
        },
        series: this.series,
      });
    },
  },
};
</script>