useChartOptions.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. import type { EChartsOption } from "echarts";
  2. import { computed, watch, ref } from "vue";
  3. import { omit, defaultsDeep } from "lodash-es";
  4. import { useRequest } from "vue-hooks-plus";
  5. import { DataSourceType } from "../chartEnum";
  6. import { message } from "ant-design-vue";
  7. import { cllJsCode } from "../utils";
  8. export const useChartOptions = (chartProps: Record<string, any>) => {
  9. const dataSource = chartProps.dataSource || {};
  10. const xAxis = ref<EChartsOption["xAxis"]>();
  11. const yAxis = ref<EChartsOption["yAxis"]>();
  12. const series = ref<EChartsOption["series"]>(dataSource?.data?.series);
  13. const server = computed(() => {
  14. return async () =>
  15. await fetch(chartProps.dataSource.url, {
  16. method: chartProps.dataSource.method,
  17. })
  18. .then((res) => res.json());
  19. });
  20. // 请求数据
  21. const { run, refresh, cancel, data, loading } = useRequest(server.value, {
  22. defaultParams: chartProps.dataSource.params,
  23. manual: true,
  24. cacheKey: chartProps.dataSource.url,
  25. cacheTime: (chartProps.dataSource?.refreshTime || 0) * 1000,
  26. pollingInterval: (chartProps.dataSource?.refreshTime || 0) * 1000, // 刷新时间
  27. onError: (error) => {
  28. console.error(error);
  29. message.error(chartProps.dataSource.url + "请求失败");
  30. }
  31. });
  32. /* 初始请求 */
  33. if (chartProps.dataSource.sourceType === DataSourceType.API) {
  34. run();
  35. }
  36. watch(
  37. () => data.value,
  38. async (val) => {
  39. if (val && chartProps.dataSource.sourceType === DataSourceType.API) {
  40. let res = val;
  41. if(chartProps.dataSource.dataProcess) {
  42. res = await cllJsCode(chartProps.dataSource.dataProcess, JSON.stringify(val));
  43. }
  44. xAxis.value = res.xAxis || { data: res.xData };
  45. yAxis.value = res.yAxis || { data: res.yData };
  46. series.value = res.series;
  47. }
  48. },
  49. {
  50. deep: true,
  51. }
  52. );
  53. watch(
  54. () => [
  55. chartProps.dataSource.sourceType,
  56. chartProps.dataSource.method
  57. ],
  58. () => {
  59. if (chartProps.dataSource.sourceType === DataSourceType.API) {
  60. refresh();
  61. } else {
  62. cancel();
  63. const dataSource = chartProps.dataSource || {};
  64. const { xData, yData, series } = dataSource?.data || {};
  65. if(xData) {
  66. xAxis.value = { data: xData };
  67. }
  68. if(yData) {
  69. yAxis.value = { data: yData };
  70. }
  71. series.value = series;
  72. }
  73. },
  74. {
  75. deep: true,
  76. }
  77. );
  78. // 获取grid
  79. const getGrid = (opt: EChartsOption) => {
  80. let bottom = 34, right = 20, left = 30, top = 20;
  81. // 有标题
  82. if(!Array.isArray(opt.title) && opt.title?.show) {
  83. top += 20;
  84. }
  85. // 图例位置
  86. if(!Array.isArray(opt.legend) && opt.legend?.show) {
  87. if(opt.legend.left === 'center' && opt.legend.top !== 'auto') {
  88. top += 20;
  89. }
  90. if(opt.legend.left === 'center' && opt.legend.bottom !== 'auto') {
  91. bottom += 20;
  92. }
  93. if(opt.legend.top === 'center' && opt.legend.left !== 'auto') {
  94. left += 70;
  95. }
  96. if(opt.legend.top === 'center' && opt.legend.right !== 'auto') {
  97. right += 50;
  98. }
  99. }
  100. if(!Array.isArray(opt.xAxis) && opt.xAxis?.name) {
  101. bottom += 20;
  102. }
  103. if(!Array.isArray(opt.yAxis) && opt.yAxis?.name) {
  104. left += 20;
  105. }
  106. return {
  107. bottom,
  108. left,
  109. right,
  110. top
  111. }
  112. }
  113. const options = computed((): EChartsOption => {
  114. const opt = omit(chartProps, [
  115. "width",
  116. "height",
  117. "dataSource",
  118. ]) as EChartsOption;
  119. if(!Array.isArray(opt.title) && !opt.title?.show && !Array.isArray(opt.legend) && opt.legend) {
  120. opt.legend.top = 12;
  121. }
  122. // 通用标签
  123. const label = opt?.label || {};
  124. const result = defaultsDeep(
  125. {
  126. xAxis: xAxis.value,
  127. yAxis: yAxis.value,
  128. series: (series.value as any[])?.map((item: any) => {
  129. // 每个类型的图,可以单独设置series.类型
  130. const customSet = (opt.series as EChartsOption)?.[item.type] || {};
  131. return {
  132. ...label,
  133. ...item,
  134. ...customSet,
  135. }
  136. }),
  137. grid: getGrid(opt)
  138. },
  139. opt
  140. );
  141. console.log('option result:', result)
  142. return result;
  143. });
  144. return {
  145. options,
  146. loading,
  147. };
  148. };