-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: bugfix for overlapping colors in color palette
- Loading branch information
Showing
33 changed files
with
1,450 additions
and
259 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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 @@ | ||
import 'jest-extended'; |
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,20 @@ | ||
import { baseConfig } from '@iot-app-kit/jest-config'; | ||
|
||
const config = { | ||
...baseConfig, | ||
testEnvironment: 'jsdom', | ||
coverageThreshold: { | ||
/** | ||
* starting low to account for untested code | ||
* since this package wasn't a part of the test command | ||
*/ | ||
global: { | ||
statements: 54, | ||
branches: 50, | ||
functions: 51, | ||
lines: 51, | ||
}, | ||
}, | ||
}; | ||
|
||
export default config; |
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
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,6 +1,6 @@ | ||
export { getSiteWiseClient } from './sdks/sitewise'; | ||
export { getIotEventsClient } from './sdks/events'; | ||
export { colorPalette } from './sdks/colorPalette'; | ||
export { assignDefaultColor } from './sdks/assignDefaultColors'; | ||
export { round } from './sdks/number'; | ||
export { isNumeric } from './sdks/number'; | ||
export { Colorizer } from './sdks/colorizer'; |
This file was deleted.
Oops, something went wrong.
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,101 @@ | ||
import { colorPalette } from './colorPalette'; | ||
import { Colorizer } from './colorizer'; | ||
|
||
it('returns colors in a rotation', () => { | ||
const colorer = Colorizer(['red', 'white', 'blue']); | ||
expect(colorer.next()).toEqual('red'); | ||
expect(colorer.next()).toEqual('white'); | ||
expect(colorer.next()).toEqual('blue'); | ||
}); | ||
|
||
it('applies colors in a rotation to a colorable object', () => { | ||
const colorer = Colorizer(['red', 'white', 'blue']); | ||
expect(colorer.nextApply({})).toEqual({ color: 'red' }); | ||
expect(colorer.nextApply({})).toEqual({ color: 'white' }); | ||
expect(colorer.nextApply({})).toEqual({ color: 'blue' }); | ||
}); | ||
|
||
it('does not repeat colors after 50 rotations', () => { | ||
const colorer = Colorizer(); | ||
const colors: ReturnType<typeof colorer.next>[] = []; | ||
|
||
const testColor = () => { | ||
const color = colorer.next(); | ||
expect(color).toBeDefined(); | ||
expect(colors).not.toContain(color); | ||
colors.push(color); | ||
}; | ||
|
||
for (let i = 0; i < 50; i++) { | ||
testColor(); | ||
} | ||
}); | ||
|
||
it('can add a color(s) at the start of the rotation', () => { | ||
const colorer = Colorizer(); | ||
|
||
const color = colorer.next(); | ||
expect(color).toBeDefined(); | ||
|
||
const nextColor = colorer.next(); | ||
expect(nextColor).not.toEqual(color); | ||
|
||
// Need to check for typescript purposes | ||
if (!color || !nextColor) throw new Error('Should not happen.'); | ||
colorer.add(color); | ||
|
||
expect(colorer.next()).toEqual(color); | ||
|
||
colorer.add([nextColor]); | ||
|
||
expect(colorer.next()).toEqual(nextColor); | ||
}); | ||
|
||
it('can remove a color(s) from the rotation', () => { | ||
const colors = [...colorPalette]; | ||
let colorer = Colorizer(colors); | ||
const thirdColorToRemove = colors.at(2); | ||
|
||
// Need to check for typescript purposes | ||
if (!thirdColorToRemove) throw new Error('Should not happen.'); | ||
|
||
colorer.remove(thirdColorToRemove); | ||
|
||
for (let i = 0; i < colorPalette.length - 1; i++) { | ||
expect(colorer.next()).not.toEqual(thirdColorToRemove); | ||
} | ||
|
||
colorer = Colorizer(colors); | ||
// colors 3 - 5 | ||
const colorsToRemove = colors.slice(2, 5); | ||
|
||
colorer.remove(colorsToRemove); | ||
|
||
for (let i = 0; i < colorPalette.length - 3; i++) { | ||
expect(colorer.next()).not.toEqual(thirdColorToRemove); | ||
} | ||
}); | ||
|
||
it('resets the rotation if all colors are removed', () => { | ||
const palette = ['red', 'white', 'blue']; | ||
const colorer = Colorizer(palette); | ||
colorer.remove(palette); | ||
|
||
expect(colorer.next()).toEqual('red'); | ||
expect(colorer.next()).toEqual('white'); | ||
expect(colorer.next()).toEqual('blue'); | ||
}); | ||
|
||
it('starts the rotation over once exhausted', () => { | ||
const colors = [...colorPalette]; | ||
const colorer = Colorizer(colors); | ||
|
||
const firstColor = colorer.next(); | ||
|
||
for (let i = 0; i < colorPalette.length - 1; i++) { | ||
// exhaust rotation | ||
colorer.next(); | ||
} | ||
|
||
expect(colorer.next()).toEqual(firstColor); | ||
}); |
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,74 @@ | ||
import difference from 'lodash.difference'; | ||
import { colorPalette } from './colorPalette'; | ||
|
||
type Colorable = { color?: string }; | ||
|
||
export const Colorizer = (palette: string[] = colorPalette) => { | ||
let colors = [...palette]; | ||
|
||
/** | ||
* | ||
* Reset the color rotation back to the original state | ||
*/ | ||
const reset = () => { | ||
colors = [...palette]; | ||
}; | ||
|
||
/** | ||
* | ||
* @returns if the color rotation is empty | ||
*/ | ||
const noColors = () => colors.length === 0; | ||
|
||
/** | ||
* | ||
* @param color the color(s) you want to add to the front of the rotation to be used next | ||
*/ | ||
const add = (color: string | string[]) => { | ||
const toAdd = Array.isArray(color) ? color : [color]; | ||
colors = [...toAdd, ...colors]; | ||
}; | ||
|
||
/** | ||
* | ||
* @param color the color(s) you want to remove from the rotation so that they are not used in the rotation | ||
*/ | ||
const remove = (color: string | string[]) => { | ||
const toRemove = Array.isArray(color) ? color : [color]; | ||
colors = difference(colors, toRemove); | ||
if (noColors()) reset(); | ||
}; | ||
|
||
/** | ||
* | ||
* @returns the next color in the rotation | ||
*/ | ||
const next = () => { | ||
if (noColors()) reset(); | ||
|
||
const nextColor = colors.at(0); | ||
colors = colors.slice(1); | ||
|
||
return nextColor; | ||
}; | ||
|
||
/** | ||
* | ||
* @param item an object with a color property | ||
* @returns the object with the color property set to the next color in the rotation | ||
*/ | ||
const nextApply = <T extends Colorable>(item: T): T => { | ||
const color = next(); | ||
return { | ||
...item, | ||
color, | ||
}; | ||
}; | ||
|
||
return { | ||
add, | ||
remove, | ||
next, | ||
nextApply, | ||
}; | ||
}; |
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,5 +1,5 @@ | ||
{ | ||
"extends": "@iot-app-kit/ts-config/base.json", | ||
"include": ["src"], | ||
} | ||
|
||
"files": ["global.d.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
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
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
19 changes: 8 additions & 11 deletions
19
packages/dashboard/src/customization/widgets/utils/assetQuery/applyDefaultStylesToQuery.tsx
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,21 +1,18 @@ | ||
import { assignDefaultColor } from '@iot-app-kit/core-util'; | ||
import type { IoTSiteWiseDataStreamQuery } from '~/types'; | ||
import { StyledAssetQuery } from '../../types'; | ||
import { colorerFromStyledQuery } from './defaultColors'; | ||
|
||
export const applyDefaultStylesToQuery = ({ assets = [], properties = [], assetModels = [] }: StyledAssetQuery) => { | ||
const assignDefaultColor = colorerFromStyledQuery({ assets, assetModels, properties }); | ||
|
||
export const applyDefaultStylesToQuery = ({ | ||
assets = [], | ||
properties = [], | ||
assetModels = [], | ||
}: IoTSiteWiseDataStreamQuery) => { | ||
let offset = 0; | ||
return { | ||
assets: assets.map(({ properties, ...others }) => ({ | ||
...others, | ||
properties: properties.map((propertyQuery) => assignDefaultColor(propertyQuery, offset++)), | ||
properties: properties.map((propertyQuery) => assignDefaultColor(propertyQuery)), | ||
})), | ||
properties: properties.map((property, propertyIndex) => assignDefaultColor(property, propertyIndex + offset++)), | ||
properties: properties.map((property) => assignDefaultColor(property)), | ||
assetModels: assetModels.map(({ properties, ...others }) => ({ | ||
...others, | ||
properties: properties.map((propertyQuery) => assignDefaultColor(propertyQuery, offset++)), | ||
properties: properties.map((propertyQuery) => assignDefaultColor(propertyQuery)), | ||
})), | ||
}; | ||
}; |
Oops, something went wrong.