ze-react-component-library
Version:
ZeroETP React Component Library
211 lines (169 loc) • 7.65 kB
JavaScript
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
import table from "./table";
import value from "./value";
import bar from "./bar";
import column from "./column";
import stackedColumn from "./stackedColumn";
import line from "./line";
import pie from "./pie";
import map, { isMapValid } from "./map";
import funnel from "./funnel";
import sheet from "./sheet";
import entity from "./entity";
import area from "./area";
import treemap from "./treemap";
import scatter from "./scatter";
import guage from "./guage";
import pareto from "./pareto";
import { findPropByName, getHierarchyCodeLength } from "zeroetp-api-sdk";
import Scatter from "./scatter";
import { canUseDualAxis } from "../../components/Chart/util";
export var chartTypes = ["bar", "column", "stackedColumn", "line", "pie", "funnel", "map", "area", "treemap", "scatter", "guage", "pareto"];
export var representationMap = {
value: value,
table: table,
sheet: sheet,
column: column,
stackedColumn: stackedColumn,
line: line,
area: area,
pie: pie,
bar: bar,
map: map,
treemap: treemap,
scatter: scatter,
funnel: funnel,
entity: entity,
guage: guage,
pareto: pareto
};
export var getDefaultRepresentation = function getDefaultRepresentation(result, pieThreshold, barFirst // 现在优先是column。这个flag就是来配置优先展示什么
) {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
if (pieThreshold === void 0) {
pieThreshold = 5;
}
if (barFirst === void 0) {
barFirst = false;
}
if (!result) return null;
if (result.result == null) return null;
if (result.returnType === "value") return "value";
var logicform = result.logicform;
if (logicform.groupby) {
// 同时要显示很多groupby id
if (logicform._noRelationGroupby && logicform._noRelationGroupby.length < logicform.groupby.length) {
return "table";
}
if (!logicform.preds || logicform.preds.length === 0) {
return "table";
} // 如果全部preds都是string,用table
if (logicform.preds.every(function (p) {
return typeof p === "string" || !p.operator;
})) {
return "table";
} // 如果是多维分组,直接用table
if (Array.isArray(logicform.groupby)) {
if (logicform.groupby.length >= 3) {
return "table";
}
if (logicform.groupby.length >= 2 && logicform.preds.length > 1) {
return "table";
}
if (logicform.groupby.length === 2 && logicform.preds.length === 1) {
return "stackedColumn";
}
} // 👇是一维分组的逻辑
var groupbyItem_1 = logicform.groupby;
if (Array.isArray(logicform.groupby)) {
groupbyItem_1 = logicform.groupby[0];
}
var groupbyProp = void 0;
if (_typeof(groupbyItem_1) === "object" && "_id" in groupbyItem_1) {
groupbyProp = findPropByName(result.schema, groupbyItem_1._id);
} else if (typeof groupbyItem_1 === "string") {
groupbyProp = findPropByName(result.schema, groupbyItem_1);
} // Schema指定的representation
if ((_a = groupbyProp.ui) === null || _a === void 0 ? void 0 : _a.representation) {
return (_b = groupbyProp.ui) === null || _b === void 0 ? void 0 : _b.representation;
} // 如果是geo,并且是同一个level,那么用地图
if (groupbyItem_1.level && (groupbyProp === null || groupbyProp === void 0 ? void 0 : groupbyProp.schema) && (groupbyProp === null || groupbyProp === void 0 ? void 0 : groupbyProp.ref) === "geo" && logicform.groupby.length === 1) {
if (groupbyItem_1.level === "省市") {
// 目前仅支持到省市开始的level
return "map";
} else if (groupbyItem_1.level === "城市" || groupbyItem_1.level === "区县") {
// 要保证所有数据都在同一个上层level中
var hierarchIndex = groupbyProp.schema.hierarchy.findIndex(function (h) {
return h.name === groupbyItem_1.level;
});
var prevCodeLength_1 = getHierarchyCodeLength(groupbyProp.schema, groupbyProp.schema.hierarchy[hierarchIndex - 1].name);
var prevCodeSet_1 = new Set();
result.result.forEach(function (r) {
prevCodeSet_1.add(r._id.substring(0, prevCodeLength_1));
});
if (prevCodeSet_1.size === 1) {
return "map";
}
}
}
if (isMapValid(result)) {
return "map";
} // 看下第一个pred的property有没有ui.representation
if ((_d = (_c = result.columnProperties[result.logicform.groupby.length]) === null || _c === void 0 ? void 0 : _c.ui) === null || _d === void 0 ? void 0 : _d.representation) {
return (_f = (_e = result.columnProperties[result.logicform.groupby.length]) === null || _e === void 0 ? void 0 : _e.ui) === null || _f === void 0 ? void 0 : _f.representation;
} // console.log("result", result);
// 如果是categorical的,并且数量没到pieThreshold,用pie
if ((groupbyProp === null || groupbyProp === void 0 ? void 0 : groupbyProp.is_categorical) && Array.isArray(result.result) && result.result.length <= pieThreshold && result.logicform.groupby.length === 1 && // 只需允许有一个维度
result.columnProperties.length === 2 && // 只需允许有一个pred
result.columnProperties[1].is_additive && // 不是百分比类型的,而是绝对值类型的
!result.logicform.limit // 不能有排名意图
) {
return "pie";
} // 如果是timestamp,用line
if ((groupbyProp === null || groupbyProp === void 0 ? void 0 : groupbyProp.type) === "timestamp" && !canUseDualAxis(result)) {
return "line";
} // 如果operator是pie,那么
if (result.logicform.groupby.length === 1 && // 只需允许有一个维度
((_g = result.logicform.preds) === null || _g === void 0 ? void 0 : _g.length) === 1 && // 只需允许有一个pred
result.logicform.preds[0].operator === "$percentage" // 百分比
) {
return "pie";
} // 如果是排名意图的,用bar
if (result.logicform.groupby.length === 1 && // 只允许有一个维度
result.logicform.sort && result.logicform.limit > 0 && ((_h = result.logicform.preds) === null || _h === void 0 ? void 0 : _h.length) === 1 // 只允许有一个指标
) {
return "bar";
}
if (barFirst) {
return "bar";
}
return "column";
}
if ((_j = logicform.preds) === null || _j === void 0 ? void 0 : _j.find(function (p) {
return p.operator === "$ent";
})) {
return "entity";
} // pattern for entity
if (result.schema.type === "entity" && result.result.length === 1 && !((_k = logicform.preds) === null || _k === void 0 ? void 0 : _k.find(function (p) {
return p.operator;
}))) {
return "entity";
}
if (logicform.entity_id) {
return "entity";
}
if (Scatter.valid(result) && (logicform.limit === -1 || logicform.limit > 100) // 这个条件有点硬编码,这个是为了不让普通的simple table进来的一个办法
) {
return "scatter";
}
if ((_l = logicform.preds) === null || _l === void 0 ? void 0 : _l.find(function (p) {
return p.operator;
})) {
return "value";
}
return "table";
};
var representations = Object.values(representationMap).filter(function (f) {
return !f.hideInMenu;
});
export default representations;