echarts折线图📈之性能数据展示在线工具

echarts在博客园上展示的挣扎过程

<!-- run -->
<div id="app">
 <div class="container">
      <div id="input">
        <textarea
          name="cpu"
          id="cpu-input"
          cols="30"
          rows="30"
          placeholder="请在这里填写CPU数据"
        ></textarea>
        <span style="margin-left: 15px"> </span>
        <textarea
          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"
            onchange="selectCpuData()"
          />
        </div>
        <div>
          <label for="file">选择内存数据: </label>
          <input
            type="file"
            accept=".txt"
            name="选择内存数据"
            id="selectMem"
            onchange="selectMemData()"
          />
        </div>
      </div>
      <div class="button" onclick="clickAction()">生成图表</div>
      <div id="cpu"></div>
      <div class="tips" id="cpu-tips"></div>
      <div id="memery"></div>
      <div class="tips" id="memery-tips"></div>
  </div>
</div>
<script src="https://cdn.bootcss.com/echarts/5.2.1/echarts.min.js"></script>
<script type="text/javascript">
      var cpuData = [];
      var memeryData = [];
  window.onload=function renderDefaultData(){
    document.getElementById("cpu-input").innerText = ' [["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]] ';
 document.getElementById("memery-input").innerText = '[["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]]';
    clickAction();
  }

      function selectCpuData() {
        var file = document.getElementById("selectCPU").files[0];
        var reader = new FileReader();
        reader.readAsText(file, "utf-8");
        reader.onload = function () {
          document.getElementById("cpu-input").innerText = this.result;
        };
      }
      function selectMemData() {
        var file = document.getElementById("selectMem").files[0];
        var reader = new FileReader();
        reader.readAsText(file, "utf-8");
        reader.onload = function () {
          document.getElementById("memery-input").innerText = this.result;
        };
      }

      function clickAction() {
        const cpuText = document.getElementById("cpu-input").value;
        if (cpuText.length) {
          cpuData = JSON.parse(cpuText);
        }
        if (cpuData.length) {
          parserCpuData();
          drawTab(true);
        }
        const memText = document.getElementById("memery-input").value;
        if (memText.length) {
          memeryData = JSON.parse(memText);
        }
        if (memeryData.length) {
          parserMemeryData();
          drawTab(false);
        }
      }

      function drawTab(isDrawCPU) {
        let title = isDrawCPU ? "CPU性能变化趋势图" : "内存性能变化趋势图";
        echarts
          .init(document.getElementById(isDrawCPU ? "cpu" : "memery"))
          .dispose();
        var myChart = isDrawCPU
          ? echarts.init(document.getElementById("cpu"))
          : echarts.init(document.getElementById("memery"));
        let tabData = isDrawCPU ? cpuData : memeryData;
        const dateList = tabData.map(function (item) {
          return item[0];
        });
        const valueList = tabData.map(function (item) {
          return item[1];
        });

        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);
      }

      function parserCpuData() {
        if (!cpuData.length) {
          return;
        }
        var cpuArr = [];
        for (let index = 0; index < cpuData.length; index++) {
          const element = cpuData[index];
          const cpu = element[1];
          cpuArr.push(cpu);
        }
        const max = this.getMaxValue(cpuArr);
        const min = this.getMinValue(cpuArr);
        const avg = this.getAvgValue(cpuArr);
        document.getElementById(
          "cpu-tips"
        ).innerHTML = `最大值:  <strong>${max}</strong>% 最小值:  <strong>${min}</strong>% 平均值:  <strong>${avg.toFixed(
          2
        )}</strong>% `;
      }
      function parserMemeryData() {
        if (!memeryData.length) {
          return;
        }
        var memArr = [];
        for (let index = 0; index < memeryData.length; index++) {
          const element = memeryData[index];
          const mem = element[1];
          memArr.push(mem);
        }
        const max = this.getMaxValue(memArr);
        const min = this.getMinValue(memArr);
        const avg = this.getAvgValue(memArr);
        document.getElementById(
          "memery-tips"
        ).innerHTML = `最大值:  <strong>${max}</strong>MB 最小值:  <strong>${min}</strong>MB 平均值:  <strong>${avg.toFixed(
          2
        )}</strong>MB `;
      }

      function getMaxValue(arr) {
        var max = arr[0];
        for (var i = 1; i < arr.length; i++) {
          if (max < arr[i]) max = arr[i];
        }
        return max;
      }

      function getMinValue(arr) {
        var min = arr[0];
        for (var i = 1; i < arr.length; i++) {
          if (min > arr[i]) min = arr[i];
        }
        return min;
      }

      function getAvgValue(arr) {
        var sum = 0;
        for (var i = 1; i < arr.length; i++) {
          sum += arr[i];
        }
        return sum / arr.length;
      }
 </script>
<style>
.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: 500px;
        height: 500px;
      }
      #memery {
        margin-top: 50px;
        width: 500px;
        height: 500px;
      }
      .tips {
        margin-top: -10px;
        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>
posted @ 2021-12-23 19:24  CoderWGB  阅读(309)  评论(0编辑  收藏  举报