useChartOptions.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. const dataSource = chartProps.dataSource || {};
  82. const obj = {
  83. viewCode: dataSource.viewCode,
  84. basicPath: dataSource.basicPath,
  85. items: '',
  86. filter: '',
  87. key: '',
  88. isOne: '',
  89. };
  90. (window as Window & typeof globalThis & {mabp: any}).mabp.$doLoadComponentData(obj).then(function (res: any) {
  91. series.value = res.data;
  92. });
  93. }
  94. },
  95. {
  96. deep: true,
  97. }
  98. );
  99. // 获取grid
  100. const getGrid = (opt: EChartsOption) => {
  101. let bottom = 34, right = 20, left = 30, top = 20;
  102. // 有标题
  103. if(!Array.isArray(opt.title) && opt.title?.show) {
  104. top += 20;
  105. }
  106. // 图例位置
  107. if(!Array.isArray(opt.legend) && opt.legend?.show) {
  108. if(opt.legend.left === 'center' && opt.legend.top !== 'auto') {
  109. top += 20;
  110. }
  111. if(opt.legend.left === 'center' && opt.legend.bottom !== 'auto') {
  112. bottom += 20;
  113. }
  114. if(opt.legend.top === 'center' && opt.legend.left !== 'auto') {
  115. left += 70;
  116. }
  117. if(opt.legend.top === 'center' && opt.legend.right !== 'auto') {
  118. right += 50;
  119. }
  120. }
  121. if(!Array.isArray(opt.xAxis) && opt.xAxis?.name) {
  122. bottom += 20;
  123. }
  124. if(!Array.isArray(opt.yAxis) && opt.yAxis?.name) {
  125. left += 20;
  126. }
  127. return {
  128. bottom,
  129. left,
  130. right,
  131. top
  132. }
  133. }
  134. const options = computed((): EChartsOption => {
  135. const opt = omit(chartProps, [
  136. "width",
  137. "height",
  138. "dataSource",
  139. ]) as EChartsOption;
  140. if(!Array.isArray(opt.title) && !opt.title?.show && !Array.isArray(opt.legend) && opt.legend) {
  141. opt.legend.top = 12;
  142. }
  143. // 通用标签
  144. const label = opt?.label || {};
  145. const result = defaultsDeep(
  146. {
  147. xAxis: xAxis.value,
  148. yAxis: yAxis.value,
  149. series: (series.value as any[])?.map((item: any) => {
  150. // 每个类型的图,可以单独设置series.类型
  151. const customSet = (opt.series as EChartsOption)?.[item.type] || {};
  152. return {
  153. ...label,
  154. ...item,
  155. ...customSet,
  156. }
  157. }),
  158. grid: getGrid(opt)
  159. },
  160. opt
  161. );
  162. console.log('option result:', result)
  163. return result;
  164. });
  165. return {
  166. options,
  167. loading,
  168. };
  169. };