Skip to content

Commit

Permalink
fix(piechart): workaround colors issue
Browse files Browse the repository at this point in the history
  • Loading branch information
scottdickerson committed Nov 9, 2020
1 parent ab29665 commit 327f486
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 58 deletions.
11 changes: 8 additions & 3 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
"request": "launch",
"name": "Jest All",
"program": "${workspaceFolder}/node_modules/.bin/jest",
"env": { "NODE_ICU_DATA": "node_modules/full-icu", "TZ": "America/Chicago" },
"env": {
"NODE_ICU_DATA": "node_modules/full-icu",
"TZ": "America/Chicago"
},
"args": ["--runInBand"],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
Expand All @@ -33,8 +36,10 @@
"name": "Jest Current File",
"program": "${workspaceFolder}/node_modules/.bin/jest",
"args": ["${relativeFile}", "--config", "jest.config.js"],
"env": { "NODE_ICU_DATA": "node_modules/full-icu", "TZ": "America/Chicago" },
"console": "integratedTerminal",
"env": {
"NODE_ICU_DATA": "node_modules/full-icu",
"TZ": "America/Chicago"
},
"internalConsoleOptions": "neverOpen",
"disableOptimisticBPs": true,
"windows": {
Expand Down
38 changes: 37 additions & 1 deletion src/components/PieChartCard/PieChartCard.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import classNames from 'classnames';
import isEmpty from 'lodash/isEmpty';
import assign from 'lodash/assign';

import { PieCardPropTypes, CardPropTypes } from '../../constants/CardPropTypes';
import {
PieCardPropTypes,
CardPropTypes,
CHART_COLORS,
} from '../../constants/CardPropTypes';
import { CARD_SIZES } from '../../constants/LayoutConstants';
import { settings } from '../../constants/Settings';
import {
Expand Down Expand Up @@ -68,6 +72,37 @@ const generateTableColumns = (values, groupDataSourceId) => {
}));
};

/**
* Formats and maps the colors to their corresponding datasets in the carbon pie chart card expected format
* @param {Array} values, an array of group, value objects for each pie label
* @param {string} groupDataSourceId, the group id to use to find the value
* @param {Object} colors an object of colors
* @returns {Object} colors - formatted
*/
export const formatColors = (values, groupDataSourceId, colors) => {
const formattedColors = {
scale: {},
};
if (Array.isArray(values)) {
values.forEach((value, index) => {
formattedColors.scale[value[groupDataSourceId]] = colors?.[
value[groupDataSourceId]
]
? colors[value[groupDataSourceId]] // look to find a matching color entry in our colors otherwise use defaults
: CHART_COLORS[index % CHART_COLORS.length];
});
} else if (!isEmpty(values)) {
// values is an object
Object.keys(values).forEach((key, index) => {
formattedColors.scale[key] = colors?.[key]
? colors?.[key]
: CHART_COLORS[index % CHART_COLORS.length];
});
}

return formattedColors;
};

const propTypes = { ...CardPropTypes, ...PieCardPropTypes };

const defaultProps = {
Expand Down Expand Up @@ -148,6 +183,7 @@ const PieChartCard = ({
groupMapsTo: groupDataSourceId,
loading: isLoading,
},
color: formatColors(values, groupDataSourceId, colors),
getFillColor: (...args) => getColor(colors, ...args),
getStrokeColor: (...args) => getColor(colors, ...args),
legend: {
Expand Down
129 changes: 75 additions & 54 deletions src/components/PieChartCard/PieChartCard.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,63 +6,84 @@ import { CARD_SIZES } from '../../constants/LayoutConstants';
import Card from '../Card/Card';
import Table from '../Table/Table';

import PieChartCard from './PieChartCard';
import PieChartCard, { formatColors } from './PieChartCard';

describe('PieChartCard', () => {
let chartDataExample;
let pieChartCardProps;

beforeEach(() => {
chartDataExample = [
{
group: '2V2N 9KYPM',
category: 'cat A',
value: 1,
},
{
group: 'L22I P66EP L22I P66EP',
category: 'cat B',
value: 10,
},
{
group: 'JQAI 2M4L1',
category: 'cat C',
value: 20,
},
{
group: 'J9DZ F37AP',
category: 'cat D',
value: 50,
},
{
group: 'YEL48 Q6XK YEL48',
category: 'cat E',
value: 15,
},
{
group: 'Misc',
category: 'cat F',
value: 40,
},
];
const chartDataExample = [
{
group: '2V2N 9KYPM',
category: 'cat A',
value: 1,
},
{
group: 'L22I P66EP L22I P66EP',
category: 'cat B',
value: 10,
},
{
group: 'JQAI 2M4L1',
category: 'cat C',
value: 20,
},
{
group: 'J9DZ F37AP',
category: 'cat D',
value: 50,
},
{
group: 'YEL48 Q6XK YEL48',
category: 'cat E',
value: 15,
},
{
group: 'Misc',
category: 'cat F',
value: 40,
},
];

pieChartCardProps = {
availableActions: { expand: true },
content: {
groupDataSourceId: 'group',
legendPosition: 'bottom',
},
id: 'pie-chart-card',
isLoading: false,
isExpanded: false,
onCardAction: jest.fn(),
size: CARD_SIZES.LARGE,
title: 'Schools',
testID: 'test-pie-chart-card',
values: chartDataExample,
const pieChartCardProps = {
availableActions: { expand: true },
content: {
groupDataSourceId: 'group',
legendPosition: 'bottom',
},
id: 'pie-chart-card',
isLoading: false,
isExpanded: false,
onCardAction: jest.fn(),
size: CARD_SIZES.LARGE,
title: 'Schools',
testID: 'test-pie-chart-card',
values: chartDataExample,
};
describe('utility functions', () => {
it('formatColors with array', () => {
const mockColors = {
'cat A': 'purple',
};
const formattedColors = formatColors(
chartDataExample,
'category',
mockColors
);
expect(formattedColors.scale['cat A']).toEqual('purple');
expect(formattedColors.scale['cat B']).toEqual('#1192e8');
});
it('formatColors with object', () => {
const mockColors = {
'cat A': 'purple',
};
const formattedColors = formatColors(
{ 'cat A': 124, 'cat B': 125 },
undefined,
mockColors
);
expect(formattedColors.scale['cat A']).toEqual('purple');
expect(formattedColors.scale['cat B']).toEqual('#1192e8');
});
});

describe('PieChartCard', () => {
it('shows loading skeleton for isLoading even for empty data ', () => {
const loadingSkeletonQuery = '.iot--pie-chart-container svg.chart-skeleton';
const { container, rerender } = render(
Expand Down Expand Up @@ -156,7 +177,7 @@ describe('PieChartCard', () => {
);

const slices = screen
.getByLabelText('slices')
.getAllByRole('group')[0]
.getElementsByClassName('slice');
const orderedColors = chartDataExample
.sort((a, b) => b.value - a.value)
Expand Down Expand Up @@ -210,7 +231,7 @@ describe('PieChartCard', () => {
expect(screen.getByText('Sample 1')).toBeVisible();

const slices = screen
.getByLabelText('slices')
.getAllByRole('group')[0]
.getElementsByClassName('slice');
const firstSliceColor = slices.item(0).getAttribute('fill');
const secondSliceColor = slices.item(1).getAttribute('fill');
Expand Down

0 comments on commit 327f486

Please sign in to comment.