BarAndLine.vue 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. <template>
  2. <div v-if="spinning" class="spinning">
  3. <a-spin :spinning="true" />
  4. </div>
  5. <div v-if="!spinning" ref="chartRef" :style="{ height, width }"></div>
  6. </template>
  7. <script lang="ts">
  8. import { defineComponent, PropType, ref, Ref, reactive, watchEffect, watch, nextTick } from 'vue';
  9. import { useECharts } from '/@/hooks/web/useECharts';
  10. import { getTableHeaderColumns } from '/@/hooks/web/useWebColumns';
  11. import EchartsUtil from '/@/utils/echartsUtil';
  12. import { merge } from 'lodash-es';
  13. type ChartColumn = {
  14. legend: string;
  15. seriesName: string;
  16. ymax: number;
  17. yname: string;
  18. linetype: string;
  19. yaxispos: string;
  20. color: string;
  21. sort: number;
  22. xRotate: number;
  23. dataIndex: string;
  24. };
  25. export default defineComponent({
  26. name: 'BarAndLine',
  27. props: {
  28. chartsColumns: {
  29. type: Array as PropType<ChartColumn[]>,
  30. default: () => [],
  31. },
  32. chartsColumnsType: {
  33. type: String,
  34. },
  35. chartsType: {
  36. type: String,
  37. default: '',
  38. },
  39. dataSource: {
  40. type: Array,
  41. default: () => [],
  42. },
  43. option: {
  44. type: Object,
  45. default: () => ({}),
  46. },
  47. xAxisPropType: {
  48. type: String,
  49. required: true,
  50. },
  51. width: {
  52. type: String as PropType<string>,
  53. default: '100%',
  54. },
  55. height: {
  56. type: String as PropType<string>,
  57. default: 'calc(100vh - 78px)',
  58. },
  59. },
  60. emits: ['refresh'],
  61. setup(props, { emit }) {
  62. const spinning = ref<boolean>(true);
  63. const chartRef = ref<HTMLDivElement | null>(null);
  64. const { setOptions, echarts } = useECharts(chartRef as Ref<HTMLDivElement>);
  65. const chartData = props.chartsColumnsType ? getTableHeaderColumns(props.chartsColumnsType) : [];
  66. let chartsColumns = (props.chartsColumns.length > 0 ? props.chartsColumns : chartData) as ChartColumn[];
  67. let tempYmax: number[] = [];
  68. let tempYmin: number[] = [];
  69. // let groupedByColumns = {};
  70. const option = reactive({
  71. name: '',
  72. color: ['#7B68EE', '#0000CD', '#6495ED', '#00BFFF', '#AFEEEE', '#008080', '#00FA9A', '#2E8B57', '#FAFAD2', '#DAA520'],
  73. tooltip: {},
  74. grid: {},
  75. toolbox: {
  76. feature: {
  77. saveAsImage: {
  78. iconStyle: {
  79. borderColor: '#ffffff',
  80. },
  81. show: false,
  82. },
  83. },
  84. },
  85. dataZoom: {},
  86. legend: {
  87. textStyle: {
  88. color: '#ffffff', // 字体颜色
  89. },
  90. top: '20',
  91. },
  92. timeline: null,
  93. xAxis: {},
  94. yAxis: null,
  95. series: null,
  96. });
  97. let optionUtil;
  98. watchEffect(() => {
  99. props.dataSource && props.xAxisPropType && option.series && initCharts();
  100. });
  101. watch([() => props.chartsType, () => props.chartsColumns], ([newChartsType, newChartsColumns]) => {
  102. spinning.value = true;
  103. chartsColumns = newChartsColumns;
  104. optionUtil.initChartOption(newChartsType, newChartsColumns);
  105. spinning.value = false;
  106. initCharts(true);
  107. emit('refresh');
  108. });
  109. function initChartsOption() {
  110. // debugger;
  111. optionUtil = new EchartsUtil(merge(option, props.option));
  112. optionUtil.initChartOption(props.chartsType, chartsColumns);
  113. }
  114. initChartsOption();
  115. function initCharts(isRefresh = false) {
  116. if (props.dataSource.length < 1) return;
  117. //轴数据
  118. let isFresh = false;
  119. if (option.series && option.series.length === chartsColumns.length) {
  120. let xAxisData = props.dataSource.map((item) => item[props.xAxisPropType]);
  121. chartsColumns = [...chartsColumns].filter((propType: any, index) => {
  122. if (!propType) return;
  123. if (props.chartsType == 'listMonitor') {
  124. option.series[index].type = 'bar';
  125. }
  126. console.log(option.series[index], '000===')
  127. option.series[index].data = props.dataSource.map((item) => Number(item[propType.dataIndex]) || 0);
  128. // console.log('nnn', option.series[index].data);
  129. // 这里动态计算echarts y轴最大值
  130. const max = Math.max(...option.series[index].data);
  131. const min = Math.min(...option.series[index].data);
  132. const digitCount = max.toFixed(0).length;
  133. const minDigitCount = min.toFixed(0).length;
  134. let yMax = 0,
  135. yMin = 0;
  136. if (option.series[index].name.indexOf('°C') == -1 && option.series[index].name.indexOf('%') == -1 ) {
  137. if (digitCount < 2) {
  138. if (max < 0.5) {
  139. yMax = 1;
  140. } else if (max < 0.9) {
  141. yMax = 1.5;
  142. } else if (max < 5) {
  143. yMax = 5;
  144. } else if (max < 10) {
  145. yMax = 10;
  146. }
  147. } else if (digitCount < 3) {
  148. const n = Number((Number(max.toFixed(0)) / 10).toFixed(0));
  149. if (max < n * 10 + 5) {
  150. yMax = (n + 1) * 10;
  151. } else {
  152. yMax = (n + 2) * 10;
  153. }
  154. } else if (digitCount < 4) {
  155. const n = Number((Number(max.toFixed(0)) / 100).toFixed(0));
  156. if (max < n * 100 + 50) {
  157. yMax = (n + 1) * 100;
  158. } else {
  159. yMax = (n + 2) * 100;
  160. }
  161. } else if (digitCount < 5) {
  162. const n = Number((Number(max.toFixed(0)) / 1000).toFixed(0));
  163. if (max < n * 1000 + 500) {
  164. yMax = (n + 1) * 1000;
  165. } else {
  166. yMax = (n + 1) * 1000 + 500;
  167. }
  168. } else if (digitCount < 6) {
  169. const n = Number((Number(max.toFixed(0)) / 10000).toFixed(0));
  170. if (max < n * 10000 + 5000) {
  171. yMax = (n + 1) * 10000;
  172. } else {
  173. yMax = (n + 1) * 10000 + 5000;
  174. }
  175. }
  176. if (minDigitCount < 2) {
  177. if (min > 1.5) {
  178. yMin = 1.0;
  179. } else if (min > 5) {
  180. yMin = 5;
  181. } else {
  182. yMin = 0;
  183. }
  184. } else if (minDigitCount < 3) {
  185. const n = Number((Number(min.toFixed(0)) / 10).toFixed(0));
  186. if (n > 1) {
  187. yMin = (n - 1) * 10;
  188. } else {
  189. yMin = 10;
  190. }
  191. } else if (digitCount < 4) {
  192. const n = Number((Number(min.toFixed(0)) / 100).toFixed(0));
  193. if (n > 1) {
  194. yMin = (n - 1) * 100;
  195. } else {
  196. yMin = 100;
  197. }
  198. } else if (digitCount < 5) {
  199. const n = Number((Number(min.toFixed(0)) / 1000).toFixed(0));
  200. if (n > 1) {
  201. yMin = (n - 1) * 1000;
  202. } else {
  203. yMin = 1000;
  204. }
  205. } else if (digitCount < 6) {
  206. const n = Number((Number(min.toFixed(0)) / 10000).toFixed(0));
  207. if (n > 1) {
  208. yMin = (n - 1) * 10000;
  209. } else {
  210. yMin = 10000;
  211. }
  212. }
  213. } else {
  214. yMax=100
  215. yMin=0
  216. }
  217. propType.ymax = yMax;
  218. propType.ymin = yMin;
  219. return true;
  220. });
  221. // debugger;
  222. // 根据sort分组
  223. const groupedBy = {};
  224. for (const item of chartsColumns) {
  225. if (groupedBy[item.sort]) {
  226. groupedBy[item.sort].push(item);
  227. } else {
  228. groupedBy[item.sort] = [item];
  229. }
  230. }
  231. // 根据分组找ymax最大值
  232. const newChartsColumns: ChartColumn[] = [];
  233. let index = 0;
  234. for (let sortId in groupedBy) {
  235. const group = groupedBy[sortId];
  236. let ymax = group[0].ymax;
  237. let ymin = group[0].ymin;
  238. for (let i = 1; i < group.length; i++) {
  239. if (group[i].ymax > ymax) {
  240. ymax = group[i].ymax;
  241. }
  242. }
  243. for (let i = 1; i < group.length; i++) {
  244. if (group[i].ymin < ymin) {
  245. ymin = group[i].ymin;
  246. }
  247. }
  248. if (!tempYmax[index] || tempYmax[index] != ymax) {
  249. tempYmax[index] = ymax;
  250. isFresh = true;
  251. }
  252. if (tempYmin[index] != ymin) {
  253. tempYmin[index] = ymin;
  254. isFresh = true;
  255. }
  256. for (let i = 0; i < group.length; i++) {
  257. group[i].ymax = ymax;
  258. group[i].ymin = ymin;
  259. newChartsColumns.push(group[i]);
  260. }
  261. ++index;
  262. }
  263. chartsColumns = newChartsColumns;
  264. option.xAxis[0].data = xAxisData;
  265. if (isFresh) {
  266. spinning.value = true;
  267. optionUtil.initChartOption(props.chartsType, chartsColumns);
  268. spinning.value = false;
  269. initCharts(true);
  270. nextTick(() => {
  271. setOptions(option, true);
  272. emit('refresh');
  273. });
  274. } else {
  275. // console.log('echarts监测列表数据', option.xAxis[0].data);
  276. setOptions(option, isRefresh);
  277. }
  278. setOptions(option, isRefresh);
  279. }
  280. }
  281. setTimeout(() => {
  282. spinning.value = false;
  283. initCharts(true);
  284. }, 1000);
  285. return { chartRef, spinning };
  286. },
  287. });
  288. </script>
  289. <style lang="less" scoped>
  290. .spinning {
  291. display: flex;
  292. width: 100%;
  293. height: 100%;
  294. justify-content: center;
  295. align-items: center;
  296. }
  297. </style>