echartsUtil.ts 9.1 KB

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