echarts折線圖📈從入門到放棄

樣例數據源

CPU數據

[["19:28:11", 0.30],["19:28:12", 6.35],["19:28:13", 7.35],["19:28:14", 7.22],["19:28:15", 7.42],["19:28:16", 6.57],["19:28:17", 5.75],["19:28:18", 6.47],["19:28:19", 6.22],["19:28:20", 6.10],["19:28:21", 6.07],["19:28:22", 5.90],["19:28:23", 5.68],["19:28:24", 5.77],["19:28:25", 6.38],["19:28:26", 5.28],["19:28:27", 6.67],["19:28:28", 5.90],["19:28:29", 6.17],["19:28:30", 5.20],["19:28:31", 5.90],["19:28:32", 7.47],["19:28:33", 6.93],["19:28:34", 6.88],["19:28:35", 8.10],["19:28:36", 7.48],["19:28:37", 7.27],["19:28:38", 5.60],["19:28:39", 5.70],["19:28:40", 5.57],["19:28:41", 5.60],["19:28:42", 5.83],["19:28:43", 5.93],["19:28:44", 6.53],["19:28:45", 6.45],["19:28:46", 6.67],["19:28:47", 6.02],["19:28:48", 6.73],["19:28:49", 6.47],["19:28:50", 6.03],["19:28:51", 5.35],["19:28:52", 5.58],["19:28:53", 5.45],["19:28:54", 5.70],["19:28:55", 6.42],["19:28:56", 7.45],["19:28:57", 8.12],["19:28:58", 8.03],["19:28:59", 6.25],["19:29:00", 6.47],["19:29:01", 6.38],["19:29:02", 7.08],["19:29:03", 6.78],["19:29:04", 6.25],["19:29:05", 5.88],["19:29:06", 6.33],["19:29:07", 6.75]]

內存數據

[["19:28:11", 13.17],["19:28:12", 15.61],["19:28:13", 18.72],["19:28:14", 18.94],["19:28:15", 19.17],["19:28:16", 19.42],["19:28:17", 19.94],["19:28:18", 16.69],["19:28:19", 16.11],["19:28:20", 16.05],["19:28:21", 16.05],["19:28:22", 15.84],["19:28:23", 17.16],["19:28:24", 16.94],["19:28:25", 17.38],["19:28:26", 16.92],["19:28:27", 17.97],["19:28:28", 18.02],["19:28:29", 27.70],["19:28:30", 18.80],["19:28:31", 18.61],["19:28:32", 17.77],["19:28:33", 16.58],["19:28:34", 16.56],["19:28:35", 15.97],["19:28:36", 16.24],["19:28:37", 16.03],["19:28:38", 14.88],["19:28:39", 14.83],["19:28:40", 14.83],["19:28:41", 14.86],["19:28:42", 16.00],["19:28:43", 14.84],["19:28:44", 16.03],["19:28:45", 14.89],["19:28:46", 16.05],["19:28:47", 16.27],["19:28:48", 16.03],["19:28:49", 16.03],["19:28:50", 16.03],["19:28:51", 16.03],["19:28:52", 16.03],["19:28:53", 16.06],["19:28:54", 16.05],["19:28:55", 16.25],["19:28:56", 16.03],["19:28:57", 16.03],["19:28:58", 16.30],["19:28:59", 14.94],["19:29:00", 14.91],["19:29:01", 14.88],["19:29:02", 16.56],["19:29:03", 15.49],["19:29:04", 16.61],["19:29:05", 16.61],["19:29:06", 16.61],["19:29:07", 16.42]]
<!-- run -->
<div id="app">
  <div class="container">
    <div id="input">
      <textarea
        v-model="cpu"
        name="cpu"
        id="cpu-input"
        cols="30"
        rows="30"
        placeholder="請在這裏填寫CPU數據"
      ></textarea>
      <span style="margin-left: 15px"> </span>
      <textarea
        v-model="memery"
        name="memery"
        id="memery-input"
        cols="30"
        rows="30"
        placeholder="請在這裏填寫內存數據"
      ></textarea>
    </div>
    <div class="file-select-bg">
      <div>
      <label for="file">選擇CPU數據: </label>
      <input type="file" accept=".txt" name="選擇CPU數據" id="selectCPU" @change="selectCpuData">
    </div>
    <div>
      <label for="file">選擇內存數據: </label>
      <input type="file" accept=".txt" name="選擇內存數據" id="selectMem" @change="selectMemData">
    </div>
    </div>
    
    <div class="button" @click="clickAction">生成圖表</div>
    <div id="cpu"></div>
    <div class="tips" v-show="showCpuTips">最大值: <strong>{{cpuDict.max}}</strong>% 最小值: <strong>{{cpuDict.min}}</strong>%  平均值: <strong>{{cpuDict.avg.toFixed(2)}}</strong>%</div>
    <div id="memery"></div>
    <div class="tips" v-show="showMemeryTips">最大值: <strong>{{memeryDict.max}}</strong>MB 最小值: <strong>{{memeryDict.min}}</strong>MB  平均值: <strong>{{memeryDict.avg.toFixed(2)}}</strong>MB</div>
<div class="tips"> 大寫的尷尬😓,博客園搞不了echarts,就這吧 不折騰了 </div>
  </div>
</div>

<script src="https://cdn.bootcss.com/echarts/5.2.1/echarts.min.js"></script>
<script>
import * as echarts from 'echarts'
Vue.prototype.$echarts = echarts
new Vue({ 
  echarts,
  el: "#app",
  data() {
    return {
      showCpuTips:false,
      showMemeryTips:false,
      cpu: "",
      memery: "",
      cpuData: [],
      memeryData: [],
      cpuDict:{
        min:0.0,
        max:1.0,
        avg:0.0
      },
      memeryDict:{
        min:0.0,
        max:0.0,
        avg:0.0
      }
    };
  },
  mounted() {},
  methods: {
    selectCpuData(){
    var self = this;
    var file = document.getElementById("selectCPU").files[0];
    var reader = new FileReader();
		reader.readAsText(file,'utf-8');
    reader.onload = function () {  
       self.cpu = this.result 
       this.cpuData = JSON.parse(this.cpu);
       this.drawTab(true);
    }
  },
    selectMemData(){
    var self = this;
    var file = document.getElementById("selectMem").files[0];
    var reader = new FileReader();
		reader.readAsText(file,'utf-8');
    reader.onload = function () {  
       self.memery = this.result 
       this.memeryData = JSON.parse(this.memery);
       this.drawTab(false);
    }
    },
    clickAction() {
      if (this.cpu.length) {
        this.cpuData = JSON.parse(this.cpu);
        this.drawTab(true);
      }
      if (this.memery.length) {
        this.memeryData = JSON.parse(this.memery);
        this.drawTab(false);
      }
    },
    getMaxValue(arr){
      var max = arr[0];
      for(var i=1;i<arr.length;i++){ 
        if(max<arr[i]) max=arr[i];
      }
      return max;
    },
    getMinValue(arr){
      var min = arr[0];
      for(var i=1;i<arr.length;i++){ 
        if(min>arr[i]) min=arr[i];
      }
      return min;
    },
    getAvgValue(arr){
      var sum = 0;
      for(var i=1;i<arr.length;i++){ 
          sum+=arr[i];
      }
      return sum/arr.length;
    },
    parserCpuData(){
      if(!this.cpuData.length){
        return
      }
      var cpuArr = []
      const localData = this.cpuData;
      for (let index = 0; index < localData.length; index++) {
           const element = localData[index];
           const cpu = element[1];
           cpuArr.push(cpu);
      }
      const max = this.getMaxValue(cpuArr);
      const min = this.getMinValue(cpuArr);
      const avg = this.getAvgValue(cpuArr);
      this.cpuDict = {
        max,
        min,
        avg
      };
      this.showCpuTips = true;
    },
    parserMemeryData(){
      if(!this.memeryData.length){
        return
      }
      var memArr = []
      const localData = this.memeryData;
      for (let index = 0; index < localData.length; index++) {
           const element = localData[index];
           const mem = element[1];
           memArr.push(mem);
      }
      const max = this.getMaxValue(memArr);
      const min = this.getMinValue(memArr);
      const avg = this.getAvgValue(memArr);
      this.memeryDict = {
        max,
        min,
        avg
      };
      this.showMemeryTips = true;
    },

    //https://echarts.apache.org/examples/zh/editor.html?c=line-gradient
    drawTab(isDrawCPU) {
      let title = isDrawCPU ? "CPU性能變化趨勢圖" : "內存性能變化趨勢圖";
      this.$echarts.init(document.getElementById(isDrawCPU
        ? 'cpu':'memery')).dispose(); 
      let myChart = isDrawCPU
        ? this.$echarts.init(document.getElementById("cpu"))
        : this.$echarts.init(document.getElementById("memery"));

      let tabData = isDrawCPU ? this.cpuData : this.memeryData;
      const dateList = tabData.map(function (item) {
        return item[0];
      });
      const valueList = tabData.map(function (item) {
        return item[1];
      });
      this.parserCpuData();
      this.parserMemeryData();
      const formatter = isDrawCPU? "CPU佔用 <span style='color:red'> {c}% </span> " : "內存佔用 <span style='color:red'> {c}MB </span> " 

      let option = {
        visualMap: [
          {
            show: false,
            type: "continuous",
            seriesIndex: 0,
            dimension: 0,
            min: 0,
            max: dateList.length - 1,
          },
        ],
        title: [
          {
            top: "5%",
            left: "center",
            text: title,
          },
        ],
        tooltip: {
          trigger: "axis",
          formatter: formatter,
          axisPointer: {
	       type: "cross",
	       crossStyle: {
		    color: "#0cf",
		    type: "solid"
		     },
	       },
	     show:true
        },
        xAxis: [
          {
            data: dateList,
            name:"時間"
          },
        ],
        yAxis: [
          {
            gridIndex: 0,
            name:isDrawCPU? "CPU(%)":"內存(MB)"
          },
        ],
        grid: [
          {
            top: "20%",
          },
        ],
        series: [
          {
            type: "line",
            showSymbol: true,
            data: valueList,
            xAxisIndex: 0,
            yAxisIndex: 0,
          },
        ],
      };
      myChart.setOption(option,true);
    },
  }
});
</script>
<style>
#app {
  width: 100%,
  height: 1280px; 
}
.button {
  margin: 30px;
  margin-left: 150px;
  font-size: 25px;
  color: white;
  text-align: center;
  width: 180px;
  height: 50px;
  line-height: 50px;
  cursor: pointer;
  border-radius: 5px;
  background-color: #7799ff;
}
.container {
  width: 100%;
  height: 100%;
}
#cpu {
  width: 300px;
  height: 400px;
}
#memery {
  margin-top: 50px;
  width: 300px;
  height: 400px;
}
.tips{
  margin-top: -10px;
  margin-bottom: 100px;
  margin-left: 10px;
  font-size: 20px;
  color: orangered;
}
.file-select-bg{
  width: 800px;
  height: 60px;
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: space-around;
  flex: 1;
}
</style>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章