This repository has been archived by the owner on Dec 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 272
feat: add functions for parsing scales #207
Merged
Merged
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
6beb085
feat: add more util functions
kristw 2dfa7da
feat: add unit test
kristw 39d31e0
feat: define HasToString
kristw f4b8642
fix: unit test
kristw fa40671
fix: update unit tests
kristw f7756bf
feat: add scale types
kristw fbb93ac
feat: update scale parsing
kristw b8f36ac
fix: enum
kristw ef288bf
feat: add color scale extraction
kristw f391f5f
refactor: create scale from config
kristw f027bcb
feat: parse more scales and add more test
kristw 9e428f0
feat: add tests for band and point
kristw 04ba182
test: add more unit tests
kristw 8f51dde
refactor: separate applyXXX into multiple files
kristw 5361078
feat: parse nice time
kristw 65ec8e3
test: add unit tests
kristw 39744e4
test: make 100% coverage
kristw 187e04b
fix: complete coverage
kristw 2e9323a
refactor: update type definitions
kristw 879c4ce
fix: address comments
kristw 21910c4
fix: add comments for date parts
kristw d6080a9
fix: build issue
kristw b40089d
fix: broken tests
kristw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
packages/superset-ui-encodeable/src/parsers/parseDateTime.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { parse, codegen } from 'vega-expression'; | ||
import { dateTimeExpr } from 'vega-lite/build/src/datetime'; | ||
import { DateTime } from '../types/VegaLite'; | ||
|
||
export default function parseDateTime(dateTime: string | number | DateTime) { | ||
if (typeof dateTime === 'number' || typeof dateTime === 'string') { | ||
return new Date(dateTime); | ||
} | ||
|
||
const expression = dateTimeExpr(dateTime, true) as string; | ||
const code = codegen({ globalvar: 'window' })(parse(expression)).code as string; | ||
// Technically the "code" here is safe to eval(), | ||
// but we will use more conservative approach and manually parse at the moment. | ||
const isUtc = code.startsWith('Date.UTC'); | ||
|
||
const dateParts = code | ||
.replace(/^(Date[.]UTC|new[ ]Date)\(/, '') | ||
.replace(/\)$/, '') | ||
.split(',') | ||
.map((chunk: string) => Number(chunk.trim())) as [ | ||
number, // year | ||
number, // month | ||
number, // date | ||
number, // hours | ||
number, // minutes | ||
number, // seconds | ||
number, // milliseconds | ||
]; | ||
|
||
return isUtc ? new Date(Date.UTC(...dateParts)) : new Date(...dateParts); | ||
} |
11 changes: 11 additions & 0 deletions
11
packages/superset-ui-encodeable/src/parsers/scale/applyAlign.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { Value } from '../../types/VegaLite'; | ||
import { ScaleConfig, D3Scale } from '../../types/Scale'; | ||
|
||
export default function applyAlign<Output extends Value>( | ||
config: ScaleConfig<Output>, | ||
scale: D3Scale<Output>, | ||
) { | ||
if ('align' in config && typeof config.align !== 'undefined' && 'align' in scale) { | ||
scale.align(config.align); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
packages/superset-ui-encodeable/src/parsers/scale/applyClamp.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { Value } from '../../types/VegaLite'; | ||
import { ScaleConfig, D3Scale } from '../../types/Scale'; | ||
|
||
export default function applyClamp<Output extends Value>( | ||
config: ScaleConfig<Output>, | ||
scale: D3Scale<Output>, | ||
) { | ||
if ('clamp' in config && typeof config.clamp !== 'undefined' && 'clamp' in scale) { | ||
scale.clamp(config.clamp); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
packages/superset-ui-encodeable/src/parsers/scale/applyDomain.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { Value } from '../../types/VegaLite'; | ||
import { ScaleConfig, D3Scale, TimeScaleConfig } from '../../types/Scale'; | ||
import parseDateTime from '../parseDateTime'; | ||
import inferElementTypeFromUnionOfArrayTypes from '../../utils/inferElementTypeFromUnionOfArrayTypes'; | ||
import { isTimeScale } from '../../typeGuards/Scale'; | ||
|
||
export default function applyDomain<Output extends Value>( | ||
config: ScaleConfig<Output>, | ||
scale: D3Scale<Output>, | ||
) { | ||
const { domain, reverse, type } = config; | ||
if (typeof domain !== 'undefined') { | ||
const processedDomain = reverse ? domain.slice().reverse() : domain; | ||
if (isTimeScale(scale, type)) { | ||
const timeDomain = processedDomain as TimeScaleConfig['domain']; | ||
scale.domain(inferElementTypeFromUnionOfArrayTypes(timeDomain).map(d => parseDateTime(d))); | ||
} else { | ||
scale.domain(processedDomain); | ||
} | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
packages/superset-ui-encodeable/src/parsers/scale/applyInterpolate.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { Value } from '../../types/VegaLite'; | ||
import { ScaleConfig, D3Scale } from '../../types/Scale'; | ||
|
||
export default function applyInterpolate<Output extends Value>( | ||
config: ScaleConfig<Output>, | ||
scale: D3Scale<Output>, | ||
) { | ||
if ( | ||
'interpolate' in config && | ||
typeof config.interpolate !== 'undefined' && | ||
'interpolate' in scale | ||
) { | ||
// TODO: Need to convert interpolate string into interpolate function | ||
throw new Error('"scale.interpolate" is not supported yet.'); | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
packages/superset-ui-encodeable/src/parsers/scale/applyNice.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { | ||
timeSecond, | ||
timeMinute, | ||
timeHour, | ||
timeDay, | ||
timeYear, | ||
timeMonth, | ||
timeWeek, | ||
utcSecond, | ||
utcMinute, | ||
utcHour, | ||
utcDay, | ||
utcWeek, | ||
utcMonth, | ||
utcYear, | ||
CountableTimeInterval, | ||
} from 'd3-time'; | ||
import { ScaleTime } from 'd3-scale'; | ||
import { Value, ScaleType, NiceTime } from '../../types/VegaLite'; | ||
import { ScaleConfig, D3Scale } from '../../types/Scale'; | ||
|
||
const localTimeIntervals: { | ||
[key in NiceTime]: CountableTimeInterval; | ||
} = { | ||
day: timeDay, | ||
hour: timeHour, | ||
minute: timeMinute, | ||
month: timeMonth, | ||
second: timeSecond, | ||
week: timeWeek, | ||
year: timeYear, | ||
}; | ||
|
||
const utcIntervals: { | ||
[key in NiceTime]: CountableTimeInterval; | ||
} = { | ||
day: utcDay, | ||
hour: utcHour, | ||
minute: utcMinute, | ||
month: utcMonth, | ||
second: utcSecond, | ||
week: utcWeek, | ||
year: utcYear, | ||
}; | ||
|
||
// eslint-disable-next-line complexity | ||
export default function applyNice<Output extends Value>( | ||
config: ScaleConfig<Output>, | ||
scale: D3Scale<Output>, | ||
) { | ||
if ('nice' in config && typeof config.nice !== 'undefined' && 'nice' in scale) { | ||
const { nice } = config; | ||
if (typeof nice === 'boolean') { | ||
if (nice === true) { | ||
scale.nice(); | ||
} | ||
} else if (typeof nice === 'number') { | ||
scale.nice(nice); | ||
} else { | ||
const timeScale = scale as ScaleTime<Output, Output>; | ||
const { type } = config; | ||
if (typeof nice === 'string') { | ||
timeScale.nice(type === ScaleType.UTC ? utcIntervals[nice] : localTimeIntervals[nice]); | ||
} else { | ||
const { interval, step } = nice; | ||
const parsedInterval = (type === ScaleType.UTC | ||
? utcIntervals[interval] | ||
: localTimeIntervals[interval] | ||
).every(step); | ||
|
||
if (parsedInterval !== null) { | ||
timeScale.nice(parsedInterval as CountableTimeInterval); | ||
} | ||
} | ||
} | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
packages/superset-ui-encodeable/src/parsers/scale/applyPadding.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { Value } from '../../types/VegaLite'; | ||
import { ScaleConfig, D3Scale } from '../../types/Scale'; | ||
|
||
export default function applyPadding<Output extends Value>( | ||
config: ScaleConfig<Output>, | ||
scale: D3Scale<Output>, | ||
) { | ||
if ('padding' in config && typeof config.padding !== 'undefined' && 'padding' in scale) { | ||
scale.padding(config.padding); | ||
} | ||
|
||
if ( | ||
'paddingInner' in config && | ||
typeof config.paddingInner !== 'undefined' && | ||
'paddingInner' in scale | ||
) { | ||
scale.paddingInner(config.paddingInner); | ||
} | ||
|
||
if ( | ||
'paddingOuter' in config && | ||
typeof config.paddingOuter !== 'undefined' && | ||
'paddingOuter' in scale | ||
) { | ||
scale.paddingOuter(config.paddingOuter); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
packages/superset-ui-encodeable/src/parsers/scale/applyRange.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { getSequentialSchemeRegistry } from '@superset-ui/color'; | ||
import { Value } from '../../types/VegaLite'; | ||
import { ScaleConfig, D3Scale } from '../../types/Scale'; | ||
|
||
export default function applyRange<Output extends Value>( | ||
config: ScaleConfig<Output>, | ||
scale: D3Scale<Output>, | ||
) { | ||
const { range } = config; | ||
if (typeof range === 'undefined') { | ||
if ('scheme' in config && typeof config.scheme !== 'undefined') { | ||
const { scheme } = config; | ||
const colorScheme = getSequentialSchemeRegistry().get(scheme); | ||
if (typeof colorScheme !== 'undefined') { | ||
scale.range(colorScheme.colors as Output[]); | ||
} | ||
} | ||
} else { | ||
scale.range(range); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
packages/superset-ui-encodeable/src/parsers/scale/applyRound.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { interpolateRound } from 'd3-interpolate'; | ||
import { ScalePoint, ScaleBand } from 'd3-scale'; | ||
import { Value } from '../../types/VegaLite'; | ||
import { ScaleConfig, D3Scale, ContinuousD3Scale } from '../../types/Scale'; | ||
import { HasToString } from '../../types/Base'; | ||
|
||
export default function applyRound<Output extends Value>( | ||
config: ScaleConfig<Output>, | ||
scale: D3Scale<Output>, | ||
) { | ||
if ('round' in config && typeof config.round !== 'undefined') { | ||
const roundableScale = scale as | ||
| ContinuousD3Scale<number> | ||
| ScalePoint<HasToString> | ||
| ScaleBand<HasToString>; | ||
if ('round' in roundableScale) { | ||
roundableScale.round(config.round); | ||
} else { | ||
roundableScale.interpolate(interpolateRound); | ||
} | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
packages/superset-ui-encodeable/src/parsers/scale/applyZero.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { Value } from '../../types/VegaLite'; | ||
import { ScaleConfig, D3Scale, ContinuousD3Scale } from '../../types/Scale'; | ||
|
||
export default function applyZero<Output extends Value>( | ||
config: ScaleConfig<Output>, | ||
scale: D3Scale<Output>, | ||
) { | ||
if ('zero' in config && typeof config.zero !== 'undefined') { | ||
const [min, max] = (scale as ContinuousD3Scale<Output>).domain() as number[]; | ||
scale.domain([Math.min(0, min), Math.max(0, max)]); | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
packages/superset-ui-encodeable/src/parsers/scale/createScaleFromScaleConfig.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { CategoricalColorNamespace } from '@superset-ui/color'; | ||
import { ScaleType, Value } from '../../types/VegaLite'; | ||
import { ScaleConfig } from '../../types/Scale'; | ||
import createScaleFromScaleType from './createScaleFromScaleType'; | ||
import applyNice from './applyNice'; | ||
import applyZero from './applyZero'; | ||
import applyInterpolate from './applyInterpolate'; | ||
import applyRound from './applyRound'; | ||
import applyDomain from './applyDomain'; | ||
import applyRange from './applyRange'; | ||
import applyPadding from './applyPadding'; | ||
import applyAlign from './applyAlign'; | ||
import applyClamp from './applyClamp'; | ||
|
||
export default function createScaleFromScaleConfig<Output extends Value>( | ||
config: ScaleConfig<Output>, | ||
) { | ||
const { domain, range, reverse } = config; | ||
|
||
// Handle categorical color scales | ||
// An ordinal scale without specified range | ||
// is assumed to be a color scale. | ||
if (config.type === ScaleType.ORDINAL && typeof range === 'undefined') { | ||
const scheme = 'scheme' in config ? config.scheme : undefined; | ||
const namespace = 'namespace' in config ? config.namespace : undefined; | ||
const colorScale = CategoricalColorNamespace.getScale(scheme, namespace); | ||
|
||
// If domain is also provided, | ||
// ensure the nth item is assigned the nth color | ||
if (typeof domain !== 'undefined') { | ||
const { colors } = colorScale; | ||
(reverse ? domain.slice().reverse() : domain).forEach((value: any, index: number) => { | ||
colorScale.setColor(`${value}`, colors[index % colors.length]); | ||
}); | ||
} | ||
|
||
// Need to manually cast here to make the unioned output types | ||
// considered function. | ||
// Otherwise have to add type guards before using the scale function. | ||
// | ||
// const scaleFn = createScaleFromScaleConfig(...) | ||
// if (isAFunction(scaleFn)) const encodedValue = scaleFn(10) | ||
// | ||
// CategoricalColorScale is actually a function, | ||
// but TypeScript is not smart enough to realize that by itself. | ||
return (colorScale as unknown) as (val?: any) => string; | ||
} | ||
|
||
const scale = createScaleFromScaleType(config); | ||
// domain and range apply to all scales | ||
applyDomain(config, scale); | ||
applyRange(config, scale); | ||
// Sort other properties alphabetically. | ||
applyAlign(config, scale); | ||
applyClamp(config, scale); | ||
applyInterpolate(config, scale); | ||
applyNice(config, scale); | ||
applyPadding(config, scale); | ||
applyRound(config, scale); | ||
applyZero(config, scale); | ||
|
||
return scale; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah this is a tricky one