Skip to content

Commit

Permalink
fix(plugin-chart-pivot-table): color weight of Conditional formatting…
Browse files Browse the repository at this point in the history
… metrics not work (apache#20396)

* fix(plugin-chart-pivot-table): color weight of Conditional formatting metrics not work

* fix: test
  • Loading branch information
stephenLYZ authored Jun 16, 2022
1 parent ccba5b2 commit 1665403
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* under the License.
*/
import memoizeOne from 'memoize-one';
import { DataRecord } from '@superset-ui/core';
import { addAlpha, DataRecord } from '@superset-ui/core';
import {
ColorFormatters,
COMPARATOR,
Expand All @@ -28,9 +28,6 @@ import {
export const round = (num: number, precision = 0) =>
Number(`${Math.round(Number(`${num}e+${precision}`))}e-${precision}`);

export const rgbToRgba = (rgb: string, alpha: number) =>
rgb.replace(/rgb/i, 'rgba').replace(/\)/i, `,${alpha})`);

const MIN_OPACITY_BOUNDED = 0.05;
const MIN_OPACITY_UNBOUNDED = 0;
const MAX_OPACITY = 1;
Expand Down Expand Up @@ -174,7 +171,7 @@ export const getColorFunction = (
const compareResult = comparatorFunction(value, columnValues);
if (compareResult === false) return undefined;
const { cutoffValue, extremeValue } = compareResult;
return rgbToRgba(
return addAlpha(
colorScheme,
getOpacity(value, cutoffValue, extremeValue, minOpacity, maxOpacity),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import { configure } from '@superset-ui/core';
import {
COMPARATOR,
getOpacity,
rgbToRgba,
round,
getColorFormatters,
getColorFunction,
Expand Down Expand Up @@ -54,53 +53,47 @@ describe('getOpacity', () => {
});
});

describe('rgba', () => {
it('returns correct rgba value', () => {
expect(rgbToRgba('rgb(255,0,0)', 0.5)).toEqual('rgba(255,0,0,0.5)');
});
});

describe('getColorFunction()', () => {
it('getColorFunction GREATER_THAN', () => {
const colorFunction = getColorFunction(
{
operator: COMPARATOR.GREATER_THAN,
targetValue: 50,
colorScheme: 'rgb(255,0,0)',
colorScheme: '#FF0000',
column: 'count',
},
countValues,
);
expect(colorFunction(50)).toBeUndefined();
expect(colorFunction(100)).toEqual('rgba(255,0,0,1)');
expect(colorFunction(100)).toEqual('#FF0000FF');
});

it('getColorFunction LESS_THAN', () => {
const colorFunction = getColorFunction(
{
operator: COMPARATOR.LESS_THAN,
targetValue: 100,
colorScheme: 'rgb(255,0,0)',
colorScheme: '#FF0000',
column: 'count',
},
countValues,
);
expect(colorFunction(100)).toBeUndefined();
expect(colorFunction(50)).toEqual('rgba(255,0,0,1)');
expect(colorFunction(50)).toEqual('#FF0000FF');
});

it('getColorFunction GREATER_OR_EQUAL', () => {
const colorFunction = getColorFunction(
{
operator: COMPARATOR.GREATER_OR_EQUAL,
targetValue: 50,
colorScheme: 'rgb(255,0,0)',
colorScheme: '#FF0000',
column: 'count',
},
countValues,
);
expect(colorFunction(50)).toEqual('rgba(255,0,0,0.05)');
expect(colorFunction(100)).toEqual('rgba(255,0,0,1)');
expect(colorFunction(50)).toEqual('#FF00000D');
expect(colorFunction(100)).toEqual('#FF0000FF');
expect(colorFunction(0)).toBeUndefined();
});

Expand All @@ -109,13 +102,13 @@ describe('getColorFunction()', () => {
{
operator: COMPARATOR.LESS_OR_EQUAL,
targetValue: 100,
colorScheme: 'rgb(255,0,0)',
colorScheme: '#FF0000',
column: 'count',
},
countValues,
);
expect(colorFunction(50)).toEqual('rgba(255,0,0,1)');
expect(colorFunction(100)).toEqual('rgba(255,0,0,0.05)');
expect(colorFunction(50)).toEqual('#FF0000FF');
expect(colorFunction(100)).toEqual('#FF00000D');
expect(colorFunction(150)).toBeUndefined();
});

Expand All @@ -124,41 +117,41 @@ describe('getColorFunction()', () => {
{
operator: COMPARATOR.EQUAL,
targetValue: 100,
colorScheme: 'rgb(255,0,0)',
colorScheme: '#FF0000',
column: 'count',
},
countValues,
);
expect(colorFunction(50)).toBeUndefined();
expect(colorFunction(100)).toEqual('rgba(255,0,0,1)');
expect(colorFunction(100)).toEqual('#FF0000FF');
});

it('getColorFunction NOT_EQUAL', () => {
let colorFunction = getColorFunction(
{
operator: COMPARATOR.NOT_EQUAL,
targetValue: 60,
colorScheme: 'rgb(255,0,0)',
colorScheme: '#FF0000',
column: 'count',
},
countValues,
);
expect(colorFunction(60)).toBeUndefined();
expect(colorFunction(100)).toEqual('rgba(255,0,0,1)');
expect(colorFunction(50)).toEqual('rgba(255,0,0,0.29)');
expect(colorFunction(100)).toEqual('#FF0000FF');
expect(colorFunction(50)).toEqual('#FF00004A');

colorFunction = getColorFunction(
{
operator: COMPARATOR.NOT_EQUAL,
targetValue: 90,
colorScheme: 'rgb(255,0,0)',
colorScheme: '#FF0000',
column: 'count',
},
countValues,
);
expect(colorFunction(90)).toBeUndefined();
expect(colorFunction(100)).toEqual('rgba(255,0,0,0.29)');
expect(colorFunction(50)).toEqual('rgba(255,0,0,1)');
expect(colorFunction(100)).toEqual('#FF00004A');
expect(colorFunction(50)).toEqual('#FF0000FF');
});

it('getColorFunction BETWEEN', () => {
Expand All @@ -167,13 +160,13 @@ describe('getColorFunction()', () => {
operator: COMPARATOR.BETWEEN,
targetValueLeft: 75,
targetValueRight: 125,
colorScheme: 'rgb(255,0,0)',
colorScheme: '#FF0000',
column: 'count',
},
countValues,
);
expect(colorFunction(50)).toBeUndefined();
expect(colorFunction(100)).toEqual('rgba(255,0,0,0.53)');
expect(colorFunction(100)).toEqual('#FF000087');
});

it('getColorFunction BETWEEN_OR_EQUAL', () => {
Expand All @@ -182,13 +175,13 @@ describe('getColorFunction()', () => {
operator: COMPARATOR.BETWEEN_OR_EQUAL,
targetValueLeft: 50,
targetValueRight: 100,
colorScheme: 'rgb(255,0,0)',
colorScheme: '#FF0000',
column: 'count',
},
countValues,
);
expect(colorFunction(50)).toEqual('rgba(255,0,0,0.05)');
expect(colorFunction(100)).toEqual('rgba(255,0,0,1)');
expect(colorFunction(50)).toEqual('#FF00000D');
expect(colorFunction(100)).toEqual('#FF0000FF');
expect(colorFunction(150)).toBeUndefined();
});

Expand All @@ -198,12 +191,12 @@ describe('getColorFunction()', () => {
operator: COMPARATOR.BETWEEN_OR_LEFT_EQUAL,
targetValueLeft: 50,
targetValueRight: 100,
colorScheme: 'rgb(255,0,0)',
colorScheme: '#FF0000',
column: 'count',
},
countValues,
);
expect(colorFunction(50)).toEqual('rgba(255,0,0,0.05)');
expect(colorFunction(50)).toEqual('#FF00000D');
expect(colorFunction(100)).toBeUndefined();
});

Expand All @@ -213,21 +206,21 @@ describe('getColorFunction()', () => {
operator: COMPARATOR.BETWEEN_OR_RIGHT_EQUAL,
targetValueLeft: 50,
targetValueRight: 100,
colorScheme: 'rgb(255,0,0)',
colorScheme: '#FF0000',
column: 'count',
},
countValues,
);
expect(colorFunction(50)).toBeUndefined();
expect(colorFunction(100)).toEqual('rgba(255,0,0,1)');
expect(colorFunction(100)).toEqual('#FF0000FF');
});

it('getColorFunction GREATER_THAN with target value undefined', () => {
const colorFunction = getColorFunction(
{
operator: COMPARATOR.GREATER_THAN,
targetValue: undefined,
colorScheme: 'rgb(255,0,0)',
colorScheme: '#FF0000',
column: 'count',
},
countValues,
Expand All @@ -242,7 +235,7 @@ describe('getColorFunction()', () => {
operator: COMPARATOR.BETWEEN,
targetValueLeft: undefined,
targetValueRight: 100,
colorScheme: 'rgb(255,0,0)',
colorScheme: '#FF0000',
column: 'count',
},
countValues,
Expand All @@ -257,7 +250,7 @@ describe('getColorFunction()', () => {
operator: COMPARATOR.BETWEEN,
targetValueLeft: 50,
targetValueRight: undefined,
colorScheme: 'rgb(255,0,0)',
colorScheme: '#FF0000',
column: 'count',
},
countValues,
Expand All @@ -272,7 +265,7 @@ describe('getColorFunction()', () => {
// @ts-ignore
operator: 'unsupported operator',
targetValue: 50,
colorScheme: 'rgb(255,0,0)',
colorScheme: '#FF0000',
column: 'count',
},
countValues,
Expand All @@ -285,15 +278,15 @@ describe('getColorFunction()', () => {
const colorFunction = getColorFunction(
{
operator: COMPARATOR.NONE,
colorScheme: 'rgb(255,0,0)',
colorScheme: '#FF0000',
column: 'count',
},
countValues,
);
expect(colorFunction(20)).toEqual(undefined);
expect(colorFunction(50)).toEqual('rgba(255,0,0,0)');
expect(colorFunction(75)).toEqual('rgba(255,0,0,0.5)');
expect(colorFunction(100)).toEqual('rgba(255,0,0,1)');
expect(colorFunction(50)).toEqual('#FF000000');
expect(colorFunction(75)).toEqual('#FF000080');
expect(colorFunction(100)).toEqual('#FF0000FF');
expect(colorFunction(120)).toEqual(undefined);
});

Expand All @@ -302,7 +295,7 @@ describe('getColorFunction()', () => {
{
operator: undefined,
targetValue: 150,
colorScheme: 'rgb(255,0,0)',
colorScheme: '#FF0000',
column: 'count',
},
countValues,
Expand Down Expand Up @@ -332,47 +325,41 @@ describe('getColorFormatters()', () => {
{
operator: COMPARATOR.GREATER_THAN,
targetValue: 50,
colorScheme: 'rgb(255,0,0)',
colorScheme: '#FF0000',
column: 'count',
},
{
operator: COMPARATOR.LESS_THAN,
targetValue: 300,
colorScheme: 'rgb(255,0,0)',
colorScheme: '#FF0000',
column: 'sum',
},
{
operator: COMPARATOR.BETWEEN,
targetValueLeft: 75,
targetValueRight: 125,
colorScheme: 'rgb(255,0,0)',
colorScheme: '#FF0000',
column: 'count',
},
{
operator: COMPARATOR.GREATER_THAN,
targetValue: 150,
colorScheme: 'rgb(255,0,0)',
colorScheme: '#FF0000',
column: undefined,
},
];
const colorFormatters = getColorFormatters(columnConfig, mockData);
expect(colorFormatters.length).toEqual(3);

expect(colorFormatters[0].column).toEqual('count');
expect(colorFormatters[0].getColorFromValue(100)).toEqual(
'rgba(255,0,0,1)',
);
expect(colorFormatters[0].getColorFromValue(100)).toEqual('#FF0000FF');

expect(colorFormatters[1].column).toEqual('sum');
expect(colorFormatters[1].getColorFromValue(200)).toEqual(
'rgba(255,0,0,1)',
);
expect(colorFormatters[1].getColorFromValue(200)).toEqual('#FF0000FF');
expect(colorFormatters[1].getColorFromValue(400)).toBeUndefined();

expect(colorFormatters[2].column).toEqual('count');
expect(colorFormatters[2].getColorFromValue(100)).toEqual(
'rgba(255,0,0,0.53)',
);
expect(colorFormatters[2].getColorFromValue(100)).toEqual('#FF000087');
});

it('undefined column config', () => {
Expand Down

0 comments on commit 1665403

Please sign in to comment.