useChartOptions.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import type { EChartsOption } from "echarts";
  2. import { computed, watch, ref } from "vue";
  3. import { omit, defaultsDeep } from "lodash";
  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"]>({ data: dataSource?.data?.xData });
  11. const yAxis = ref<EChartsOption["yAxis"]>({ data: dataSource?.data?.yData });
  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. pollingInterval: (chartProps.dataSource?.refreshTime || 0) * 1000, // 刷新时间
  25. onError: (error) => {
  26. console.error(error);
  27. message.error(chartProps.dataSource.url + "请求失败");
  28. }
  29. });
  30. /* 初始请求 */
  31. if (chartProps.dataSource.sourceType === DataSourceType.API) {
  32. run();
  33. }
  34. watch(
  35. () => data.value,
  36. async (val) => {
  37. if (val && chartProps.dataSource.sourceType === DataSourceType.API) {
  38. let res = val;
  39. if(chartProps.dataSource.dataProcess) {
  40. res = await cllJsCode(chartProps.dataSource.dataProcess, JSON.stringify(val));
  41. }
  42. xAxis.value = res.xAxis || { data: res.xData };
  43. yAxis.value = res.yAxis || { data: res.yData };
  44. series.value = res.series;
  45. }
  46. },
  47. {
  48. deep: true,
  49. }
  50. );
  51. watch(
  52. () => [
  53. chartProps.dataSource.sourceType,
  54. chartProps.dataSource.method
  55. ],
  56. () => {
  57. if (chartProps.dataSource.sourceType === DataSourceType.API) {
  58. refresh();
  59. } else {
  60. cancel();
  61. const dataSource = chartProps.dataSource || {};
  62. xAxis.value = { data: dataSource?.data?.xData };
  63. yAxis.value = { data: dataSource?.data?.yData };
  64. series.value = dataSource?.data?.series;
  65. }
  66. },
  67. {
  68. deep: true,
  69. }
  70. );
  71. const options = computed((): EChartsOption => {
  72. const opt = omit(chartProps, [
  73. "width",
  74. "height",
  75. "dataSource",
  76. ]) as EChartsOption;
  77. const result = defaultsDeep(
  78. {
  79. xAxis: xAxis.value,
  80. yAxis: yAxis.value,
  81. series: series.value,
  82. },
  83. opt
  84. );
  85. return result;
  86. });
  87. return {
  88. options,
  89. loading,
  90. };
  91. };