Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dataset trans2 #13127

Merged
merged 2 commits into from
Aug 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 45 additions & 44 deletions src/component/transform/sortTransform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ import {
DimensionLoose, SOURCE_FORMAT_KEYED_COLUMNS, DimensionIndex, OptionDataValue
} from '../../util/types';
import { makePrintable, throwError } from '../../util/log';
import { isArray, each, hasOwn } from 'zrender/src/core/util';
import { isArray, each } from 'zrender/src/core/util';
import { normalizeToArray } from '../../util/model';
import { parseDate } from '../../util/number';
import {
RawValueParserType, getRawValueParser, SortOrderComparator
} from '../../data/helper/dataValueHelper';

/**
* @usage
Expand Down Expand Up @@ -52,26 +54,23 @@ export interface SortTransformOption extends DataTransformOption {
// PENDING: whether support { dimension: 'score', order: 'asc' } ?
type OrderExpression = {
dimension: DimensionLoose;
order: SortOrder;
parse?: 'time'
order: 'asc' | 'desc';
parser?: RawValueParserType;
// Value that is not comparable (like null/undefined) will be
// put to head or tail.
incomparable?: 'min' | 'max';
};

type SortOrder = 'asc' | 'desc';
const SortOrderValidMap = { asc: true, desc: true } as const;

let sampleLog = '';
if (__DEV__) {
sampleLog = [
'Valid config is like:',
'{ dimension: "age", order: "asc" }',
'or [{ dimension: "age", order: "asc"], { dimension: "date", order: "desc" }]'
].join('');
].join(' ');
}

const timeParser = function (val: OptionDataValue): number {
return +parseDate(val);
};


export const sortTransform: ExternalDataTransform<SortTransformOption> = {

Expand All @@ -97,13 +96,14 @@ export const sortTransform: ExternalDataTransform<SortTransformOption> = {

const orderDefList: {
dimIdx: DimensionIndex;
orderReturn: -1 | 1;
parser: (val: OptionDataValue) => number;
parser: ReturnType<typeof getRawValueParser>;
comparator: SortOrderComparator
}[] = [];
each(orderExprList, function (orderExpr) {
const dimLoose = orderExpr.dimension;
const order = orderExpr.order;
const parserName = orderExpr.parse;
const parserName = orderExpr.parser;
const incomparable = orderExpr.incomparable;

if (dimLoose == null) {
if (__DEV__) {
Expand All @@ -112,13 +112,28 @@ export const sortTransform: ExternalDataTransform<SortTransformOption> = {
throwError(errMsg);
}

if (!hasOwn(SortOrderValidMap, order)) {
if (order !== 'asc' && order !== 'desc') {
if (__DEV__) {
errMsg = 'Sort transform config must has "order" specified.' + sampleLog;
}
throwError(errMsg);
}

if (incomparable && (incomparable !== 'min' && incomparable !== 'max')) {
let errMsg = '';
if (__DEV__) {
errMsg = 'incomparable must be "min" or "max" rather than "' + incomparable + '".';
}
throwError(errMsg);
}
if (order !== 'asc' && order !== 'desc') {
let errMsg = '';
if (__DEV__) {
errMsg = 'order must be "asc" or "desc" rather than "' + order + '".';
}
throwError(errMsg);
}

const dimInfo = source.getDimensionInfo(dimLoose);
if (!dimInfo) {
if (__DEV__) {
Expand All @@ -131,24 +146,21 @@ export const sortTransform: ExternalDataTransform<SortTransformOption> = {
throwError(errMsg);
}

let parser;
if (parserName) {
if (parserName !== 'time') {
if (__DEV__) {
errMsg = makePrintable(
'Invalid parser name' + parserName + '.\n',
'Illegal config:', orderExpr, '.\n'
);
}
throwError(errMsg);
const parser = parserName ? getRawValueParser(parserName) : null;
if (parserName && !parser) {
if (__DEV__) {
errMsg = makePrintable(
'Invalid parser name ' + parserName + '.\n',
'Illegal config:', orderExpr, '.\n'
);
}
parser = timeParser;
throwError(errMsg);
}

orderDefList.push({
dimIdx: dimInfo.index,
orderReturn: order === 'asc' ? -1 : 1,
parser: parser
parser: parser,
comparator: new SortOrderComparator(order, incomparable)
});
});

Expand Down Expand Up @@ -182,28 +194,17 @@ export const sortTransform: ExternalDataTransform<SortTransformOption> = {
if (item1 === headerPlaceholder) {
return 1;
}
// FIXME: check other empty?
// Always put empty item last?
if (item0 == null) {
return 1;
}
if (item1 == null) {
return -1;
}
// TODO Optimize a little: manually loop unrolling?
for (let i = 0; i < orderDefList.length; i++) {
const orderDef = orderDefList[i];
let val0 = source.retrieveItemValue(item0, orderDef.dimIdx);
let val1 = source.retrieveItemValue(item1, orderDef.dimIdx);
if (orderDef.parser) {
val0 = orderDef.parser(val0);
val1 = orderDef.parser(val1);
}
if (val0 < val1) {
return orderDef.orderReturn;
val0 = orderDef.parser(val0) as OptionDataValue;
val1 = orderDef.parser(val1) as OptionDataValue;
}
else if (val0 > val1) {
return -orderDef.orderReturn;
const result = orderDef.comparator.evaluate(val0, val1);
if (result !== 0) {
return result;
}
}
return 0;
Expand Down
2 changes: 1 addition & 1 deletion src/data/List.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import { PathStyleProps } from 'zrender/src/graphic/Path';
import type Graph from './Graph';
import type Tree from './Tree';
import type { VisualMeta } from '../component/visualMap/VisualMapModel';
import { parseDataValue } from './helper/parseDataValue';
import { parseDataValue } from './helper/dataValueHelper';


const isObject = zrUtil.isObject;
Expand Down
Loading