@qn-pandora/pandora-visualization
Version:
Pandora 通用可视化库
104 lines (103 loc) • 3.85 kB
JavaScript
var __values = (this && this.__values) || function(o) {
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
if (m) return m.call(o);
if (o && typeof o.length === "number") return {
next: function () {
if (o && i >= o.length) o = void 0;
return { value: o && o[i++], done: !o };
}
};
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
};
import { isNil, isNumber, size, toNumber, findIndex, first, last, get } from 'lodash';
import { ThresholdType } from '../../../constants/table-style';
import { getRuleColor } from '../transforms/color-splitter';
export function getSplitterRangeColor(defaultColor, colorSplitter, value) {
if (!colorSplitter) {
return defaultColor;
}
var colorPalette = colorSplitter.colorPalette, _a = colorSplitter.scale, scale = _a === void 0 ? [] : _a;
var colors = colorPalette.value;
if (!isNil(value) &&
typeof value !== 'boolean' &&
isNumber(toNumber(value)) &&
!isNaN(toNumber(value)) &&
size(scale) &&
size(colors)) {
var min = first(scale);
var max = last(scale);
if (isNumber(min) && toNumber(value) <= min) {
return defaultColor;
}
var index = scale.findIndex(function (item) { return isNumber(item) && toNumber(item) >= toNumber(value); });
if (index >= 1) {
return colors[index - 1];
}
if (!isNumber(max)) {
return last(colors);
}
}
return defaultColor;
}
var BuiltInFields = ['_span', '_spandays'];
// 去掉数组指定的数据,但保持源数组本身不变,生成新的数组
function spliceNoMutate(myArray, indexToRemove) {
return myArray
.slice(0, indexToRemove)
.concat(myArray.slice(indexToRemove + 1));
}
export function filterBuiltInFields(dataset) {
var fields = dataset.fields, rows = dataset.rows;
BuiltInFields.forEach(function (builtInField) {
var index = findIndex(fields, function (field) { return field.key === builtInField; });
if (index !== -1) {
fields = spliceNoMutate(fields, index);
rows = rows.map(function (row) {
return spliceNoMutate(row, index);
});
}
});
return {
fields: fields,
rows: rows
};
}
export function getColor(type, defaultColor, isNumeric, splitter, value) {
var e_1, _a;
if (!splitter || !splitter.colorPalette) {
return defaultColor;
}
if (isNumeric) {
if (type === ThresholdType.Range) {
return getSplitterRangeColor(defaultColor, splitter, value);
}
if (type === ThresholdType.Constant) {
if (!isNil(value)) {
var colorPaletteValue = splitter.colorPalette.value || {};
try {
for (var _b = __values(Object.keys(colorPaletteValue)), _c = _b.next(); !_c.done; _c = _b.next()) {
var key = _c.value;
var newValue = value === 0 ? '0' : value ? value : '';
if ((key || '').toString() === newValue.toString()) {
return get(colorPaletteValue, key);
}
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
}
finally { if (e_1) throw e_1.error; }
}
return defaultColor;
}
}
}
else {
if (!isNil(value)) {
return getRuleColor(defaultColor, splitter, value);
}
}
return defaultColor;
}