-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(encodable): handle edge cases when making domain includes zero (#…
…257) * feat: handle edge cases when making domain includes zero * test: remove redundant tests
- Loading branch information
1 parent
9ef8318
commit e719c19
Showing
3 changed files
with
47 additions
and
22 deletions.
There are no files selected for viewing
13 changes: 9 additions & 4 deletions
13
...ary_superset_ui/superset-ui/packages/superset-ui-encodable/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 |
---|---|---|
@@ -1,12 +1,17 @@ | ||
import { Value } from '../../types/VegaLite'; | ||
import { ScaleConfig, D3Scale, ContinuousD3Scale } from '../../types/Scale'; | ||
import { ScaleConfig, D3Scale } from '../../types/Scale'; | ||
import { isContinuousScale } from '../../typeGuards/Scale'; | ||
|
||
export default function applyZero<Output extends Value>( | ||
config: ScaleConfig<Output>, | ||
scale: D3Scale<Output>, | ||
) { | ||
if ('zero' in config && config.zero === true) { | ||
const [min, max] = (scale as ContinuousD3Scale<Output>).domain() as number[]; | ||
scale.domain([Math.min(0, min), Math.max(0, max)]); | ||
if ('zero' in config && config.zero === true && isContinuousScale(scale, config.type)) { | ||
const domain = scale.domain() as number[]; | ||
const [a, b] = domain; | ||
const isDescending = b < a; | ||
const [min, max] = isDescending ? [b, a] : [a, b]; | ||
const domainWithZero = [Math.min(0, min), Math.max(0, max)]; | ||
scale.domain(isDescending ? domainWithZero.reverse() : domainWithZero); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
...perset_ui/superset-ui/packages/superset-ui-encodable/test/parsers/scale/applyZero.test.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,38 @@ | ||
import { scaleLinear } from 'd3-scale'; | ||
import applyZero from '../../../src/parsers/scale/applyZero'; | ||
|
||
describe('applyZero()', () => { | ||
describe('zero = true', () => { | ||
it('positive domain', () => { | ||
const scale = scaleLinear().domain([2, 10]); | ||
applyZero({ type: 'linear', zero: true }, scale); | ||
expect(scale.domain()).toEqual([0, 10]); | ||
}); | ||
it('negative domain', () => { | ||
const scale = scaleLinear().domain([-10, -2]); | ||
applyZero({ type: 'linear', zero: true }, scale); | ||
expect(scale.domain()).toEqual([-10, 0]); | ||
}); | ||
it('domain contains zero', () => { | ||
const scale = scaleLinear().domain([-5, 5]); | ||
applyZero({ type: 'linear', zero: true }, scale); | ||
expect(scale.domain()).toEqual([-5, 5]); | ||
}); | ||
it('descending domain', () => { | ||
const scale = scaleLinear().domain([5, 1]); | ||
applyZero({ type: 'linear', zero: true }, scale); | ||
expect(scale.domain()).toEqual([5, 0]); | ||
}); | ||
it('descending negative domain', () => { | ||
const scale = scaleLinear().domain([-1, -5]); | ||
applyZero({ type: 'linear', zero: true }, scale); | ||
expect(scale.domain()).toEqual([0, -5]); | ||
}); | ||
}); | ||
|
||
it('zero = false', () => { | ||
const scale = scaleLinear().domain([2, 10]); | ||
applyZero({ type: 'linear', zero: false }, scale); | ||
expect(scale.domain()).toEqual([2, 10]); | ||
}); | ||
}); |
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