useChartOptions.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. // 请求后数据处理
  43. res = await cllJsCode(chartProps.dataSource.dataProcess, JSON.stringify(val));
  44. }
  45. xAxis.value = res.xAxis || res.xData ? { data: res.xData } : xAxis.value;
  46. yAxis.value = res.yAxis || res.yData ? { data: res.yData } : yAxis.value;
  47. series.value = res.series;
  48. }
  49. },
  50. {
  51. deep: true,
  52. }
  53. );
  54. // 数据源设置
  55. watch(
  56. () => [
  57. chartProps.dataSource.sourceType,
  58. chartProps.dataSource.method,
  59. chartProps.dataSource.data
  60. ],
  61. () => {
  62. // 请求接口
  63. if (chartProps.dataSource.sourceType === DataSourceType.API) {
  64. refresh();
  65. }
  66. // 静态数据
  67. if(chartProps.dataSource.sourceType === DataSourceType.STATIC) {
  68. cancel();
  69. const dataSource = chartProps.dataSource || {};
  70. const { xData, yData, series: seriesData } = dataSource?.data || {};
  71. if(xData) {
  72. xAxis.value = { data: xData };
  73. }
  74. if(yData) {
  75. yAxis.value = { data: yData };
  76. }
  77. series.value = seriesData;
  78. }
  79. // 视图或基础数据源
  80. if([DataSourceType.BASIC_PATH, DataSourceType.VIEW_CODE].includes(chartProps.dataSource.sourceType)) {
  81. window?.mabp && window.mabp.$doLoadComponentData(obj).then(function (res) {
  82. itemList.value = res.data;
  83. });
  84. }
  85. },
  86. {
  87. deep: true,
  88. }
  89. );
  90. // 获取grid
  91. const getGrid = (opt: EChartsOption) => {
  92. let bottom = 34, right = 20, left = 30, top = 20;
  93. // 有标题
  94. if(!Array.isArray(opt.title) && opt.title?.show) {
  95. top += 20;
  96. }
  97. // 图例位置
  98. if(!Array.isArray(opt.legend) && opt.legend?.show) {
  99. if(opt.legend.left === 'center' && opt.legend.top !== 'auto') {
  100. top += 20;
  101. }
  102. if(opt.legend.left === 'center' && opt.legend.bottom !== 'auto') {
  103. bottom += 20;
  104. }
  105. if(opt.legend.top === 'center' && opt.legend.left !== 'auto') {
  106. left += 70;
  107. }
  108. if(opt.legend.top === 'center' && opt.legend.right !== 'auto') {
  109. right += 50;
  110. }
  111. }
  112. if(!Array.isArray(opt.xAxis) && opt.xAxis?.name) {
  113. bottom += 20;
  114. }
  115. if(!Array.isArray(opt.yAxis) && opt.yAxis?.name) {
  116. left += 20;
  117. }
  118. return {
  119. bottom,
  120. left,
  121. right,
  122. top
  123. }
  124. }
  125. const options = computed((): EChartsOption => {
  126. const opt = omit(chartProps, [
  127. "width",
  128. "height",
  129. "dataSource",
  130. ]) as EChartsOption;
  131. if(!Array.isArray(opt.title) && !opt.title?.show && !Array.isArray(opt.legend) && opt.legend) {
  132. opt.legend.top = 12;
  133. }
  134. // 通用标签
  135. const label = opt?.label || {};
  136. const result = defaultsDeep(
  137. {
  138. xAxis: xAxis.value,
  139. yAxis: yAxis.value,
  140. series: (series.value as any[])?.map((item: any) => {
  141. // 每个类型的图,可以单独设置series.类型
  142. const customSet = (opt.series as EChartsOption)?.[item.type] || {};
  143. return {
  144. ...label,
  145. ...item,
  146. ...customSet,
  147. }
  148. }),
  149. grid: getGrid(opt)
  150. },
  151. opt
  152. );
  153. console.log('option result:', result)
  154. return result;
  155. });
  156. return {
  157. options,
  158. loading,
  159. };
  160. };