diff --git a/packages/graphic-walker/src/vis/spec/aggregate.ts b/packages/graphic-walker/src/vis/spec/aggregate.ts index 2fa131f9..4b6f975c 100644 --- a/packages/graphic-walker/src/vis/spec/aggregate.ts +++ b/packages/graphic-walker/src/vis/spec/aggregate.ts @@ -2,9 +2,12 @@ import { COUNT_FIELD_ID } from '../../constants'; import { IViewField } from '../../interfaces'; import { getMeaAggKey } from '../../utils'; -export function channelAggregate(encoding: { [key: string]: any }, fields: IViewField[]) { +export function channelAggregate(encoding: { [key: string]: any }, channels: WeakMap) { Object.values(encoding).forEach((c) => { - const targetField = fields.find((f) => f.fid === c.field && !('aggregate' in c)); + if ('aggregate' in c) { + return; + } + const targetField = channels.get(c); if (targetField && targetField.fid === COUNT_FIELD_ID) { c.title = 'Count'; c.field = getMeaAggKey(targetField.fid, targetField.aggName) diff --git a/packages/graphic-walker/src/vis/spec/encode.ts b/packages/graphic-walker/src/vis/spec/encode.ts index 69af8803..e65afde0 100644 --- a/packages/graphic-walker/src/vis/spec/encode.ts +++ b/packages/graphic-walker/src/vis/spec/encode.ts @@ -26,7 +26,8 @@ export function availableChannels(geomType: string): Set { } return new Set(['column', 'opacity', 'color', 'row', 'size', 'x', 'y', 'xOffset', 'yOffset', 'shape']); } -export function channelEncode(props: IEncodeProps) { +export function channelEncode(props: IEncodeProps): [{ [key: string]: any }, WeakMap] { + const channels = new WeakMap(); const avcs = availableChannels(props.geomType); const encoding: { [key: string]: any } = {}; Object.keys(props) @@ -46,6 +47,7 @@ export function channelEncode(props: IEncodeProps) { if (props[c].analyticType === 'measure') { encoding[c].type = 'quantitative'; } + channels.set(encoding[c], props[c]); } }); // FIXME: temporal fix, only for x and y relative order @@ -70,5 +72,5 @@ export function channelEncode(props: IEncodeProps) { } } } - return encoding; + return [encoding, channels]; } diff --git a/packages/graphic-walker/src/vis/spec/view.ts b/packages/graphic-walker/src/vis/spec/view.ts index 770cd8e2..09f56a85 100644 --- a/packages/graphic-walker/src/vis/spec/view.ts +++ b/packages/graphic-walker/src/vis/spec/view.ts @@ -1,4 +1,4 @@ -import { ISemanticType, IStackMode, IViewField } from '../../interfaces'; +import { ISemanticType, IStackMode } from '../../interfaces'; import { autoMark } from './mark'; import { NULL_FIELD } from './field'; import { channelAggregate } from './aggregate'; @@ -32,7 +32,6 @@ export function getSingleView(props: SingleViewProps) { geomType, hideLegend = false, } = props; - const fields: IViewField[] = [x, y, color, opacity, size, shape, row, column, xOffset, yOffset, theta, radius, text]; let markType = geomType; let config: any = {}; if (hideLegend) { @@ -47,7 +46,7 @@ export function getSingleView(props: SingleViewProps) { markType = autoMark(types); } - let encoding = channelEncode({ + let [encoding, mapping] = channelEncode({ geomType: markType, x, y, @@ -65,7 +64,7 @@ export function getSingleView(props: SingleViewProps) { text }); if (defaultAggregated) { - channelAggregate(encoding, fields); + channelAggregate(encoding, mapping); } addTooltipEncode(encoding, details, defaultAggregated); channelStack(encoding, stack);