echartsUtil.ts 7.7 KB

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