This repository was archived by the owner on Nov 3, 2023. It is now read-only.
forked from apache/superset
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add brand color * update test * add unit tests
- Loading branch information
1 parent
c19a55f
commit fbdefa7
Showing
5 changed files
with
92 additions
and
1 deletion.
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
29 changes: 29 additions & 0 deletions
29
...tend/temporary_superset_ui/superset-ui/packages/superset-ui-color/src/SequentialScheme.js
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,9 +1,38 @@ | ||
import { scaleLinear } from 'd3-scale'; | ||
import ColorScheme from './ColorScheme'; | ||
|
||
function range(count) { | ||
return [...Array(count).keys()]; | ||
} | ||
|
||
export default class SequentialScheme extends ColorScheme { | ||
constructor(input) { | ||
super(input); | ||
const { isDiverging = false } = input; | ||
this.isDiverging = isDiverging; | ||
} | ||
|
||
createLinearScale(extent = [0, 1]) { | ||
// Create matching domain | ||
// because D3 continuous scale uses piecewise mapping | ||
// between domain and range. | ||
const valueScale = scaleLinear().range(extent); | ||
const denominator = this.colors.length - 1; | ||
const domain = range(this.colors.length).map(i => valueScale(i / denominator)); | ||
|
||
return scaleLinear() | ||
.domain(domain) | ||
.range(this.colors) | ||
.clamp(true); | ||
} | ||
|
||
getColors(numColors = this.colors.length) { | ||
if (numColors === this.colors.length) { | ||
return this.colors; | ||
} | ||
const colorScale = this.createLinearScale(); | ||
const denominator = numColors - 1; | ||
|
||
return range(numColors).map(i => colorScale(i / denominator)); | ||
} | ||
} |
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
57 changes: 57 additions & 0 deletions
57
...emporary_superset_ui/superset-ui/packages/superset-ui-color/test/SequentialScheme.test.js
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,57 @@ | ||
import SequentialScheme from '../src/SequentialScheme'; | ||
|
||
describe('SequentialScheme', () => { | ||
const scheme = new SequentialScheme({ | ||
name: 'white to black', | ||
colors: ['#fff', '#000'], | ||
}); | ||
it('exists', () => { | ||
expect(SequentialScheme).toBeDefined(); | ||
}); | ||
describe('new SequentialScheme()', () => { | ||
it('creates new instance', () => { | ||
const scheme2 = new SequentialScheme({ | ||
name: 'white to black', | ||
colors: ['#fff', '#000'], | ||
}); | ||
expect(scheme2).toBeInstanceOf(SequentialScheme); | ||
}); | ||
}); | ||
describe('.createLinearScale(extent)', () => { | ||
it('returns a linear scale for the given extent', () => { | ||
const scale = scheme.createLinearScale([10, 100]); | ||
expect(scale(1)).toEqual('rgb(255, 255, 255)'); | ||
expect(scale(10)).toEqual('rgb(255, 255, 255)'); | ||
expect(scale(55)).toEqual('rgb(128, 128, 128)'); | ||
expect(scale(100)).toEqual('rgb(0, 0, 0)'); | ||
expect(scale(1000)).toEqual('rgb(0, 0, 0)'); | ||
}); | ||
it('uses [0, 1] as extent if not specified', () => { | ||
const scale = scheme.createLinearScale(); | ||
expect(scale(-1)).toEqual('rgb(255, 255, 255)'); | ||
expect(scale(0)).toEqual('rgb(255, 255, 255)'); | ||
expect(scale(0.5)).toEqual('rgb(128, 128, 128)'); | ||
expect(scale(1)).toEqual('rgb(0, 0, 0)'); | ||
expect(scale(2)).toEqual('rgb(0, 0, 0)'); | ||
}); | ||
}); | ||
describe('.getColors(numColors)', () => { | ||
it('returns the original colors if numColors is not specified', () => { | ||
expect(scheme.getColors()).toEqual(['#fff', '#000']); | ||
}); | ||
it('returns the exact number of colors if numColors is specified', () => { | ||
expect(scheme.getColors(2)).toEqual(['#fff', '#000']); | ||
expect(scheme.getColors(3)).toEqual([ | ||
'rgb(255, 255, 255)', | ||
'rgb(128, 128, 128)', | ||
'rgb(0, 0, 0)', | ||
]); | ||
expect(scheme.getColors(4)).toEqual([ | ||
'rgb(255, 255, 255)', | ||
'rgb(170, 170, 170)', | ||
'rgb(85, 85, 85)', | ||
'rgb(0, 0, 0)', | ||
]); | ||
}); | ||
}); | ||
}); |
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