LineCharts.vue 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <template>
  2. <div class="line-content">
  3. <div id="chart1" class="line-chart" v-loading="loading1"></div>
  4. <div id="chart2" class="line-chart" v-loading="loading2"></div>
  5. <div id="chart3" class="line-chart" v-loading="loading3"></div>
  6. </div>
  7. </template>
  8. <script>
  9. import moment from 'moment'
  10. // import resize from '../../service/mixins/resize'
  11. import storage from '@/utils/storage'
  12. import { getChartData } from '@/api/service'
  13. export default {
  14. data() {
  15. return {
  16. myChartScale1: null,
  17. myChartScale2: null,
  18. myChartScale3: null,
  19. subListQuery:{
  20. start_time: 0,
  21. end_time: 0,
  22. app_alias: ''
  23. },
  24. loading1: false,
  25. loading2: false,
  26. loading3: false
  27. }
  28. },
  29. watch:{
  30. '$store.state.time.globalTimes':{
  31. handler(newValue, oldValue) {
  32. if (newValue){
  33. this.subListQuery.start_time= newValue.startTime;
  34. this.subListQuery.end_time= newValue.endTime;
  35. this.drawEchartsScale()
  36. }
  37. }
  38. }
  39. },
  40. created(){
  41. let appItem = storage.get('appsItem');
  42. if(JSON.stringify(appItem) !="{}"){
  43. this.subListQuery.start_time = appItem.start_time;
  44. this.subListQuery.end_time = appItem.end_time
  45. this.subListQuery.app_alias = appItem.alias
  46. }
  47. },
  48. mounted() {
  49. this.$nextTick(() =>{
  50. this.drawEchartsScale()
  51. })
  52. },
  53. methods: {
  54. getChartDataFn(i){
  55. return new Promise((resolve, reject)=>{
  56. const rowItem = storage.get('row');
  57. let obj ={
  58. '请求趋势(QPS)': 'request_trend',
  59. '延迟分位(ms)': 'latency',
  60. '状态码占比': 'status_trend'
  61. }
  62. let par = {
  63. start_time:this.subListQuery.start_time, // 开始时间
  64. end_time:this.subListQuery.end_time, // 结束时间
  65. app_alias:this.subListQuery.app_alias, // 应用别名
  66. type:obj[i], // 类型,request_trend(默认)、status_trend、latency
  67. service_name:rowItem.service_name, // 服务名
  68. method:rowItem.method, // 请求方法
  69. route:rowItem.route, // 请求路由
  70. }
  71. getChartData(par).then(res=>{
  72. resolve(res)
  73. }).catch(error=>{
  74. reject(error)
  75. })
  76. })
  77. },
  78. async drawEchartsScale() {
  79. //绘制趋势echarts
  80. let obj = {
  81. 1: '请求趋势(QPS)',
  82. 2: '延迟分位(ms)',
  83. 3: '状态码占比'
  84. // 4: 'HTTP Bandwidth(KB)'
  85. // 5: 'HTTP Status 4xx Percent Per Minute(%)',
  86. // 6: 'HTTP Status 5xx Percent Per Minute(%)',
  87. }
  88. for(let i = 1; i <=3; i++){
  89. this[`loading${i}`] = true
  90. if(this[`myChartScale${i}`] == null) {
  91. this[`myChartScale${i}`] = this.$echarts5.init(document.getElementById(`chart${i}`));
  92. this[`myChartScale${i}`].resize();
  93. }
  94. const res = await this.getChartDataFn(obj[i])
  95. if(res.code == 200){
  96. this[`loading${i}`] = false
  97. let data = res.data
  98. data.text = obj[i]
  99. let option = this.getOption(data, obj[i])
  100. this[`myChartScale${i}`].setOption(option);
  101. this[`myChartScale${i}`].getZr().on('click', params => {
  102. let data = storage.get('paramsValue')
  103. let timestamp = moment(data[0].name).unix();
  104. this.$emit('chartClick', timestamp);
  105. })
  106. // 分别设置每个实例的 group id
  107. this[`myChartScale${i}`].group = 'group1';
  108. this.$echarts5.connect('group1');
  109. }
  110. }
  111. },
  112. getOption(data, type) {
  113. let option = {
  114. title:{
  115. top: 0,
  116. text:data.text,
  117. textStyle:{
  118. fontSize:14
  119. }
  120. },
  121. legend: {
  122. top: '10%',
  123. left: 0, // 设置水平位置为居中
  124. orient: 'horizontal' // 设置图例水平排布
  125. },
  126. tooltip: {
  127. trigger: 'axis',
  128. formatter: function (params) {
  129. storage.set('paramsValue', params)
  130. let axisValueLabel = params[0].axisValueLabel
  131. let str0 = '';
  132. params.forEach((item, idx) => {
  133. str0 += `${item.marker}${item.seriesName}<span style='margin-left:30px;text-align:right;font-weight:400'>${item.data}</span>`
  134. switch (idx) {
  135. case 0:
  136. str0;
  137. break;
  138. case 1:
  139. str0;
  140. break;
  141. default:
  142. str0
  143. }
  144. str0 += idx === params.length - 1 ? '' : '<br/>'
  145. })
  146. return axisValueLabel + '<br>' + str0
  147. }
  148. },
  149. // toolbox: {
  150. // show: true,
  151. // top: '8%',
  152. // feature: {
  153. // dataZoom: { //数据区域缩放。目前只支持直角坐标系的缩放
  154. // show: false, //是否显示该工具。
  155. // },
  156. // dataView: { //数据视图工具,可以展现当前图表所用的数据,编辑后可以动态更新
  157. // show: false
  158. // },
  159. // magicType: { type: ['line', 'bar'] },
  160. // restore: {},//配置项还原。
  161. // saveAsImage: { // 保存为图片。
  162. //  show: false, //是否显示该工具。
  163. // }
  164. // }
  165. // },
  166. grid: {
  167. left: '4%',
  168. right: '8%',
  169. bottom: 0,
  170. containLabel: true
  171. },
  172. xAxis: {
  173. type: 'category',
  174. data: data.times,
  175. axisLabel: {
  176. interval: data.times.length > 10 ? (data.times.length > 40 ? 8: 3): 0, //每间隔显示一个坐标
  177. formatter: function(params) {
  178. let newParams = moment(params).format('MM-DD HH:mm');
  179. let newArr = newParams.split(' ')
  180. let time = newArr[0] + "\n" + newArr[1]
  181. return time;
  182. }
  183. }
  184. },
  185. yAxis: {
  186. type: 'value',
  187. scale: true,
  188. gridIndex: 0,
  189. axisLabel: {
  190. formatter: '{value}'
  191. },
  192. axisLine: {
  193. show: true
  194. },
  195. axisTick: {
  196. show: true
  197. },
  198. splitLine: {
  199. show: true
  200. },
  201. },
  202. series: []
  203. };
  204. for(let key in data.values){
  205. let tempType = type == '状态码占比' ? 'bar':'line'
  206. let obj = {
  207. name: key,
  208. type: tempType,
  209. data:data.values[key]
  210. }
  211. option.series.push(obj)
  212. }
  213. return option
  214. }
  215. }
  216. }
  217. </script>
  218. <style lang="scss" scoped>
  219. .line-content{
  220. display: flex;
  221. justify-content: space-between;
  222. flex-direction: row;
  223. flex-flow:row wrap;
  224. .line-chart{
  225. width: 33%;
  226. height:300px;
  227. margin-bottom: 20px;
  228. }
  229. }
  230. </style>