echartsUtil.ts 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. formatter: function (params) {
  126. var newParamsName = '';
  127. const paramsNameNumber = params.length;
  128. const provideNumber = 10; // 单行显示文字个数
  129. const rowNumber = Math.ceil(paramsNameNumber / provideNumber);
  130. if (paramsNameNumber > provideNumber) {
  131. for (let p = 0; p < rowNumber; p++) {
  132. var tempStr = '';
  133. var start = p * provideNumber;
  134. var end = start + provideNumber;
  135. if (p === rowNumber - 1) {
  136. tempStr = params.substring(start, paramsNameNumber);
  137. } else {
  138. tempStr = params.substring(start, end) + '\n';
  139. }
  140. newParamsName += tempStr;
  141. }
  142. } else {
  143. newParamsName = params;
  144. }
  145. return newParamsName;
  146. },
  147. },
  148. axisPointer: {
  149. type: isHasBar > -1 ? 'shadow' : 'line',
  150. shadowStyle: {
  151. color: 'rgba(0,0,0,0.1)',
  152. },
  153. },
  154. // prettier-ignore
  155. data: xdata,
  156. },
  157. ];
  158. return xAxis;
  159. }
  160. getYAxis(item) {
  161. const yAxisobj = {
  162. type: 'value',
  163. name: item.seriesName ? item.seriesName : item.legend,
  164. min: 0,
  165. max: item.ymax,
  166. position: item.yaxispos ? item.yaxispos : 'right',
  167. offset: item.yaxispos == 'right' ? (item.sort - 2) * 60 : 0,
  168. alignTicks: true,
  169. axisLine: {
  170. show: true,
  171. lineStyle: {
  172. color: '#006c9d',
  173. },
  174. },
  175. axisLabel: {
  176. show: true,
  177. color: '#ffffffcc',
  178. // formatter: '{value}' + item.yname
  179. },
  180. splitLine: {
  181. lineStyle: {
  182. color: 'rgba(21,80,126,.3)',
  183. type: 'dashed', //设置网格线类型 dotted:虚线 solid:实线
  184. },
  185. // show: item.linetype == 'line' ? true : false,
  186. show: true,
  187. },
  188. showBackground: true,
  189. backgroundStyle: {
  190. color: 'rgba(205, 95, 255, 1)',
  191. },
  192. };
  193. return yAxisobj;
  194. }
  195. getSeries(item, ylist) {
  196. const seriesObj = {
  197. name: item.legend + (item.yname ? '(' + item.yname + ')' : ''),
  198. type: item.linetype,
  199. yAxisIndex: item.sort - 1,
  200. barCategoryGap: '30%',
  201. data: [...ylist],
  202. barMaxWidth: '20',
  203. itemStyle: {
  204. color:
  205. item.linetype == 'bar'
  206. ? new echarts.graphic.LinearGradient(0, 0, 0, 1, [
  207. {
  208. offset: 0,
  209. color: item.color + 'ff',
  210. },
  211. {
  212. offset: 1,
  213. color: item.color + '33',
  214. },
  215. ])
  216. : item.color,
  217. borderRadius: [15, 15, 0, 0],
  218. },
  219. lineStyle: {
  220. shadowColor: '#ffffff99',
  221. shadowBlur: 3,
  222. },
  223. smooth: true,
  224. };
  225. return seriesObj;
  226. }
  227. getGrid(yAxis, type) {
  228. if (!this.option.grid) {
  229. let rightnum = 0,
  230. leftnum = 0;
  231. yAxis.forEach((item) => {
  232. if (item.position == 'right') {
  233. ++rightnum;
  234. } else if (item.position == 'left') {
  235. ++leftnum;
  236. }
  237. });
  238. const grid = {
  239. top: '60px',
  240. bottom: type == 'history' ? '40px' : '15px',
  241. right: rightnum * 30 + 20 + 'px',
  242. left: leftnum * 40 + 'px',
  243. containLabel: true,
  244. };
  245. return grid;
  246. } else {
  247. return this.option.grid;
  248. }
  249. }
  250. getTooltip() {
  251. const tooltip = {
  252. backgroundColor: '#00000005',
  253. borderColor: '#74E9FE44',
  254. extraCssText: 'backdrop-filter: blur(15px); box-shadow: 0 0 0 rgba(0, 0, 0, 0);',
  255. textStyle: {
  256. color: '#ffffff', // 字体颜色
  257. },
  258. trigger: 'axis',
  259. axisPointer: {
  260. label: {
  261. backgroundColor: 'rgba(30,120,50,0.8)',
  262. },
  263. type: 'cross',
  264. },
  265. };
  266. return tooltip;
  267. }
  268. // 分页显示数据
  269. getTimeline(xdata, ydata) {
  270. // 结合x、y轴的数据量判断是否要分页(x轴分页)ydata长度的倍数就是X轴要显示的数量n
  271. const n = Math.floor(20 / ydata.length);
  272. const size = Math.ceil(xdata.length / n); //分页数量
  273. if (size > 2) {
  274. // 设置时间轴
  275. const timeline = {
  276. axisType: 'category',
  277. // realtime: false,
  278. // loop: false,
  279. autoPlay: true,
  280. // currentIndex: 2,
  281. playInterval: 1000,
  282. // controlStyle: {
  283. // position: 'left'
  284. // },
  285. data: [],
  286. };
  287. timeline.data = Array.from(new Array(size).keys());
  288. return timeline;
  289. } else {
  290. return null;
  291. }
  292. }
  293. }