Skip to content

Commit

Permalink
@superset-ui/color (#20)
Browse files Browse the repository at this point in the history
* add initial files

* update references

* fix lint issues

* Define top-level exports

* disable linting for color schemes

* update test

* update travis

* update travis

* revert travis

* install once

* update clear()

* update core index test

* update color index test

* update travis

* try build before test

* update unit tes

* override beemo config

* remove test command

* update ignore path

* use new build config
  • Loading branch information
kristw authored and zhaoyongjie committed Nov 25, 2021
1 parent a10e176 commit 2490be7
Show file tree
Hide file tree
Showing 26 changed files with 1,477 additions and 8 deletions.
Expand Up @@ -4,18 +4,18 @@ node_js:
- 10.7

cache:
directories:
- node_modules
- npm: true
- yarn: true

matrix:
fast_finish: true

install:
- npm install
- npm install -g codecov
- yarn install

script:
- yarn install
- yarn run build
- yarn run test

after_script:
Expand Down
@@ -0,0 +1,6 @@
## Changelog

### 0.3.0

- Rename former `@superset-ui/core` to `@superset-ui/connection`.
- Add new `@superset-ui/core` with data structures and utilities.
Expand Up @@ -33,7 +33,7 @@
],
"license": "Apache-2.0",
"devDependencies": {
"@data-ui/build-config": "^0.0.25",
"@data-ui/build-config": "^0.0.26",
"husky": "^1.1.2",
"lerna": "^3.2.1",
"lint-staged": "^7.3.0",
Expand Down
@@ -0,0 +1,23 @@
## @superset-ui/color

[![Version](https://img.shields.io/npm/v/@superset-ui/color.svg?style=flat)](https://img.shields.io/npm/v/@superset-ui/color.svg?style=flat)
[![David (path)](https://img.shields.io/david/apache-superset/superset-ui.svg?path=packages%2Fsuperset-ui-color&style=flat-square)](https://david-dm.org/apache-superset/superset-ui?path=packages/superset-ui-color)

Description

#### Example usage

```js
import { xxx } from '@superset-ui/color';
```

#### API

`fn(args)`

- Do something

### Development

`@data-ui/build-config` is used to manage the build configuration for this package including babel
builds, jest testing, eslint, and prettier.
@@ -0,0 +1,31 @@
{
"name": "@superset-ui/color",
"version": "0.0.0",
"description": "Superset UI color",
"sideEffects": false,
"main": "lib/index.js",
"module": "esm/index.js",
"files": [
"esm",
"lib"
],
"repository": {
"type": "git",
"url": "git+https://github.com/apache-superset/superset-ui.git"
},
"keywords": [
"superset"
],
"author": "Superset",
"license": "Apache-2.0",
"bugs": {
"url": "https://github.com/apache-superset/superset-ui/issues"
},
"homepage": "https://github.com/apache-superset/superset-ui#readme",
"publishConfig": {
"access": "public"
},
"dependencies": {
"@superset-ui/core": "^0.3.0"
}
}
@@ -0,0 +1,62 @@
import CategoricalColorScale from './CategoricalColorScale';
import getCategoricalSchemeRegistry from './CategoricalSchemeRegistrySingleton';

export default class CategoricalColorNamespace {
constructor(name) {
this.name = name;
this.scales = {};
this.forcedItems = {};
}

getScale(schemeName) {
const name = schemeName || getCategoricalSchemeRegistry().getDefaultSchemeName();
const scale = this.scales[name];
if (scale) {
return scale;
}
const newScale = new CategoricalColorScale(
getCategoricalSchemeRegistry().get(name).colors,
this.forcedItems,
);
this.scales[name] = newScale;

return newScale;
}

/**
* Enforce specific color for given value
* This will apply across all color scales
* in this namespace.
* @param {*} value value
* @param {*} forcedColor color
*/
setColor(value, forcedColor) {
this.forcedItems[value] = forcedColor;

return this;
}
}

const namespaces = {};
export const DEFAULT_NAMESPACE = 'GLOBAL';

export function getNamespace(name = DEFAULT_NAMESPACE) {
const instance = namespaces[name];
if (instance) {
return instance;
}
const newInstance = new CategoricalColorNamespace(name);
namespaces[name] = newInstance;

return newInstance;
}

export function getColor(value, scheme, namespace) {
return getNamespace(namespace)
.getScale(scheme)
.getColor(value);
}

export function getScale(scheme, namespace) {
return getNamespace(namespace).getScale(scheme);
}
@@ -0,0 +1,62 @@
export function cleanValue(value) {
// for superset series that should have the same color
return String(value)
.trim()
.toLowerCase();
}

export default class CategoricalColorScale {
/**
* Constructor
* @param {*} colors an array of colors
* @param {*} parentForcedColors optional parameter that comes from parent
* (usually CategoricalColorNamespace) and supersede this.forcedColors
*/
constructor(colors, parentForcedColors) {
this.colors = colors;
this.parentForcedColors = parentForcedColors;
this.forcedColors = {};
this.seen = {};
this.fn = value => this.getColor(value);
}

getColor(value) {
const cleanedValue = cleanValue(value);

const parentColor = this.parentForcedColors && this.parentForcedColors[cleanedValue];
if (parentColor) {
return parentColor;
}

const forcedColor = this.forcedColors[cleanedValue];
if (forcedColor) {
return forcedColor;
}

const seenColor = this.seen[cleanedValue];
const { length } = this.colors;
if (seenColor !== undefined) {
return this.colors[seenColor % length];
}

const index = Object.keys(this.seen).length;
this.seen[cleanedValue] = index;

return this.colors[index % length];
}

/**
* Enforce specific color for given value
* @param {*} value value
* @param {*} forcedColor forcedColor
*/
setColor(value, forcedColor) {
this.forcedColors[value] = forcedColor;

return this;
}

toFunction() {
return this.fn;
}
}
@@ -0,0 +1,3 @@
import ColorScheme from './ColorScheme';

export default class CategoricalScheme extends ColorScheme {}
@@ -0,0 +1,8 @@
import { makeSingleton } from '@superset-ui/core';
import ColorSchemeRegistry from './ColorSchemeRegistry';

class CategoricalSchemeRegistry extends ColorSchemeRegistry {}

const getInstance = makeSingleton(CategoricalSchemeRegistry);

export default getInstance;
@@ -0,0 +1,15 @@
import { isRequired } from '@superset-ui/core';

export default class ColorScheme {
constructor({
name = isRequired('name'),
label,
colors = isRequired('colors'),
description = '',
}) {
this.name = name;
this.label = label || name;
this.colors = colors;
this.description = description;
}
}
@@ -0,0 +1,46 @@
import { Registry } from '@superset-ui/core';

class ColorSchemeRegistry extends Registry {
clear() {
super.clear();
this.defaultSchemeName = undefined;

return this;
}

getDefaultSchemeName() {
return this.defaultSchemeName;
}

setDefaultSchemeName(schemeName) {
this.defaultSchemeName = schemeName;

return this;
}

get(schemeName) {
return super.get(schemeName || this.defaultSchemeName);
}

registerValue(schemeName, colors) {
super.registerValue(schemeName, colors);
// If there is no default, set as default
if (!this.defaultSchemeName) {
this.defaultSchemeName = schemeName;
}

return this;
}

registerLoader(schemeName, loader) {
super.registerLoader(schemeName, loader);
// If there is no default, set as default
if (!this.defaultSchemeName) {
this.defaultSchemeName = schemeName;
}

return this;
}
}

export default ColorSchemeRegistry;
@@ -0,0 +1,9 @@
import ColorScheme from './ColorScheme';

export default class SequentialScheme extends ColorScheme {
constructor(input) {
super(input);
const { isDiverging = false } = input;
this.isDiverging = isDiverging;
}
}
@@ -0,0 +1,8 @@
import { makeSingleton } from '@superset-ui/core';
import ColorSchemeRegistry from './ColorSchemeRegistry';

class SequentialSchemeRegistry extends ColorSchemeRegistry {}

const getInstance = makeSingleton(SequentialSchemeRegistry);

export default getInstance;
@@ -0,0 +1,34 @@
/* eslint-disable sort-keys */

import CategoricalScheme from '../../CategoricalScheme';

const schemes = [
{
name: 'bnbColors',
colors: [
'#ff5a5f', // rausch
'#7b0051', // hackb
'#007A87', // kazan
'#00d1c1', // babu
'#8ce071', // lima
'#ffb400', // beach
'#b4a76c', // barol
'#ff8083',
'#cc0086',
'#00a1b3',
'#00ffeb',
'#bbedab',
'#ffd266',
'#cbc29a',
'#ff3339',
'#ff1ab1',
'#005c66',
'#00b3a5',
'#55d12e',
'#b37e00',
'#988b4e',
],
},
].map(s => new CategoricalScheme(s));

export default schemes;

0 comments on commit 2490be7

Please sign in to comment.