This repository has been archived by the owner on Jun 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: convert scale config to data-ui's config correctly (#115)
- Loading branch information
Showing
6 changed files
with
77 additions
and
9 deletions.
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
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
62 changes: 62 additions & 0 deletions
62
packages/superset-ui-preset-chart-xy/src/utils/convertScaleToDataUIScaleShape.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,62 @@ | ||
import { Value } from 'vega-lite/build/src/channeldef'; | ||
import { Scale } from '../encodeable/types/Scale'; | ||
|
||
type DataUIScaleType = 'time' | 'timeUtc' | 'linear' | 'band'; | ||
|
||
interface DataUIScale { | ||
type: DataUIScaleType; | ||
domain?: number[] | string[]; | ||
includeZero?: boolean; | ||
nice?: boolean; | ||
paddingInner?: number; | ||
paddingOuter?: number; | ||
range?: number[] | string[]; | ||
rangeRound?: number[] | string[]; | ||
} | ||
|
||
function isCompatibleDomainOrRange( | ||
array: Scale['domain'] | Scale['range'], | ||
): array is number[] | string[] { | ||
return ( | ||
typeof array !== 'undefined' && | ||
array.length > 0 && | ||
(typeof array[0] === 'string' || typeof array[0] === 'number') | ||
); | ||
} | ||
|
||
/** | ||
* Convert encodeable scale object into @data-ui's scale config | ||
* @param scale | ||
*/ | ||
export default function convertScaleToDataUIScale<Output extends Value>(scale: Scale<Output>) { | ||
const { type, domain, range, nice, paddingInner, paddingOuter } = scale; | ||
|
||
let outputType: DataUIScaleType; | ||
|
||
if (type === 'linear' || type === 'time' || type === 'band') { | ||
outputType = type; | ||
} else if (type === 'utc') { | ||
outputType = 'timeUtc'; | ||
} else { | ||
throw new Error(`Unsupported scale type: ${type}`); | ||
} | ||
|
||
const output: DataUIScale = { type: outputType }; | ||
if (isCompatibleDomainOrRange(domain)) { | ||
output.domain = domain; | ||
} | ||
if (isCompatibleDomainOrRange(range)) { | ||
output.range = range; | ||
} | ||
if (typeof nice !== 'undefined') { | ||
output.nice = nice; | ||
} | ||
if (typeof paddingInner !== 'undefined') { | ||
output.paddingInner = paddingInner; | ||
} | ||
if (typeof paddingOuter !== 'undefined') { | ||
output.paddingOuter = paddingOuter; | ||
} | ||
|
||
return output; | ||
} |