echartsUtil.ts 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. import echarts from '/@/utils/lib/echarts';
  2. export default class echartsUtil {
  3. option: any;
  4. type: string;
  5. constructor(option) {
  6. this.option = option;
  7. }
  8. /**
  9. * 获取数据渲染echarts图表
  10. * @param type 类型目前两种 listMonitor(实时检测对应的图表)、history(历史数据对应的图表)
  11. * @param echartsComponent echarts组件类
  12. * @param chartcolumns EnumData文件对应图表类型配置属性
  13. * @param listData 从后台获取到的数据
  14. * @param devicetype 设备类型
  15. * @param columnname 某些特定设备类型下,从后台获取到的数据中,属性名为columnname的属性值存放的是x轴的信息
  16. */
  17. initChartOption(type, chartColumns: any[] = []) {
  18. if (!this.option) {
  19. return;
  20. }
  21. const xdata = [], // 存放x轴的数据
  22. ydata = [],
  23. yAxis: any[] = [], // 存放图表y轴样式、数据
  24. colors: string[] = [], // 存放每个图表系列的颜色
  25. legends: string[] = [], // 存放每个图表系列的名字
  26. series: any[] = []; // 存放每个图表系列的样式
  27. let xAxis: any[] = [], //存放图表x轴样式、数据
  28. timeline: any = null, //
  29. grid = {},
  30. tooltip = {},
  31. dataZoom: any = null; //进度条
  32. const columns = JSON.parse(JSON.stringify(chartColumns));
  33. columns.forEach((column: any) => {
  34. const ylist = [];
  35. if (type == 'detail' || type == 'history') {
  36. column.linetype = 'line';
  37. }
  38. if (column.color)
  39. // ydata.push(ylist);
  40. /** 获取静态文件配置的图表样式信息 */
  41. colors.push(column.color); //获取每个图表系列的颜色
  42. if (column.legend) legends.push(column.legend + (column.yname ? '(' + column.yname + ')' : '')); //获取每个图表系列的名字
  43. series.push(this.getSeries(column, ylist)); //获取每个图表系列的样式
  44. if (column.seriesName || column.seriesName == undefined) {
  45. yAxis.push(this.getYAxis(column));
  46. }
  47. });
  48. /* 如果是历史记录的话需要添加进度条 */
  49. grid = this.getGrid(yAxis, type);
  50. // timeline = this.getTimeline(xdata, ydata);
  51. tooltip = this.getTooltip();
  52. xAxis = this.getXAxis(xdata, series, type);
  53. dataZoom = this.getDataZoom(type);
  54. if (this.option) {
  55. Object.assign(this.option['tooltip'], tooltip);
  56. // this.option['tooltip'] = tooltip;
  57. this.option['grid'] = grid;
  58. // this.option['legend'] = this.getLegend(legends);
  59. Object.assign(this.option['legend'], this.getLegend(legends));
  60. this.option['xAxis'] = xAxis;
  61. this.option['yAxis'] = yAxis;
  62. this.option['series'] = series;
  63. this.option['dataZoom'] = dataZoom;
  64. }
  65. }
  66. getDataZoom(type) {
  67. if (type == 'history') {
  68. return [
  69. {
  70. bottom: '10',
  71. height: 20,
  72. start: 100,
  73. end: 0,
  74. textStyle: {
  75. color: '#ffffff',
  76. },
  77. },
  78. ];
  79. } else if (type == 'listMonitor' || type == 'detail') {
  80. return {
  81. start: 0,
  82. type: 'inside',
  83. };
  84. }
  85. return null;
  86. }
  87. getLegend(legend) {
  88. const legendObj = {
  89. textStyle: {
  90. color: '#ffffff', // 字体颜色
  91. },
  92. data: legend,
  93. top: '20',
  94. };
  95. return legendObj;
  96. }
  97. getXAxis(xdata, series, type) {
  98. let rotate = 0;
  99. const isHasBar = series.findIndex((item) => {
  100. if (item.xRotate != undefined) rotate = item.xRotate;
  101. return item.type == 'bar';
  102. });
  103. const xAxis = [
  104. {
  105. type: 'category',
  106. axisTick: {
  107. alignWithLabel: true,
  108. },
  109. axisLine: {
  110. lineStyle: {
  111. color: '#006c9d',
  112. width: 1, // 这里是为了突出显示加上的
  113. },
  114. },
  115. splitLine: { show: true, lineStyle: { color: 'rgba(21,80,126,.3)', type: 'dashed' } },
  116. axisLabel: {
  117. show: true,
  118. color: '#ffffffbb',
  119. rotate: rotate,
  120. },
  121. axisPointer: {
  122. type: isHasBar > -1 ? 'shadow' : 'line',
  123. shadowStyle: {
  124. color: 'rgba(0,0,0,0.1)',
  125. },
  126. },
  127. // prettier-ignore
  128. data: xdata,
  129. },
  130. ];
  131. return xAxis;
  132. }
  133. getYAxis(item) {
  134. const yAxisobj = {
  135. type: 'value',
  136. name: item.seriesName ? item.seriesName : item.legend,
  137. min: 0,
  138. max: item.ymax,
  139. position: item.yaxispos ? item.yaxispos : 'right',
  140. offset: item.yaxispos == 'right' ? (item.sort - 2) * 60 : 0,
  141. alignTicks: true,
  142. axisLine: {
  143. show: true,
  144. lineStyle: {
  145. color: '#006c9d',
  146. },
  147. },
  148. axisLabel: {
  149. show: true,
  150. color: '#ffffffcc',
  151. // formatter: '{value}' + item.yname
  152. },
  153. splitLine: {
  154. lineStyle: {
  155. color: 'rgba(21,80,126,.3)',
  156. type: 'dashed', //设置网格线类型 dotted:虚线 solid:实线
  157. },
  158. // show: item.linetype == 'line' ? true : false,
  159. show: true,
  160. },
  161. showBackground: true,
  162. backgroundStyle: {
  163. color: 'rgba(205, 95, 255, 1)',
  164. },
  165. };
  166. return yAxisobj;
  167. }
  168. getSeries(item, ylist) {
  169. const seriesObj = {
  170. name: item.legend + (item.yname ? '(' + item.yname + ')' : ''),
  171. type: item.linetype,
  172. yAxisIndex: item.sort - 1,
  173. barCategoryGap: '30%',
  174. data: [...ylist],
  175. barMaxWidth: '20',
  176. itemStyle: {
  177. color:
  178. item.linetype == 'bar'
  179. ? new echarts.graphic.LinearGradient(0, 0, 0, 1, [
  180. {
  181. offset: 0,
  182. color: item.color + 'ff',
  183. },
  184. {
  185. offset: 1,
  186. color: item.color + '33',
  187. },
  188. ])
  189. : item.color,
  190. borderRadius: [15, 15, 0, 0],
  191. },
  192. lineStyle: {
  193. shadowColor: '#ffffff99',
  194. shadowBlur: 3,
  195. },
  196. smooth: true,
  197. };
  198. return seriesObj;
  199. }
  200. getGrid(yAxis, type) {
  201. if (!this.option.grid) {
  202. let rightnum = 0,
  203. leftnum = 0;
  204. yAxis.forEach((item) => {
  205. if (item.position == 'right') {
  206. ++rightnum;
  207. } else if (item.position == 'left') {
  208. ++leftnum;
  209. }
  210. });
  211. const grid = {
  212. top: '60px',
  213. bottom: type == 'history' ? '40px' : '15px',
  214. right: rightnum * 30 + 20 + 'px',
  215. left: leftnum * 40 + 'px',
  216. containLabel: true,
  217. };
  218. return grid;
  219. } else {
  220. return this.option.grid;
  221. }
  222. }
  223. getTooltip() {
  224. const tooltip = {
  225. backgroundColor: '#00000005',
  226. borderColor: '#74E9FE44',
  227. extraCssText: 'backdrop-filter: blur(15px); box-shadow: 0 0 0 rgba(0, 0, 0, 0);',
  228. textStyle: {
  229. color: '#ffffff', // 字体颜色
  230. },
  231. trigger: 'axis',
  232. axisPointer: {
  233. label: {
  234. backgroundColor: 'rgba(30,120,50,0.8)',
  235. },
  236. type: 'cross',
  237. },
  238. };
  239. return tooltip;
  240. }
  241. // 分页显示数据
  242. getTimeline(xdata, ydata) {
  243. // 结合x、y轴的数据量判断是否要分页(x轴分页)ydata长度的倍数就是X轴要显示的数量n
  244. const n = Math.floor(20 / ydata.length);
  245. const size = Math.ceil(xdata.length / n); //分页数量
  246. if (size > 2) {
  247. // 设置时间轴
  248. const timeline = {
  249. axisType: 'category',
  250. // realtime: false,
  251. // loop: false,
  252. autoPlay: true,
  253. // currentIndex: 2,
  254. playInterval: 1000,
  255. // controlStyle: {
  256. // position: 'left'
  257. // },
  258. data: [],
  259. };
  260. timeline.data = Array.from(new Array(size).keys());
  261. return timeline;
  262. } else {
  263. return null;
  264. }
  265. }
  266. }