Skip to content

Commit

Permalink
feat(scales): add reverse option to symlog scale
Browse files Browse the repository at this point in the history
  • Loading branch information
wyze authored and plouc committed Jun 8, 2021
1 parent 0f6f342 commit 5c0cc65
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 2 deletions.
1 change: 1 addition & 0 deletions packages/scales/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ declare module '@nivo/scales' {
constant?: number
min?: 'auto' | number
max?: 'auto' | number
reverse?: boolean
}

export interface BandScale {
Expand Down
7 changes: 5 additions & 2 deletions packages/scales/src/symlogScale.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { scaleSymlog } from 'd3-scale'
import PropTypes from 'prop-types'

export const symlogScale = (
{ axis, constant = 1, min = 'auto', max = 'auto' },
{ axis, constant = 1, min = 'auto', max = 'auto', reverse = false },
xy,
width,
height
Expand All @@ -29,11 +29,13 @@ export const symlogScale = (
}

const scale = scaleSymlog()
.domain([minValue, maxValue])
.constant(constant)
.rangeRound(axis === 'x' ? [0, size] : [size, 0])
.nice()

if (reverse === true) scale.domain([maxValue, minValue])
else scale.domain([minValue, maxValue])

scale.type = 'symlog'

return scale
Expand All @@ -44,4 +46,5 @@ export const symLogScalePropTypes = {
constant: PropTypes.number,
min: PropTypes.oneOfType([PropTypes.oneOf(['auto']), PropTypes.number]),
max: PropTypes.oneOfType([PropTypes.oneOf(['auto']), PropTypes.number]),
reverse: PropTypes.bool,
}
69 changes: 69 additions & 0 deletions packages/scales/tests/symlogScale.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { symlogScale } from '../src/symlogScale'

it(`should be able to build a symlog scale for x axis`, () => {
const scale = symlogScale({ axis: 'x' }, { x: { min: 0, max: 1 } }, 100, 100)

expect(scale(0)).toBe(0)
expect(scale(0.5)).toBe(58)
expect(scale(1)).toBe(100)
})

it(`should be able to build a symlog scale for y axis`, () => {
const scale = symlogScale({ axis: 'y' }, { y: { min: 0, max: 1 } }, 100, 100)

expect(scale(0)).toBe(100)
expect(scale(0.5)).toBe(42)
expect(scale(1)).toBe(0)
})

it(`should allow to define min value for x axis`, () => {
const scale = symlogScale({ axis: 'x', min: 0.5 }, { x: { min: 0, max: 1 } }, 100, 100)

expect(scale.domain()[0]).toBe(0.5)
expect(scale(0)).toBe(-141)
expect(scale(0.5)).toBe(0)
expect(scale(1)).toBe(100)
})

it(`should allow to define min value for y axis`, () => {
const scale = symlogScale({ axis: 'y', min: 0.5 }, { y: { min: 0, max: 1 } }, 100, 100)

expect(scale.domain()[0]).toBe(0.5)
expect(scale(0)).toBe(241)
expect(scale(0.5)).toBe(100)
expect(scale(1)).toBe(0)
})

it(`should allow to define max value for x axis`, () => {
const scale = symlogScale({ axis: 'x', max: 2 }, { x: { min: 0, max: 1 } }, 100, 100)

expect(scale.domain()[1]).toBe(2)
expect(scale(0)).toBe(0)
expect(scale(0.5)).toBe(37)
expect(scale(1)).toBe(63)
})

it(`should allow to define max value for y axis`, () => {
const scale = symlogScale({ axis: 'y', max: 2 }, { y: { min: 0, max: 1 } }, 100, 100)

expect(scale.domain()[1]).toBe(2)
expect(scale(0)).toBe(100)
expect(scale(0.5)).toBe(63)
expect(scale(1)).toBe(37)
})

it(`should allow to reverse domain`, () => {
const scale = symlogScale({ axis: 'y', reverse: true }, { y: { min: 0, max: 1 } }, 100, 100)

expect(scale(0)).toBe(0)
expect(scale(0.5)).toBe(58)
expect(scale(1)).toBe(100)
})

it(`should allow to adjust the constant`, () => {
const scale = symlogScale({ axis: 'x', constant: 0.1 }, { x: { min: 0, max: 1 } }, 100, 100)

expect(scale(0)).toBe(0)
expect(scale(0.5)).toBe(75)
expect(scale(1)).toBe(100)
})

0 comments on commit 5c0cc65

Please sign in to comment.