BarAndLine.vue 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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 (digitCount < 2) {
  137. if (max < 0.5) {
  138. yMax = 1;
  139. } else if (max < 0.9) {
  140. yMax = 1.5;
  141. } else if (max < 5) {
  142. yMax = 5;
  143. } else if (max < 10) {
  144. yMax = 10;
  145. }
  146. } else if (digitCount < 3) {
  147. const n = Number((Number(max.toFixed(0)) / 10).toFixed(0));
  148. if (max < n * 10 + 5) {
  149. yMax = (n + 1) * 10;
  150. } else {
  151. yMax = (n + 2) * 10;
  152. }
  153. } else if (digitCount < 4) {
  154. const n = Number((Number(max.toFixed(0)) / 100).toFixed(0));
  155. if (max < n * 100 + 50) {
  156. yMax = (n + 1) * 100;
  157. } else {
  158. yMax = (n + 2) * 100;
  159. }
  160. } else if (digitCount < 5) {
  161. const n = Number((Number(max.toFixed(0)) / 1000).toFixed(0));
  162. if (max < n * 1000 + 500) {
  163. yMax = (n + 1) * 1000;
  164. } else {
  165. yMax = (n + 1) * 1000 + 500;
  166. }
  167. } else if (digitCount < 6) {
  168. const n = Number((Number(max.toFixed(0)) / 10000).toFixed(0));
  169. if (max < n * 10000 + 5000) {
  170. yMax = (n + 1) * 10000;
  171. } else {
  172. yMax = (n + 1) * 10000 + 5000;
  173. }
  174. }
  175. if (minDigitCount < 2) {
  176. if (min > 1.5) {
  177. yMin = 1.0;
  178. } else if (min > 5) {
  179. yMin = 5;
  180. } else {
  181. yMin = 0;
  182. }
  183. } else if (minDigitCount < 3) {
  184. const n = Number((Number(min.toFixed(0)) / 10).toFixed(0));
  185. if (n > 1) {
  186. yMin = (n - 1) * 10;
  187. } else {
  188. yMin = 10;
  189. }
  190. } else if (digitCount < 4) {
  191. const n = Number((Number(min.toFixed(0)) / 100).toFixed(0));
  192. if (n > 1) {
  193. yMin = (n - 1) * 100;
  194. } else {
  195. yMin = 100;
  196. }
  197. } else if (digitCount < 5) {
  198. const n = Number((Number(min.toFixed(0)) / 1000).toFixed(0));
  199. if (n > 1) {
  200. yMin = (n - 1) * 1000;
  201. } else {
  202. yMin = 1000;
  203. }
  204. } else if (digitCount < 6) {
  205. const n = Number((Number(min.toFixed(0)) / 10000).toFixed(0));
  206. if (n > 1) {
  207. yMin = (n - 1) * 10000;
  208. } else {
  209. yMin = 10000;
  210. }
  211. }
  212. propType.ymax = yMax;
  213. propType.ymin = yMin;
  214. return true;
  215. });
  216. // debugger;
  217. // 根据sort分组
  218. const groupedBy = {};
  219. for (const item of chartsColumns) {
  220. if (groupedBy[item.sort]) {
  221. groupedBy[item.sort].push(item);
  222. } else {
  223. groupedBy[item.sort] = [item];
  224. }
  225. }
  226. // 根据分组找ymax最大值
  227. const newChartsColumns: ChartColumn[] = [];
  228. let index = 0;
  229. for (let sortId in groupedBy) {
  230. const group = groupedBy[sortId];
  231. let ymax = group[0].ymax;
  232. let ymin = group[0].ymin;
  233. for (let i = 1; i < group.length; i++) {
  234. if (group[i].ymax > ymax) {
  235. ymax = group[i].ymax;
  236. }
  237. }
  238. for (let i = 1; i < group.length; i++) {
  239. if (group[i].ymin < ymin) {
  240. ymin = group[i].ymin;
  241. }
  242. }
  243. if (!tempYmax[index] || tempYmax[index] != ymax) {
  244. tempYmax[index] = ymax;
  245. isFresh = true;
  246. }
  247. if (tempYmin[index] != ymin) {
  248. tempYmin[index] = ymin;
  249. isFresh = true;
  250. }
  251. for (let i = 0; i < group.length; i++) {
  252. group[i].ymax = ymax;
  253. group[i].ymin = ymin;
  254. newChartsColumns.push(group[i]);
  255. }
  256. ++index;
  257. }
  258. chartsColumns = newChartsColumns;
  259. option.xAxis[0].data = xAxisData;
  260. if (isFresh) {
  261. spinning.value = true;
  262. optionUtil.initChartOption(props.chartsType, chartsColumns);
  263. spinning.value = false;
  264. initCharts(true);
  265. nextTick(() => {
  266. setOptions(option, true);
  267. emit('refresh');
  268. });
  269. } else {
  270. // console.log('echarts监测列表数据', option.xAxis[0].data);
  271. setOptions(option, isRefresh);
  272. }
  273. setOptions(option, isRefresh);
  274. }
  275. }
  276. setTimeout(() => {
  277. spinning.value = false;
  278. initCharts(true);
  279. }, 1000);
  280. return { chartRef, spinning };
  281. },
  282. });
  283. </script>
  284. <style lang="less" scoped>
  285. .spinning {
  286. display: flex;
  287. width: 100%;
  288. height: 100%;
  289. justify-content: center;
  290. align-items: center;
  291. }
  292. </style>