|
@@ -24,6 +24,8 @@ export const useChartOptions = (chartProps: Record<string, any>) => {
|
|
|
const { run, refresh, cancel, data, loading } = useRequest(server.value, {
|
|
|
defaultParams: chartProps.dataSource.params,
|
|
|
manual: true,
|
|
|
+ cacheKey: chartProps.dataSource.url,
|
|
|
+ cacheTime: (chartProps.dataSource?.refreshTime || 0) * 1000,
|
|
|
pollingInterval: (chartProps.dataSource?.refreshTime || 0) * 1000, // 刷新时间
|
|
|
onError: (error) => {
|
|
|
console.error(error);
|
|
@@ -81,6 +83,43 @@ export const useChartOptions = (chartProps: Record<string, any>) => {
|
|
|
}
|
|
|
);
|
|
|
|
|
|
+ // 获取grid
|
|
|
+ const getGrid = (opt: EChartsOption) => {
|
|
|
+ let bottom = 34, right = 20, left = 30, top = 20;
|
|
|
+ // 有标题
|
|
|
+ if(!Array.isArray(opt.title) && opt.title?.show) {
|
|
|
+ top += 20;
|
|
|
+ }
|
|
|
+ // 图例位置
|
|
|
+ if(!Array.isArray(opt.legend) && opt.legend?.show) {
|
|
|
+ if(opt.legend.left === 'center' && opt.legend.top !== 'auto') {
|
|
|
+ top += 20;
|
|
|
+ }
|
|
|
+ if(opt.legend.left === 'center' && opt.legend.bottom !== 'auto') {
|
|
|
+ bottom += 20;
|
|
|
+ }
|
|
|
+ if(opt.legend.top === 'center' && opt.legend.left !== 'auto') {
|
|
|
+ left += 70;
|
|
|
+ }
|
|
|
+ if(opt.legend.top === 'center' && opt.legend.right !== 'auto') {
|
|
|
+ right += 50;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if(!Array.isArray(opt.xAxis) && opt.xAxis?.name) {
|
|
|
+ bottom += 20;
|
|
|
+ }
|
|
|
+ if(!Array.isArray(opt.yAxis) && opt.yAxis?.name) {
|
|
|
+ left += 20;
|
|
|
+ }
|
|
|
+
|
|
|
+ return {
|
|
|
+ bottom,
|
|
|
+ left,
|
|
|
+ right,
|
|
|
+ top
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
const options = computed((): EChartsOption => {
|
|
|
const opt = omit(chartProps, [
|
|
|
"width",
|
|
@@ -88,6 +127,10 @@ export const useChartOptions = (chartProps: Record<string, any>) => {
|
|
|
"dataSource",
|
|
|
]) as EChartsOption;
|
|
|
|
|
|
+ if(!Array.isArray(opt.title) && !opt.title?.show && !Array.isArray(opt.legend) && opt.legend) {
|
|
|
+ opt.legend.top = 12;
|
|
|
+ }
|
|
|
+
|
|
|
// 通用标签
|
|
|
const label = opt?.label || {};
|
|
|
const result = defaultsDeep(
|
|
@@ -97,13 +140,13 @@ export const useChartOptions = (chartProps: Record<string, any>) => {
|
|
|
series: (series.value as any[])?.map((item: any) => {
|
|
|
// 每个类型的图,可以单独设置series.类型
|
|
|
const customSet = (opt.series as EChartsOption)?.[item.type] || {};
|
|
|
- // TODO 动态计算上下左右距离
|
|
|
return {
|
|
|
...label,
|
|
|
...item,
|
|
|
- ...customSet
|
|
|
+ ...customSet,
|
|
|
}
|
|
|
}),
|
|
|
+ grid: getGrid(opt)
|
|
|
},
|
|
|
opt
|
|
|
);
|