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 271
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(encodable): fill missing fields in user-specified channel defini…
…tion (#222) * feat: fill missing fields in user-specified channel definition * fix: type * test: add unit tests * fix: lint * test: add more unit tests * test: add unit tests * test: fix unit tests * fix: unit tests * refactor: change order of parameters * fix: type annotation * fix: type annotation * feat: add generic support * refactor: reorder params * fix: rename variables
- Loading branch information
Showing
24 changed files
with
743 additions
and
80 deletions.
There are no files selected for viewing
73 changes: 73 additions & 0 deletions
73
packages/superset-ui-encodable/src/fillers/completeAxisConfig.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,73 @@ | ||
/* eslint-disable no-magic-numbers */ | ||
import isEnabled from '../utils/isEnabled'; | ||
import { isTypedFieldDef } from '../typeGuards/ChannelDef'; | ||
import { ChannelDef, PositionFieldDef } from '../types/ChannelDef'; | ||
import { ChannelType } from '../types/Channel'; | ||
import { isXOrY, isX } from '../typeGuards/Channel'; | ||
import { RequiredSome } from '../types/Base'; | ||
import { AxisConfig, LabelOverlapStrategy } from '../types/Axis'; | ||
import expandLabelOverlapStrategy from './expandLabelOverlapStrategy'; | ||
|
||
function isChannelDefWithAxisSupport( | ||
channelType: ChannelType, | ||
channelDef: ChannelDef, | ||
): channelDef is PositionFieldDef { | ||
return isTypedFieldDef(channelDef) && isXOrY(channelType); | ||
} | ||
|
||
export type CompleteAxisConfig = | ||
| false | ||
| RequiredSome< | ||
Omit<AxisConfig, 'labelOverlap'>, | ||
| 'labelAngle' | ||
| 'labelFlush' | ||
| 'labelPadding' | ||
| 'orient' | ||
| 'tickCount' | ||
| 'ticks' | ||
| 'title' | ||
| 'titlePadding' | ||
> & { | ||
labelOverlap: LabelOverlapStrategy; | ||
}; | ||
|
||
export default function completeAxisConfig( | ||
channelType: ChannelType, | ||
channelDef: ChannelDef, | ||
): CompleteAxisConfig { | ||
if (isChannelDefWithAxisSupport(channelType, channelDef) && isEnabled(channelDef.axis)) { | ||
const axis = | ||
channelDef.axis === true || typeof channelDef.axis === 'undefined' ? {} : channelDef.axis; | ||
|
||
const isXChannel = isX(channelType); | ||
|
||
const { | ||
format = channelDef.format, | ||
labelAngle = 0, | ||
labelFlush = true, | ||
labelOverlap, | ||
labelPadding = 4, | ||
orient = isXChannel ? 'bottom' : 'left', | ||
tickCount = 5, | ||
ticks = true, | ||
title = channelDef.title!, | ||
titlePadding = 4, | ||
} = axis; | ||
|
||
return { | ||
...axis, | ||
format, | ||
labelAngle, | ||
labelFlush, | ||
labelOverlap: expandLabelOverlapStrategy(channelType, labelOverlap), | ||
labelPadding, | ||
orient, | ||
tickCount, | ||
ticks, | ||
title, | ||
titlePadding, | ||
}; | ||
} | ||
|
||
return false as const; | ||
} |
32 changes: 32 additions & 0 deletions
32
packages/superset-ui-encodable/src/fillers/completeChannelDef.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,32 @@ | ||
import { ChannelDef } from '../types/ChannelDef'; | ||
import { ChannelType } from '../types/Channel'; | ||
import { isFieldDef } from '../typeGuards/ChannelDef'; | ||
import completeAxisConfig, { CompleteAxisConfig } from './completeAxisConfig'; | ||
import completeScaleConfig, { CompleteScaleConfig } from './completeScaleConfig'; | ||
import { Value } from '../types/VegaLite'; | ||
|
||
type CompleteChannelDef<Output extends Value = Value> = Omit< | ||
ChannelDef, | ||
'title' | 'axis' | 'scale' | ||
> & { | ||
axis: CompleteAxisConfig; | ||
scale: CompleteScaleConfig<Output>; | ||
title: string; | ||
}; | ||
|
||
export default function completeChannelDef<Output extends Value = Value>( | ||
channelType: ChannelType, | ||
channelDef: ChannelDef<Output>, | ||
): CompleteChannelDef<Output> { | ||
// Fill top-level properties | ||
const copy = { | ||
...channelDef, | ||
title: isFieldDef(channelDef) ? channelDef.title || channelDef.field : '', | ||
}; | ||
|
||
return { | ||
...copy, | ||
axis: completeAxisConfig(channelType, copy), | ||
scale: completeScaleConfig(channelType, copy), | ||
}; | ||
} |
41 changes: 41 additions & 0 deletions
41
packages/superset-ui-encodable/src/fillers/completeScaleConfig.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,41 @@ | ||
import { isTypedFieldDef } from '../typeGuards/ChannelDef'; | ||
import inferScaleType from './inferScaleType'; | ||
import { isContinuousScaleConfig, isScaleConfigWithZero } from '../typeGuards/Scale'; | ||
import { ScaleConfig } from '../types/Scale'; | ||
import { ChannelDef } from '../types/ChannelDef'; | ||
import isEnabled from '../utils/isEnabled'; | ||
import { ChannelType } from '../types/Channel'; | ||
import { Value } from '../types/VegaLite'; | ||
|
||
export type CompleteScaleConfig<Output extends Value = Value> = false | ScaleConfig<Output>; | ||
|
||
export default function completeScaleConfig<Output extends Value = Value>( | ||
channelType: ChannelType, | ||
channelDef: ChannelDef<Output>, | ||
): CompleteScaleConfig<Output> { | ||
if (isTypedFieldDef(channelDef) && isEnabled(channelDef.scale)) { | ||
const { scale = {}, type, bin } = channelDef; | ||
const { type: scaleType = inferScaleType(channelType, type, bin) } = scale; | ||
|
||
if (typeof scaleType === 'undefined') { | ||
return false; | ||
} | ||
|
||
const filledScale = { ...scale, type: scaleType } as ScaleConfig<Output>; | ||
if (isContinuousScaleConfig(filledScale)) { | ||
if (typeof filledScale.nice === 'undefined') { | ||
filledScale.nice = true; | ||
} | ||
if (typeof filledScale.clamp === 'undefined') { | ||
filledScale.clamp = true; | ||
} | ||
} | ||
if (isScaleConfigWithZero(filledScale) && typeof filledScale.zero === 'undefined') { | ||
filledScale.zero = true; | ||
} | ||
|
||
return filledScale; | ||
} | ||
|
||
return false as const; | ||
} |
29 changes: 29 additions & 0 deletions
29
packages/superset-ui-encodable/src/fillers/expandLabelOverlapStrategy.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,29 @@ | ||
import { LabelOverlapStrategy, LabelOverlapType } from '../types/Axis'; | ||
import { ChannelType } from '../types/Channel'; | ||
import { isX } from '../typeGuards/Channel'; | ||
|
||
const STRATEGY_FLAT = { strategy: 'flat' } as const; | ||
const STRATEGY_ROTATE = { labelAngle: 40, strategy: 'rotate' } as const; | ||
|
||
export default function expandLabelOverlapStrategy( | ||
channelType: ChannelType, | ||
labelOverlap: LabelOverlapType = 'auto', | ||
): LabelOverlapStrategy { | ||
let output: LabelOverlapStrategy; | ||
switch (labelOverlap) { | ||
case 'flat': | ||
output = STRATEGY_FLAT; | ||
break; | ||
case 'rotate': | ||
output = STRATEGY_ROTATE; | ||
break; | ||
case 'auto': | ||
output = isX(channelType) ? STRATEGY_ROTATE : STRATEGY_FLAT; | ||
break; | ||
default: | ||
output = labelOverlap; | ||
break; | ||
} | ||
|
||
return { ...output }; | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { ChannelType } from '../types/Channel'; | ||
|
||
export function isX(channelType: ChannelType): channelType is 'X' | 'XBand' { | ||
return channelType === 'X' || channelType === 'XBand'; | ||
} | ||
|
||
export function isY(channelType: ChannelType): channelType is 'Y' | 'YBand' { | ||
return channelType === 'Y' || channelType === 'YBand'; | ||
} | ||
|
||
export function isXOrY(channelType: ChannelType): channelType is 'X' | 'XBand' | 'Y' | 'YBand' { | ||
return isX(channelType) || isY(channelType); | ||
} |
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
Oops, something went wrong.