echartsUtil.ts 9.1 KB

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