2023-06-30 20:49:44 +08:00
|
|
|
import { Schema } from '@formily/react';
|
|
|
|
import { uid } from '@formily/shared';
|
2023-09-07 23:27:59 +08:00
|
|
|
import { SelectedField } from './configure/ChartConfigure';
|
2023-06-30 20:49:44 +08:00
|
|
|
import { FieldOption } from './hooks';
|
|
|
|
import { QueryProps } from './renderer';
|
|
|
|
|
|
|
|
export const createRendererSchema = (decoratorProps: any, componentProps = {}) => {
|
|
|
|
const { collection } = decoratorProps;
|
|
|
|
return {
|
|
|
|
type: 'void',
|
|
|
|
'x-decorator': 'ChartRendererProvider',
|
|
|
|
'x-decorator-props': decoratorProps,
|
|
|
|
'x-acl-action': `${collection}:list`,
|
|
|
|
'x-designer': 'ChartRenderer.Designer',
|
|
|
|
'x-component': 'CardItem',
|
|
|
|
'x-component-props': {
|
|
|
|
size: 'small',
|
|
|
|
},
|
|
|
|
'x-initializer': 'ChartInitializers',
|
|
|
|
properties: {
|
|
|
|
[uid()]: {
|
|
|
|
type: 'void',
|
|
|
|
'x-component': 'ChartRenderer',
|
|
|
|
'x-component-props': componentProps,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
// For AssociationField, the format of field is [targetField, field]
|
|
|
|
export const parseField = (field: string | string[]) => {
|
|
|
|
let target: string;
|
|
|
|
let name: string;
|
|
|
|
if (!Array.isArray(field)) {
|
|
|
|
name = field;
|
|
|
|
} else if (field.length === 1) {
|
|
|
|
name = field[0];
|
|
|
|
} else if (field.length > 1) {
|
|
|
|
[target, name] = field;
|
|
|
|
}
|
|
|
|
return { target, name, alias: target ? `${target}.${name}` : name };
|
|
|
|
};
|
|
|
|
|
|
|
|
export const getField = (fields: FieldOption[], field: string | string[]) => {
|
|
|
|
const { target, name } = parseField(field);
|
|
|
|
if (!target) {
|
|
|
|
return fields.find((f) => f.name === name);
|
|
|
|
}
|
|
|
|
const targetField = fields.find((f) => f.name === target)?.targetFields?.find((f) => f.name === name);
|
|
|
|
return targetField;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const getSelectedFields = (fields: FieldOption[], query: QueryProps) => {
|
|
|
|
// When field alias is set, appends it to the field list
|
|
|
|
const process = (selectedFields: SelectedField[]) => {
|
|
|
|
return selectedFields.map((selectedField) => {
|
|
|
|
const fieldProps = getField(fields, selectedField.field);
|
|
|
|
return {
|
|
|
|
...fieldProps,
|
|
|
|
key: selectedField.alias || fieldProps?.key,
|
|
|
|
label: selectedField.alias || fieldProps?.label,
|
|
|
|
value: selectedField.alias || fieldProps?.value,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
};
|
|
|
|
const measures = query.measures || [];
|
|
|
|
const dimensions = query.dimensions || [];
|
|
|
|
// unique
|
|
|
|
const map = new Map([...process(measures), ...process(dimensions)].map((item) => [item.value, item]));
|
|
|
|
const selectedFields = [...map.values()];
|
|
|
|
return selectedFields;
|
|
|
|
};
|
|
|
|
|
2023-07-29 22:35:55 +08:00
|
|
|
export const processData = (selectedFields: FieldOption[], data: any[], scope: any) => {
|
2023-06-30 20:49:44 +08:00
|
|
|
const parseEnum = (field: FieldOption, value: any) => {
|
|
|
|
const options = field.uiSchema?.enum as { value: string; label: string }[];
|
|
|
|
if (!options || !Array.isArray(options)) {
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
const option = options.find((option) => option.value === value);
|
|
|
|
return Schema.compile(option?.label || value, scope);
|
|
|
|
};
|
|
|
|
return data.map((record) => {
|
|
|
|
const processed = {};
|
|
|
|
Object.entries(record).forEach(([key, value]) => {
|
2023-07-29 22:35:55 +08:00
|
|
|
const field = selectedFields.find((field) => field.value === key);
|
2023-06-30 20:49:44 +08:00
|
|
|
if (!field) {
|
|
|
|
processed[key] = value;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
switch (field.interface) {
|
|
|
|
case 'select':
|
|
|
|
case 'radioGroup':
|
|
|
|
processed[key] = parseEnum(field, value);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
processed[key] = value;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return processed;
|
|
|
|
});
|
|
|
|
};
|