BarAndLine.vue 9.4 KB

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