network.vue 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. <template>
  2. <div class="network">
  3. <p class="title">流量</p>
  4. <ul>
  5. <li
  6. class="rx"
  7. @click="change('上行')"
  8. :class="[chartOptions.legend.selected['上行'] ? 'on' : 'off']"
  9. >
  10. <p>上行</p>
  11. <p>{{ tx_sec | unit_size }}</p>
  12. </li>
  13. <li
  14. class="tx"
  15. @click="change('下载')"
  16. :class="[chartOptions.legend.selected['下载'] ? 'on' : 'off']"
  17. >
  18. <p>下载</p>
  19. <p>{{ rx_sec | unit_size }}</p>
  20. </li>
  21. <li class="send">
  22. <p>总发送</p>
  23. <p>{{ tx_bytes | unit_size }}</p>
  24. </li>
  25. <li class="receive">
  26. <p>总接收</p>
  27. <p>{{ rx_bytes | unit_size }}</p>
  28. </li>
  29. </ul>
  30. <vue-echarts v-if="visible" autoresize :options="chartOptions"></vue-echarts>
  31. </div>
  32. </template>
  33. <script>
  34. import echarts from "echarts";
  35. import Common from "./common";
  36. export default {
  37. mixins: [Common],
  38. props: {
  39. data: null
  40. },
  41. data() {
  42. return {
  43. visible: true,
  44. chartOptions: {
  45. tooltip: {
  46. trigger: "axis",
  47. axisPointer: {
  48. lineStyle: {
  49. color: "#ddd"
  50. }
  51. },
  52. backgroundColor: "rgba(255,255,255,1)",
  53. padding: [5, 10],
  54. textStyle: {
  55. color: "#7588E4"
  56. },
  57. extraCssText: "box-shadow: 0 0 5px rgba(0, 0, 0, 0.3)",
  58. formatter: (arr) => {
  59. return arr
  60. .map((e, i) => (i == 0 ? "上行:" : "下载:") + this.conByte(e.value))
  61. .join("<br>");
  62. }
  63. },
  64. legend: {
  65. data: ["上行", "下载"],
  66. selected: {
  67. 上行: true,
  68. 下载: true
  69. },
  70. left: "1000%"
  71. },
  72. grid: {
  73. bottom: "5%",
  74. top: "10%",
  75. left: "10px",
  76. right: "10px",
  77. containLabel: true
  78. },
  79. xAxis: {
  80. type: "category",
  81. data: [],
  82. boundaryGap: false,
  83. splitLine: {
  84. show: true,
  85. interval: "auto",
  86. lineStyle: {
  87. color: ["#D4DFF5"]
  88. }
  89. },
  90. axisTick: {
  91. show: false
  92. },
  93. axisLine: {
  94. show: false,
  95. lineStyle: {
  96. color: "#609ee9"
  97. }
  98. },
  99. axisLabel: {
  100. margin: 10,
  101. textStyle: {
  102. fontSize: 14
  103. }
  104. }
  105. },
  106. yAxis: {
  107. type: "value",
  108. splitLine: {
  109. show: true,
  110. lineStyle: {
  111. color: ["#D4DFF5"]
  112. }
  113. },
  114. axisTick: {
  115. show: false
  116. },
  117. axisLine: {
  118. show: false,
  119. lineStyle: {
  120. color: "#609ee9"
  121. }
  122. },
  123. axisLabel: {
  124. margin: 10,
  125. textStyle: {
  126. fontSize: 14
  127. },
  128. formatter: (value) => {
  129. return this.conByte(value);
  130. }
  131. }
  132. },
  133. series: [
  134. {
  135. type: "line",
  136. showSymbol: false,
  137. symbol: "circle",
  138. symbolSize: 6,
  139. data: [],
  140. areaStyle: {
  141. normal: {
  142. color: new echarts.graphic.LinearGradient(
  143. 0,
  144. 0,
  145. 0,
  146. 1,
  147. [
  148. {
  149. offset: 0,
  150. color: "rgba(255, 202, 89, 0.5)"
  151. },
  152. {
  153. offset: 1,
  154. color: "rgba(255, 202, 89, 0.2)"
  155. }
  156. ],
  157. false
  158. )
  159. }
  160. },
  161. itemStyle: {
  162. normal: {
  163. color: "#f7b851"
  164. }
  165. },
  166. lineStyle: {
  167. normal: {
  168. width: 1
  169. }
  170. }
  171. },
  172. {
  173. type: "line",
  174. showSymbol: false,
  175. symbol: "circle",
  176. symbolSize: 6,
  177. data: [],
  178. areaStyle: {
  179. normal: {
  180. color: new echarts.graphic.LinearGradient(
  181. 0,
  182. 0,
  183. 0,
  184. 1,
  185. [
  186. {
  187. offset: 0,
  188. color: "rgba(216, 244, 247, 1)"
  189. },
  190. {
  191. offset: 1,
  192. color: "rgba(216, 244, 247, 1)"
  193. }
  194. ],
  195. false
  196. )
  197. }
  198. },
  199. itemStyle: {
  200. normal: {
  201. color: "#58c8da"
  202. }
  203. },
  204. lineStyle: {
  205. normal: {
  206. width: 1
  207. }
  208. }
  209. }
  210. ]
  211. },
  212. rx_bytes: 0,
  213. tx_bytes: 0,
  214. rx_sec: 0,
  215. tx_sec: 0
  216. };
  217. },
  218. methods: {
  219. change(n) {
  220. this.chartOptions.legend.selected[n] = !this.chartOptions.legend.selected[n];
  221. this.visible = false;
  222. this.$nextTick(() => {
  223. this.visible = true;
  224. });
  225. },
  226. refresh(res) {
  227. let { data = [], time } = res;
  228. let network = data[data.length - 1].server.network;
  229. this.rx_bytes = 0;
  230. this.tx_bytes = 0;
  231. network.forEach((e) => {
  232. this.rx_bytes += e.rx_bytes;
  233. this.tx_bytes += e.tx_bytes;
  234. });
  235. this.rx_sec = network.reduce((a, b) => a + b.rx_sec, 0);
  236. this.tx_sec = network.reduce((a, b) => a + b.tx_sec, 0);
  237. this.chartOptions.xAxis.data = time;
  238. this.chartOptions.series[0].name = "上行";
  239. this.chartOptions.series[0].data = data.map((e) =>
  240. e.server.network.reduce((a, b) => a + b.tx_sec, 0)
  241. );
  242. this.chartOptions.series[1].name = "下载";
  243. this.chartOptions.series[1].data = data.map((e) =>
  244. e.server.network.reduce((a, b) => a + b.rx_sec, 0)
  245. );
  246. }
  247. }
  248. };
  249. </script>
  250. <style lang="scss" scoped>
  251. .network {
  252. background-color: #fff;
  253. padding: 15px;
  254. .title {
  255. font-size: 16px;
  256. border-bottom: 1px solid #eee;
  257. padding-bottom: 15px;
  258. }
  259. ul {
  260. display: flex;
  261. margin-top: 30px;
  262. text-align: center;
  263. li {
  264. list-style: none;
  265. flex: 1;
  266. &.rx {
  267. color: #f7b851;
  268. }
  269. &.tx {
  270. color: #58c8da;
  271. }
  272. p {
  273. &:first-child {
  274. font-size: 12px;
  275. color: #999;
  276. }
  277. &:last-child {
  278. font-size: 15px;
  279. margin-top: 10px;
  280. }
  281. }
  282. }
  283. li:nth-last-child(n + 3) p {
  284. cursor: pointer;
  285. position: relative;
  286. &:hover {
  287. opacity: 0.8;
  288. }
  289. }
  290. li:nth-last-child(n + 3) p:first-child::before {
  291. display: block;
  292. content: "";
  293. height: 8px;
  294. width: 8px;
  295. border-radius: 8px;
  296. position: absolute;
  297. left: calc(50% - 26px);
  298. top: calc(50% - 4px);
  299. background-color: #eee;
  300. }
  301. li:nth-child(1).on p:first-child::before {
  302. background-color: #f7b851;
  303. }
  304. li:nth-child(2).on p:first-child::before {
  305. background-color: #58c8da;
  306. }
  307. }
  308. .echarts {
  309. height: 350px;
  310. width: 100%;
  311. }
  312. }
  313. </style>