Skip to content

Commit

Permalink
feat: support discretizing scales for shape
Browse files Browse the repository at this point in the history
fixes #7167
  • Loading branch information
domoritz committed Jan 11, 2021
1 parent 096ed88 commit 4849e12
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
5 changes: 2 additions & 3 deletions src/scale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export const SCALE_CATEGORY_INDEX: Record<ScaleType, ScaleType | 'numeric' | 'or
threshold: 'discretizing'
};

export const SCALE_TYPES = keys(SCALE_CATEGORY_INDEX) as ScaleType[];
export const SCALE_TYPES: ScaleType[] = keys(SCALE_CATEGORY_INDEX);

/**
* Whether the two given scale types can be merged together.
Expand Down Expand Up @@ -839,8 +839,7 @@ export function channelSupportScaleType(channel: Channel, scaleType: ScaleType):
case CHANNEL.STROKE:
return scaleType !== 'band'; // band does not make sense with color
case CHANNEL.STROKEDASH:
return scaleType === 'ordinal' || isContinuousToDiscrete(scaleType);
case CHANNEL.SHAPE:
return scaleType === 'ordinal'; // shape = lookup only
return scaleType === 'ordinal' || isContinuousToDiscrete(scaleType);
}
}
30 changes: 25 additions & 5 deletions test/scale.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import {ScaleChannel, SCALE_CHANNELS} from '../src/channel';
import * as scale from '../src/scale';
import {channelSupportScaleType, CONTINUOUS_TO_CONTINUOUS_SCALES, ScaleType, SCALE_TYPES} from '../src/scale';
import {
channelSupportScaleType,
CONTINUOUS_TO_CONTINUOUS_SCALES,
CONTINUOUS_TO_DISCRETE_SCALES,
ScaleType,
SCALE_TYPES
} from '../src/scale';
import {some} from '../src/util';
import {without} from './util';

Expand Down Expand Up @@ -51,14 +57,28 @@ describe('scale', () => {
}
});

it('shape should support only ordinal', () => {
expect(channelSupportScaleType('shape', 'ordinal')).toBeTruthy();
const nonOrdinal = without<ScaleType>(SCALE_TYPES, ['ordinal']);
for (const scaleType of nonOrdinal) {
it('shape should support only ordinal and discretizing scales', () => {
const supportedScales = [ScaleType.ORDINAL, ...CONTINUOUS_TO_DISCRETE_SCALES] as const;
for (const scaleType of supportedScales) {
expect(channelSupportScaleType('shape', scaleType)).toBeTruthy();
}
const unsupported = without(SCALE_TYPES, supportedScales);
for (const scaleType of unsupported) {
expect(!channelSupportScaleType('shape', scaleType)).toBeTruthy();
}
});

it('strokeDash should support only ordinal and discretizing scales', () => {
const supportedScales = [ScaleType.ORDINAL, ...CONTINUOUS_TO_DISCRETE_SCALES] as const;
for (const scaleType of supportedScales) {
expect(channelSupportScaleType('strokeDash', scaleType)).toBeTruthy();
}
const unsupported = without(SCALE_TYPES, supportedScales);
for (const scaleType of unsupported) {
expect(!channelSupportScaleType('strokeDash', scaleType)).toBeTruthy();
}
});

it('color should support all scale types except band', () => {
for (const scaleType of SCALE_TYPES) {
expect(channelSupportScaleType('color', scaleType)).toEqual(scaleType !== 'band');
Expand Down

0 comments on commit 4849e12

Please sign in to comment.