chart.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. import Humanize from "humanize-plus"
  2. import echarts from "echarts/lib/echarts"
  3. import "echarts/theme/macarons"
  4. import "echarts/lib/chart/bar"
  5. import "echarts/lib/chart/pie"
  6. import "echarts/lib/component/tooltip"
  7. import "echarts/lib/component/title"
  8. function DrawTrafficChart(elementId, trafficIn, trafficOut) {
  9. let myChart = echarts.init(document.getElementById(elementId), 'macarons');
  10. myChart.showLoading()
  11. let option = {
  12. title: {
  13. text: 'Network Traffic',
  14. subtext: 'today',
  15. x: 'center'
  16. },
  17. tooltip: {
  18. trigger: 'item',
  19. formatter: function(v) {
  20. return Humanize.fileSize(v.data.value) + " (" + v.percent + "%)"
  21. }
  22. },
  23. series: [{
  24. type: 'pie',
  25. radius: '55%',
  26. center: ['50%', '60%'],
  27. data: [{
  28. value: trafficIn,
  29. name: 'Traffic In'
  30. }, {
  31. value: trafficOut,
  32. name: 'Traffic Out'
  33. }, ],
  34. itemStyle: {
  35. emphasis: {
  36. shadowBlur: 10,
  37. shadowOffsetX: 0,
  38. shadowColor: 'rgba(0, 0, 0, 0.5)'
  39. }
  40. }
  41. }]
  42. };
  43. myChart.setOption(option);
  44. myChart.hideLoading()
  45. }
  46. function DrawProxyChart(elementId, serverInfo) {
  47. if (serverInfo.proxy_type_count.tcp == null) {
  48. serverInfo.proxy_type_count.tcp = 0
  49. }
  50. if (serverInfo.proxy_type_count.udp == null) {
  51. serverInfo.proxy_type_count.udp = 0
  52. }
  53. if (serverInfo.proxy_type_count.http == null) {
  54. serverInfo.proxy_type_count.http = 0
  55. }
  56. if (serverInfo.proxy_type_count.https == null) {
  57. serverInfo.proxy_type_count.https = 0
  58. }
  59. if (serverInfo.proxy_type_count.stcp == null) {
  60. serverInfo.proxy_type_count.stcp = 0
  61. }
  62. if (serverInfo.proxy_type_count.xtcp == null) {
  63. serverInfo.proxy_type_count.xtcp = 0
  64. }
  65. let myChart = echarts.init(document.getElementById(elementId), 'macarons')
  66. myChart.showLoading()
  67. let option = {
  68. title: {
  69. text: 'Proxies',
  70. subtext: 'now',
  71. x: 'center'
  72. },
  73. tooltip: {
  74. trigger: 'item',
  75. formatter: function(v) {
  76. return v.data.value
  77. }
  78. },
  79. series: [{
  80. type: 'pie',
  81. radius: '55%',
  82. center: ['50%', '60%'],
  83. data: [{
  84. value: serverInfo.proxy_type_count.tcp,
  85. name: 'TCP'
  86. }, {
  87. value: serverInfo.proxy_type_count.udp,
  88. name: 'UDP'
  89. }, {
  90. value: serverInfo.proxy_type_count.http,
  91. name: 'HTTP'
  92. }, {
  93. value: serverInfo.proxy_type_count.https,
  94. name: 'HTTPS'
  95. }, {
  96. value: serverInfo.proxy_type_count.stcp,
  97. name: 'STCP'
  98. }, {
  99. value: serverInfo.proxy_type_count.xtcp,
  100. name: 'XTCP'
  101. }],
  102. itemStyle: {
  103. emphasis: {
  104. shadowBlur: 10,
  105. shadowOffsetX: 0,
  106. shadowColor: 'rgba(0, 0, 0, 0.5)'
  107. }
  108. }
  109. }]
  110. };
  111. myChart.setOption(option);
  112. myChart.hideLoading()
  113. }
  114. // 7 days
  115. function DrawProxyTrafficChart(elementId, trafficInArr, trafficOutArr) {
  116. let params = {
  117. width: '600px',
  118. height: '400px'
  119. }
  120. let myChart = echarts.init(document.getElementById(elementId), 'macarons', params);
  121. myChart.showLoading()
  122. trafficInArr = trafficInArr.reverse()
  123. trafficOutArr = trafficOutArr.reverse()
  124. let now = new Date()
  125. now = new Date(now.getFullYear(), now.getMonth(), now.getDate() - 6)
  126. let dates = new Array()
  127. for (let i = 0; i < 7; i++) {
  128. dates.push(now.getFullYear() + '-' + (now.getMonth() + 1) + '-' + now.getDate())
  129. now = new Date(now.getFullYear(), now.getMonth(), now.getDate() + 1)
  130. }
  131. let option = {
  132. tooltip: {
  133. trigger: 'axis',
  134. axisPointer: {
  135. type: 'shadow'
  136. },
  137. formatter: function(data) {
  138. let html = ''
  139. if (data.length > 0) {
  140. html += data[0].name + '<br/>'
  141. }
  142. for (let v of data) {
  143. let colorEl = '<span style="display:inline-block;margin-right:5px;' +
  144. 'border-radius:10px;width:9px;height:9px;background-color:' + v.color + '"></span>';
  145. html += colorEl + v.seriesName + ': ' + Humanize.fileSize(v.value) + '<br/>'
  146. }
  147. return html
  148. }
  149. },
  150. legend: {
  151. data: ['Traffic In', 'Traffic Out']
  152. },
  153. grid: {
  154. left: '3%',
  155. right: '4%',
  156. bottom: '3%',
  157. containLabel: true
  158. },
  159. xAxis: [{
  160. type: 'category',
  161. data: dates
  162. }],
  163. yAxis: [{
  164. type: 'value',
  165. axisLabel: {
  166. formatter: function(value) {
  167. return Humanize.fileSize(value)
  168. }
  169. }
  170. }],
  171. series: [{
  172. name: 'Traffic In',
  173. type: 'bar',
  174. data: trafficInArr
  175. }, {
  176. name: 'Traffic Out',
  177. type: 'bar',
  178. data: trafficOutArr
  179. }]
  180. };
  181. myChart.setOption(option);
  182. myChart.hideLoading()
  183. }
  184. export {
  185. DrawTrafficChart,
  186. DrawProxyChart,
  187. DrawProxyTrafficChart
  188. }