vue echarts動態數據定時刷新

關鍵代碼截圖

 

 

 

完整代碼

<template>
  <div class="block">
    <div :class="className" :id="id" :style="{height:height,width:width}" />
  </div>
</template>

<script>
import echarts from "echarts";
import resize from "./mixins/resize";

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: "chart"
    },
    id: {
      type: String,
      default: "chart"
    },
    text: {
      type: String,
      default: "text"
    },
    value: {
      type: Number
    },

    max: {
      type: Number,
      default: 100
    },

    mid: {
      type: Number,
      default: 8
    },

    min: {
      type: Number,
      default: 0
    },

    midbule: {
      type: Number,
      default: 15
    },

    width: {
      type: String,
      default: "330px"
    },
    legendshow: {
      type: Boolean,
      default: false
    },
    height: {
      type: String,
      default: "330px"
    }
  },

  watch: {
    value: {
      handler(newVal, oldVal) {
        console.log("tag", newVal);
        console.log("oldVal: ", oldVal);
        this.option = {
          tooltip: {
            formatter: "{a} <br/>{b} : {c}kpa"
          },

          series: [
            {
              name: " ",
              type: "gauge",
              min: this.min, // 最小的數據值,默認 0 。映射到 minAngle。
              max: this.max, // 最大的數據值,默認 100 。映射到 maxAngle。
              // startAngle: 180,
              axisLine: {
                // 座標軸線
                lineStyle: {
                  // 屬性lineStyle控制線條樣式
                  // color: [[0.2, "#c23531"], [0.8, "#63869e"], [1, "#91c7ae"]]

                  //        :min="60"
                  // :value="95"
                  // :mid="86"
                  // :midbule="95"
                  // :max="106"

                  color: [
                    [this.mid / this.max, "#c23531"],
                    [1 - (this.max - this.midbule) / this.max, "#63869e"],
                    [1, "#91c7ae"]
                  ]
                  // color: [
                  //   [(this.mid - this.min) / (this.max  - this.min), "#c23531"],
                  //   [1 - ( this.max - this.min)- (this.midbule -this.min) /( this.max - this.min), "#63869e"],
                  //   [1, "#91c7ae"]
                  //
                }
              },

              title: {
                show: true,
                offsetCenter: [0, "80%"], // x, y,單位px
                textStyle: {
                  color: "#hhh",
                  fontSize: 24
                }
              },

              detail: { formatter: "{value}" },
              data: [{ value: this.value, name: this.text }]
            }
          ]
        };

        this.chart.setOption(this.option);
      },
      deep: true //對象內部屬性的監聽,關鍵。
    }
  },
  data() {
    return {
      chart: null,
      option: {
        tooltip: {
          formatter: "{a} <br/>{b} : {c}kpa"
        },

        series: [
          {
            name: " ",
            type: "gauge",
            min: this.min, // 最小的數據值,默認 0 。映射到 minAngle。
            max: this.max, // 最大的數據值,默認 100 。映射到 maxAngle。
            // startAngle: 180,
            axisLine: {
              // 座標軸線
              lineStyle: {
                // 屬性lineStyle控制線條樣式
                // color: [[0.2, "#c23531"], [0.8, "#63869e"], [1, "#91c7ae"]]

                //        :min="60"
                // :value="95"
                // :mid="86"
                // :midbule="95"
                // :max="106"

                color: [
                  [this.mid / this.max, "#c23531"],
                  [1 - (this.max - this.midbule) / this.max, "#63869e"],
                  [1, "#91c7ae"]
                ]
                // color: [
                //   [(this.mid - this.min) / (this.max  - this.min), "#c23531"],
                //   [1 - ( this.max - this.min)- (this.midbule -this.min) /( this.max - this.min), "#63869e"],
                //   [1, "#91c7ae"]
                //
              }
            },

            title: {
              show: true,
              offsetCenter: [0, "80%"], // x, y,單位px
              textStyle: {
                color: "#hhh",
                fontSize: 24
              }
            },

            detail: { formatter: "{value}" },
            data: [{ value: this.value, name: this.text }]
          }
        ]
      }
    };
  },
  mounted() {
    this.initChart();
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    initChart() {
      this.chart = echarts.init(document.getElementById(this.id));

      this.chart.setOption(this.option);
    }
  }
};
</script>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章