echartsUtil.ts 7.2 KB

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