feat: allow nominal scale fallback to default size

This commit is contained in:
natoverse 2020-10-14 11:30:32 -07:00
Родитель 7d4f4adc46
Коммит a65de9e372
5 изменённых файлов: 52 добавлений и 95 удалений

Просмотреть файл

@ -155,25 +155,37 @@ export class Theme implements ITheme {
public scales = (): ColorScales => {
return {
nominal: (sizeOrDomain: number | string[] | number[]) => {
nominal: (sizeOrDomain?: number | string[] | number[]) => {
const size =
typeof sizeOrDomain === 'number' ? sizeOrDomain : sizeOrDomain.length
typeof sizeOrDomain === 'number'
? sizeOrDomain
: sizeOrDomain
? sizeOrDomain.length
: 10
const domain =
typeof sizeOrDomain !== 'number' ? sizeOrDomain : undefined
const scheme = this.getScheme(size)
return nominal(scheme.nominal, domain)
},
nominalBold: (sizeOrDomain: number | string[] | number[]) => {
nominalBold: (sizeOrDomain?: number | string[] | number[]) => {
const size =
typeof sizeOrDomain === 'number' ? sizeOrDomain : sizeOrDomain.length
typeof sizeOrDomain === 'number'
? sizeOrDomain
: sizeOrDomain
? sizeOrDomain.length
: 10
const domain =
typeof sizeOrDomain !== 'number' ? sizeOrDomain : undefined
const scheme = this.getScheme(size)
return nominal(scheme.nominalBold, domain)
},
nominalMuted: (sizeOrDomain: number | string[] | number[]) => {
nominalMuted: (sizeOrDomain?: number | string[] | number[]) => {
const size =
typeof sizeOrDomain === 'number' ? sizeOrDomain : sizeOrDomain.length
typeof sizeOrDomain === 'number'
? sizeOrDomain
: sizeOrDomain
? sizeOrDomain.length
: 10
const domain =
typeof sizeOrDomain !== 'number' ? sizeOrDomain : undefined
const scheme = this.getScheme(size)

Просмотреть файл

@ -52,17 +52,7 @@ describe('clone', () => {
test('no changes', () => {
const clone = theme.clone()
expect(clone.name).toBe(theme.name)
expect(
clone
.rect()
.stroke()
.hex(),
).toBe(
theme
.rect()
.stroke()
.hex(),
)
expect(clone.rect().stroke().hex()).toBe(theme.rect().stroke().hex())
})
test('basic property changes', () => {
@ -80,12 +70,7 @@ describe('clone', () => {
)
expect(clone.name).toBe('lighter')
expect(
clone
.rect()
.stroke()
.hex(),
).toBe('#ff0000')
expect(clone.rect().stroke().hex()).toBe('#ff0000')
})
test('basic config change + swap color space', () => {
@ -101,12 +86,14 @@ describe('clone', () => {
},
)
expect(
clone
.rect()
.stroke()
.rgbav(),
).toEqual([1.0, 0, 0, 1.0])
expect(clone.rect().stroke().rgbav()).toEqual([1.0, 0, 0, 1.0])
})
test('default nominal scale', () => {
// confirm that the default uses 10
const scale = theme.scales().nominal()
const arr = scale.toArray()
expect(arr).toHaveLength(10)
})
test('computed nominal scale', () => {
@ -138,12 +125,7 @@ describe('apply config', () => {
})
test('any optional mark properties', () => {
expect(
theme
.arc()
.stroke()
.hex(),
).toBe('#ff0000')
expect(theme.arc().stroke().hex()).toBe('#ff0000')
})
})
@ -151,12 +133,7 @@ describe('apply config', () => {
const theme = new Theme(lightSpec, lightConfig)
test('basic color', () => {
expect(
theme
.arc()
.stroke()
.rgbav(),
).toEqual(STROKE_VECTOR)
expect(theme.arc().stroke().rgbav()).toEqual(STROKE_VECTOR)
})
test('computed nominal scale', () => {
@ -169,12 +146,7 @@ describe('apply config', () => {
const theme = new Theme(lightSpec, lightConfig)
test('basic color', () => {
expect(
theme
.arc()
.stroke()
.rgbaint(),
).toBe(STROKE_NUMBER)
expect(theme.arc().stroke().rgbaint()).toBe(STROKE_NUMBER)
})
test('computed nominal scale', () => {

Просмотреть файл

@ -9,59 +9,32 @@ import { Theme, ThemeVariant, Transformer } from '../types'
* @param theme
*/
export const office: Transformer = (theme: Theme) => {
const nominal = theme.scales().nominal(10)
const nominal = theme.scales().nominal().toArray()
return {
dark1:
theme.variant === ThemeVariant.Dark
? theme
.application()
.background()
.hex()
: theme
.text()
.fill()
.hex(),
? theme.application().background().hex()
: theme.text().fill().hex(),
light1:
theme.variant === ThemeVariant.Dark
? theme
.text()
.fill()
.hex()
: theme
.application()
.background()
.hex(),
? theme.text().fill().hex()
: theme.application().background().hex(),
dark2:
theme.variant === ThemeVariant.Dark
? theme
.plotArea()
.fill()
.hex()
: theme
.application()
.accent()
.hex(),
? theme.plotArea().fill().hex()
: theme.application().accent().hex(),
light2:
theme.variant === ThemeVariant.Dark
? theme
.application()
.accent()
.hex()
: theme
.plotArea()
.fill()
.hex(),
accent1: nominal(0).hex(),
accent2: nominal(1).hex(),
accent3: nominal(2).hex(),
accent4: nominal(3).hex(),
accent5: nominal(4).hex(),
accent6: nominal(5).hex(),
hyperlink: theme
.application()
.accent()
.hex(),
followedHyperlink: nominal(0).hex(),
? theme.application().accent().hex()
: theme.plotArea().fill().hex(),
accent1: nominal[0],
accent2: nominal[1],
accent3: nominal[2],
accent4: nominal[3],
accent5: nominal[4],
accent6: nominal[5],
hyperlink: theme.application().accent().hex(),
followedHyperlink: nominal[0],
}
}

Просмотреть файл

@ -142,13 +142,13 @@ export interface ContinuousColorScaleFunction extends ColorScaleFunction {
export interface ColorScales {
nominal: (
sizeOrDomain: number | string[] | number[],
sizeOrDomain?: number | string[] | number[],
) => NominalColorScaleFunction
nominalBold: (
sizeOrDomain: number | string[] | number[],
sizeOrDomain?: number | string[] | number[],
) => NominalColorScaleFunction
nominalMuted: (
sizeOrDomain: number | string[] | number[],
sizeOrDomain?: number | string[] | number[],
) => NominalColorScaleFunction
/**
* Constructs a sequential numeric scale based on the input domain and specified ScaleType

Просмотреть файл

@ -122,7 +122,7 @@ function group(theme: Theme) {
// TODO: allow configuration of the length
function range(theme: Theme) {
return {
category: theme.scales().nominal(10).toArray(),
category: theme.scales().nominal().toArray(),
diverging: theme.scales().diverging().toArray(10),
heatmap: theme.scales().sequential().toArray(10),
ordinal: theme.scales().sequential().toArray(10),