BarAndLine.vue 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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 groupedByColumns = {};
  69. const option = reactive({
  70. name: '',
  71. color: ['#7B68EE', '#0000CD', '#6495ED', '#00BFFF', '#AFEEEE', '#008080', '#00FA9A', '#2E8B57', '#FAFAD2', '#DAA520'],
  72. tooltip: {},
  73. grid: {},
  74. toolbox: {
  75. feature: {
  76. saveAsImage: {
  77. iconStyle: {
  78. borderColor: '#ffffff',
  79. },
  80. show: true,
  81. },
  82. },
  83. },
  84. dataZoom: {},
  85. legend: {
  86. textStyle: {
  87. color: '#ffffff', // 字体颜色
  88. },
  89. top: '20',
  90. },
  91. timeline: null,
  92. xAxis: {},
  93. yAxis: null,
  94. series: null,
  95. });
  96. let optionUtil;
  97. watchEffect(() => {
  98. props.dataSource && props.xAxisPropType && option.series && initCharts();
  99. });
  100. watch([() => props.chartsType, () => props.chartsColumns], ([newChartsType, newChartsColumns]) => {
  101. spinning.value = true;
  102. chartsColumns = newChartsColumns;
  103. optionUtil.initChartOption(newChartsType, newChartsColumns);
  104. spinning.value = false;
  105. initCharts(true);
  106. emit('refresh');
  107. });
  108. function initChartsOption() {
  109. // debugger;
  110. optionUtil = new EchartsUtil(merge(option, props.option));
  111. optionUtil.initChartOption(props.chartsType, chartsColumns);
  112. }
  113. initChartsOption();
  114. function initCharts(isRefresh = false) {
  115. debugger;
  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 digitCount = max.toFixed(0).length;
  131. let yMax = 0;
  132. if (digitCount < 2) {
  133. if (max < 0.5) {
  134. yMax = 1;
  135. } else if (max < 0.9) {
  136. yMax = 1.5;
  137. } else if (max < 5) {
  138. yMax = 10;
  139. } else {
  140. yMax = 15;
  141. }
  142. } else if (digitCount < 3) {
  143. const n = Number((Number(max.toFixed(0)) / 10).toFixed(0));
  144. if (max < n * 10 + 5) {
  145. yMax = (n + 1) * 10;
  146. } else {
  147. yMax = (n + 2) * 10;
  148. }
  149. } else if (digitCount < 4) {
  150. const n = Number((Number(max.toFixed(0)) / 100).toFixed(0));
  151. if (max < n * 100 + 50) {
  152. yMax = (n + 1) * 100;
  153. } else {
  154. yMax = (n + 2) * 100;
  155. }
  156. } else if (digitCount < 5) {
  157. const n = Number((Number(max.toFixed(0)) / 1000).toFixed(0));
  158. if (max < n * 1000 + 500) {
  159. yMax = (n + 1) * 1000;
  160. } else {
  161. yMax = (n + 1) * 1000 + 500;
  162. }
  163. } else if (digitCount < 6) {
  164. const n = Number((Number(max.toFixed(0)) / 10000).toFixed(0));
  165. if (max < n * 10000 + 5000) {
  166. yMax = (n + 1) * 1000;
  167. } else {
  168. yMax = (n + 1) * 10000 + 5000;
  169. }
  170. }
  171. if (yMax != 0 && yMax !== propType?.ymax) {
  172. propType.ymax = yMax;
  173. }
  174. return true;
  175. });
  176. // 根据sort分组
  177. const groupedBy = {};
  178. for (const item of chartsColumns) {
  179. if (groupedBy[item.sort]) {
  180. groupedBy[item.sort].push(item);
  181. } else {
  182. groupedBy[item.sort] = [item];
  183. }
  184. }
  185. // 根据分组找ymax最大值
  186. const newChartsColumns: ChartColumn[] = [];
  187. let index = 0;
  188. for (let sortId in groupedBy) {
  189. const group = groupedBy[sortId];
  190. let ymax = group[0].ymax;
  191. for (let i = 1; i < group.length; i++) {
  192. if (group[i].ymax > ymax) {
  193. ymax = group[i].ymax;
  194. }
  195. }
  196. if (!tempYmax[index] || tempYmax[index] != ymax) {
  197. tempYmax[index] = ymax;
  198. isFresh = true;
  199. }
  200. for (let i = 0; i < group.length; i++) {
  201. group[i].ymax = ymax;
  202. newChartsColumns.push(group[i]);
  203. }
  204. ++index;
  205. }
  206. chartsColumns = newChartsColumns;
  207. option.xAxis[0].data = xAxisData;
  208. if (isFresh) {
  209. spinning.value = true;
  210. optionUtil.initChartOption(props.chartsType, chartsColumns);
  211. spinning.value = false;
  212. initCharts(true);
  213. nextTick(() => {
  214. setOptions(option, true);
  215. emit('refresh');
  216. });
  217. } else {
  218. // console.log('echarts监测列表数据', option.xAxis[0].data);
  219. setOptions(option, isRefresh);
  220. }
  221. }
  222. }
  223. setTimeout(() => {
  224. spinning.value = false;
  225. initCharts(true);
  226. }, 1000);
  227. return { chartRef, spinning };
  228. },
  229. });
  230. </script>
  231. <style lang="less" scoped>
  232. .spinning {
  233. display: flex;
  234. width: 100%;
  235. height: 100%;
  236. justify-content: center;
  237. align-items: center;
  238. }
  239. </style>