【Vue中使用Echarts】大屏可視化項目整體佈局(pink老師vue 版)

 

 

一、效果展示

先看一下展示的效果,無論是尺寸多寬的屏幕,都會將內容顯示完整,做到了正正的響應式。唯一不足的是圖表中的樣例,會隨着圖表的縮放而變換位置,窗口尺寸變化過快會反應不過來,好在有節流函數,可以讓瀏覽器計算量沒有那麼大。本篇博客不會直接拿echarts圖表下手,會先介紹一些這個大屏可視化的響應式佈局。後面會出一個專門的博客介紹echarts的使用。

 

 

二、基本的佈局

大致的佈局如下,整體分爲頭部與body,頭部有標題與時間兩部分,body分爲三個子標籤,使用flex佈局分別佔3\5\3份,然後在佔3份的標籤內又分爲三部分,佔5份的標籤內分爲兩部分。

寫入樣式之後就有了下面的樣子

此時需要將前面封裝的畫圖組件插入到指定的位置。得到下面的結果

 

三、背景

可以看出。這有一個在轉的地球,地球有一個比較亮的描邊,還有一些網格狀的東西罩在上面。
地球與網狀格格順時針旋轉,光邊逆時針旋轉,這種效果使用的是動畫效果與過渡效果實現,樣式代碼如下:
map1、map2、map3盒子的背景分別是地球、描邊、網格。

.map1,
.map2,
.map3 {
  position: absolute;
  top: 50%;
  left: 50%;
  background-size: 100% 100%;
  background-repeat: no-repeat;
  transform: translate(-50%, -50%);
  width: 6.475rem;
  height: 6.475rem;
  opacity: 0.3;
}
.map1 {
  background-image: url(../../public/images/map.png);

  animation: rotate 15s linear infinite;
}
.map2 {
  width: 8.0375rem;
  height: 8.0375rem;
  background-image: url(../../public/images/lbx.png);

  opacity: 0.8;
  animation: rotate 5s linear infinite;
  z-index: 2;
}
.map3 {
  width: 7.075rem;
  height: 7.075rem;
  background-image: url(../../public/images/jt.png);

  animation: rotate1 10s linear infinite;
}
@keyframes rotate {
  from {
    transform: translate(-50%, -50%) rotate(0deg);
  }
  to {
    transform: translate(-50%, -50%) rotate(360deg);
  }
}
@keyframes rotate1 {
  from {
    transform: translate(-50%, -50%) rotate(0deg);
  }
  to {
    transform: translate(-50%, -50%) rotate(-360deg);
  }
}

 

 

 

四、代碼

//以下面一個panel作爲例子進行講解
<Panel
  mychart="echart1"
  :option="chartsList[0]"
  title="柱形圖 - 就業形式"
></Panel>
//這個標籤控制一個圖表,mychart是圖表將要掛載的ref,option是圖表的配置項,title是圖表的標題。

Mainbox.vue整體佈局

<template>
  <div class="mainbox">
    <!-- 左邊的圖形展示 -->
    <div class="column">
      <Panel
        mychart="echart1"
        :option="chartsList[0]"
        title="柱形圖 - 就業形式"
      ></Panel>
      <Panel
        mychart="echart2"
        :option="chartsList[2]"
        title="曲線圖-人員變化"
      ></Panel>
      <Panel
        mychart="echart3"
        :option="chartsList[4]"
        title="南丁格爾圖-地區分佈"
      ></Panel>
    </div>

    <!-- 中間的圖形展示 -->
    <div class="column">
      <!-- 首頁數字展示 -->
      <div class="mainboxtop">
        <div class="tophd">
          <ul>
            <li>999999999+</li>
            <li>200+</li>
          </ul>
        </div>
        <div class="topbd">
          <ul>
            <li>前端需求人數</li>
            <li>市場供應人數</li>
          </ul>
        </div>
      </div>
      <!-- 首頁地圖展示 -->
      <div class="mainboxbody">
        <div ref="chinamap" class="chinamap"></div>
        <!-- 背景地球 -->
        <div class="map1"></div>
        <!-- 正旋轉 -->
        <div class="map2"></div>
        <!-- 逆旋轉 -->
        <div class="map3"></div>
      </div>
    </div>
    <!-- 右邊的圖形展示 -->
    <div class="column">
      <Panel
        mychart="echart4"
        :option="chartsList[1]"
        title="技能佔比-進度條"
      ></Panel>
      <Panel
        mychart="echart5"
        :option="chartsList[3]"
        title="曲線圖-播放量"
      ></Panel>
      <Panel mychart="echart6" :option="chartsList[5]" title="餅圖"></Panel>
    </div>
  </div>
</template>

<script>
// import MyEcharts from "./MyEcharts.vue";
// import "../../node_modules/echarts/dist/china.js";
import "../../node_modules/echarts/map/js/china.js";
// import "../config/chinamap.js";
// import "../config/china.js";

import * as echarts from "echarts";
import Panel from "./Panel.vue";
var yearData = [
];

var geoCoordMap = {
 
};

var XAData = [
  
];

var XNData = [

];

var YCData = [
  
];

var planePath =
  "path://M1705.06,1318.313v-89.254l-319.9-221.799l0.073-208.063c0.521-84.662-26.629-121.796-63.961-121.491c-37.332-0.305-64.482,36.829-63.961,121.491l0.073,208.063l-319.9,221.799v89.254l330.343-157.288l12.238,241.308l-134.449,92.931l0.531,42.034l175.125-42.917l175.125,42.917l0.531-42.034l-134.449-92.931l12.238-241.308L1705.06,1318.313z";
//var planePath = 'arrow';
var convertData = function (data) {
  var res = [];
  for (var i = 0; i < data.length; i++) {
    var dataItem = data[i];

    var fromCoord = geoCoordMap[dataItem[0].name];
    var toCoord = geoCoordMap[dataItem[1].name];
    if (fromCoord && toCoord) {
      res.push({
        fromName: dataItem[0].name,
        toName: dataItem[1].name,
        coords: [fromCoord, toCoord],
        value: dataItem[1].value,
      });
    }
  }
  return res;
};

var color = ["#a6c84c", "#ffa022", "#46bee9"]; //航線的顏色
var series = [];
[
  ["西安", XAData],
  ["西寧", XNData],
  ["銀川", YCData],
].forEach(function (item, i) {
  series.push(
    {
      name: item[0] + " Top3",
      type: "lines",
      zlevel: 1,
      effect: {
        show: true,
        period: 6,
        trailLength: 0.7,
        color: "red", //arrow箭頭的顏色
        symbolSize: 3,
      },
      lineStyle: {
        normal: {
          color: color[i],
          width: 0,
          curveness: 0.2,
        },
      },
      data: convertData(item[1]),
    },
    {
      name: item[0] + " Top3",
      type: "lines",
      zlevel: 2,
      symbol: ["none", "arrow"],
      symbolSize: 10,
      effect: {
        show: true,
        period: 6,
        trailLength: 0,
        symbol: planePath,
        symbolSize: 15,
      },
      lineStyle: {
        normal: {
          color: color[i],
          width: 1,
          opacity: 0.6,
          curveness: 0.2,
        },
      },
      data: convertData(item[1]),
    },
    {
      name: item[0] + " Top3",
      type: "effectScatter",
      coordinateSystem: "geo",
      zlevel: 2,
      rippleEffect: {
        brushType: "stroke",
      },
      label: {
        normal: {
          show: true,
          position: "right",
          formatter: "{b}",
        },
      },
      symbolSize: function (val) {
        return val[2] / 8;
      },
      itemStyle: {
        normal: {
          color: color[i],
        },
        emphasis: {
          areaColor: "#2B91B7",
        },
      },
      data: item[1].map(function (dataItem) {
        return {
          name: dataItem[1].name,
          value: geoCoordMap[dataItem[1].name].concat([dataItem[1].value]),
        };
      }),
    }
  );
});
var option = {
  tooltip: {
    trigger: "item",
    formatter: function (params, ticket, callback) {
      if (params.seriesType == "effectScatter") {
        return "線路:" + params.data.name + "" + params.data.value[2];
      } else if (params.seriesType == "lines") {
        return (
          params.data.fromName +
          ">" +
          params.data.toName +
          "<br />" +
          params.data.value
        );
      } else {
        return params.name;
      }
    },
  },
  legend: {
    orient: "vertical",
    top: "bottom",
    left: "right",
    data: ["西安 Top3", "西寧 Top3", "銀川 Top3"],
    textStyle: {
      color: "#fff",
    },
    selectedMode: "multiple",
  },
  geo: {
    map: "china",
    label: {
      emphasis: {
        show: true,
        color: "#fff",
      },
    },
    // 把中國地圖放大了1.2倍
    zoom: 1.2,
    roam: true,
    itemStyle: {
      normal: {
        // 地圖省份的背景顏色
        areaColor: "rgba(20, 41, 87,0.6)",
        borderColor: "#195BB9",
        borderWidth: 1,
      },
      emphasis: {
        areaColor: "#2B91B7",
      },
    },
  },
  series: series,
};
export default {
  components: { Panel },
  name: "MainBox",
  data() {
    return {
    //這裏存放圖表
      chartsList: [],
      mycharts: null,
      chartFun: null,
    };
  },
  mounted() {
    // this.$refs.echart1.setOption(this.chartsList[0]);
    // this.$refs.echart2.setOption(this.chartsList[0]);
    if (this.mycharts) {
      this.mycharts.dispose();
    }
    // this.mycharts = echarts.init(document.getElementsByClassName("chinamap"));
    this.mycharts = echarts.init(this.$refs.chinamap);
    this.mycharts.setOption(option);
    let chart = this.mycharts;
    // 節流函數
    function throttle(func, wait, options) {
      let time, context, args, result;
      let previous = 0;
      if (!options) options = {};
      let later = function () {
        previous = options.leading === false ? 0 : new Date().getTime();
        time = null;
        func.apply(context, args);
        if (!time) context = args = null;
      };

      let throttled = function () {
        let now = new Date().getTime();
        if (!previous && options.leading === false) previous = now;
        let remaining = wait - (now - previous);
        context = this;
        args = arguments;
        if (remaining <= 0 || remaining > wait) {
          if (time) {
            clearTimeout(time);
            time = null;
          }
          previous = now;
          func.apply(context, args);
          if (!time) context = args = null;
        } else if (!time && options.trailing !== false) {
          time = setTimeout(later, remaining);
        }
      };
      return throttled;
    }
    this.chartFun = throttle(function () {
      chart.resize();
    }, 10);
    window.addEventListener("resize", this.chartFun);
  },
  beforeDestroy() {
    // 移除窗口改變監聽
    window.removeEventListener("resize", this.chartFun);
  },
};
</script>

<style scoped>
.mainbox {
  display: flex;
  /* background-color: pink; */
  padding: 0.125rem 0.125rem 0;
}
.column {
  /* height: 10rem; */
  /* background-color: red; */
  flex: 3;
}
.mainbox .column:nth-child(2) {
  /* background-color: blue; */
  padding: 0 0.125rem 0.1875rem;
  /* background-color: blue; */
  flex: 5;
}

/* 以下是存放中國地圖的容器樣式 */
.mainboxtop {
  background-color: rgba(101, 132, 226, 0.1);
  padding: 0.1875rem;
}
.tophd {
  position: relative;
  border: 1px solid rgba(25, 186, 139, 0.17);
}
@font-face {
  font-family: electronicFont;
  src: url(../../public/font/DS-DIGIT.TTF);
}
.tophd > ul,
.topbd > ul {
  display: flex;
}
.tophd > ul > li {
  flex: 1;
  display: inline-block;
  height: 1rem;
  color: #ffeb7b;
  font-size: 0.875rem;
  font-family: electronicFont;
}
.tophd > ul::after {
  position: absolute;
  right: 50%;
  width: 0.0125rem;
  height: 50%;
  top: 25%;
  background-color: rgba(255, 255, 255, 0.2);
  content: "";
}
.tophd::before,
.tophd::after {
  position: absolute;
  content: "";
  width: 0.375rem;
  height: 0.125rem;
}
.tophd::before {
  top: 0;
  left: 0;
  border-top: 2px solid #02a6b5;
  border-left: 2px solid #02a6b5;
}
.tophd::after {
  bottom: 0;
  right: 0;
  border-bottom: 2px solid #02a6b5;
  border-right: 2px solid #02a6b5;
}

.topbd > ul > li {
  flex: 1;
  height: 0.5rem;
  line-height: 0.5rem;
  color: rgba(255, 255, 255, 0.7);
  font-size: 0.225rem;
  padding-top: 0.125rem;
}

.mainboxbody {
  position: relative;
  width: 100%;
  height: 10.125rem;
  /* background-color: pink; */
}
.chinamap {
  position: absolute;
  top: 0;
  left: 0;
  z-index: 5;
  height: 10.125rem;
  width: 100%;
}

</style>

Header.vue用於管理佈局中的頭部。

<template>
  <div class="header">
    <h1>大屏數據可視化展示</h1>
    <div class="showtime">時間</div>
  </div>
</template>

<script>
export default {
  name: "Header",
  mounted() {
    var t = null;
    t = setTimeout(time, 1000); //開始運行
    function time() {
      clearTimeout(t); //清除定時器
      let dt = new Date();
      var y = dt.getFullYear();
      var mt = dt.getMonth() + 1;
      var day = dt.getDate();
      var h = dt.getHours(); //獲取時
      var m = dt.getMinutes(); //獲取分
      var s = dt.getSeconds(); //獲取秒
      document.querySelector(".showtime").innerHTML =
        y + "/" + mt + "/" + day + " -" + h + ":" + m + ":" + s;
      t = setTimeout(time, 1000); //設定定時器,循環運行
    }
  },
};
</script>

<style scoped>
.header {
  position: relative;
  width: 100%;
  height: 1.25rem;
  /* background-color: pink; */
  background: url(../../public/images/head_bg.png) no-repeat;
  background-position: top center;
  background-size: cover;
}
h1 {
  color: #fff;
  text-align: center;
  line-height: 1rem;
  font-size: 0.475rem;
}
.showtime {
  overflow: hidden;
  width: 4.5625rem;
  height: 1rem;
  position: absolute;
  top: 0;
  right: 0.375rem;
  line-height: 0.9375rem;
  font-size: 0.25rem;
  color: rgba(255, 255, 255, 0.7);
}
</style>

佈局中遇到的一些問題
下面是在佈局的時候遇到的一些問題,可以參考一下:

Echarts:There is a chart instance already initialized on the dom.//重複給一個dom元素畫圖

echarts警告:Can‘t get DOM width or height. Please check dom.clientWidth and dom.clientHeight. …//沒有給盒子寬高

Uncaught TypeError: Cannot read properties of undefined (reading ‘echarts’)//沒有找到echarts中的china.js
將china.js文件放入echarts.js所在的目錄

Error in mounted hook: “TypeError: this.dom.getContext is not a function”
一開始是使用jQuery獲取dom,一直報上面的錯誤,後來改變用vue的ref獲取就可以了
使用document.getelementById()獲取也會報錯

./src/components/Home.vue Module not found: Error: Can’t resolve ‘less-loader’ in 'C:\Users\123\Desk
//使用了less語法,沒有裝less相關插件,執行npm install [email protected] -D

 

https://blog.csdn.net/apple_51931783/article/details/128312569

 

 

pink老師js大屏

https://gitee.com/xiaoqiang001/eckarts_open_class

https://www.bilibili.com/video/BV1v7411R7mp?p=1&vd_source=45ac2ea8c9b3ba6a602adf5486cef4cd

 

 

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