Skip to content

Commit

Permalink
refactor(chartTypeVariant): ent-4899 consistent types (#927)
Browse files Browse the repository at this point in the history
  • Loading branch information
cdcabrera committed Oct 12, 2022
1 parent 4f3145a commit 2609829
Show file tree
Hide file tree
Showing 14 changed files with 91 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -834,6 +834,11 @@ exports[`ChartHelpers should generate y axis props: props 1`] = `

exports[`ChartHelpers should return specific properties: specific properties 1`] = `
{
"ChartTypeVariant": {
"area": "area",
"line": "line",
"threshold": "threshold",
},
"generateAxisProps": [Function],
"generateDomains": [Function],
"generateElementsProps": [Function],
Expand Down
6 changes: 3 additions & 3 deletions src/components/chart/chart.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { ChartThemeColor } from '@patternfly/react-charts';
import { ChartContext } from './chartContext';
import { ChartElements } from './chartElements';
import { ChartLegend } from './chartLegend';
import { chartHelpers } from './chartHelpers';
import { chartHelpers, ChartTypeVariant } from './chartHelpers';
import { useResizeObserver } from '../../hooks/useWindow';

/**
Expand Down Expand Up @@ -176,7 +176,7 @@ Chart.propTypes = {
})
),
animate: PropTypes.oneOfType([PropTypes.bool, PropTypes.object]),
chartType: PropTypes.oneOf(['area', 'line', 'threshold']),
chartType: PropTypes.oneOf([...Object.values(ChartTypeVariant)]),
fill: PropTypes.string,
stroke: PropTypes.string,
strokeWidth: PropTypes.number,
Expand Down Expand Up @@ -239,4 +239,4 @@ Chart.defaultProps = {
yValueFormat: null
};

export { Chart as default, Chart };
export { Chart as default, Chart, ChartTypeVariant };
10 changes: 6 additions & 4 deletions src/components/chart/chartElements.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Chart, ChartArea, ChartAxis, ChartContainer, ChartLine, ChartThreshold
import { useChartContext } from './chartContext';
import { chartTooltip } from './chartTooltip';
import { chartAxisLabel } from './chartAxisLabel';
import { ChartTypeVariant } from './chartHelpers';

/**
* Generate a compatible Victory chart element/facet component.
Expand Down Expand Up @@ -96,7 +97,8 @@ const ChartElements = ({ chartTypeDefaults }) => {
}

const setChartElement = ({ chartType, props }) => {
const { component: Component, ...defaultProps } = chartTypeDefaults[chartType] || chartTypeDefaults.area;
const { component: Component, ...defaultProps } =
chartTypeDefaults[chartType] || chartTypeDefaults[ChartTypeVariant.area];
return <Component {...{ ...defaultProps, ...props }} />;
};

Expand Down Expand Up @@ -130,23 +132,23 @@ ChartElements.propTypes = {

ChartElements.defaultProps = {
chartTypeDefaults: {
area: {
[ChartTypeVariant.area]: {
component: ChartArea,
animate: {
duration: 250,
onLoad: { duration: 250 }
},
interpolation: 'monotoneX'
},
line: {
[ChartTypeVariant.line]: {
component: ChartLine,
animate: {
duration: 250,
onLoad: { duration: 250 }
},
interpolation: 'monotoneX'
},
threshold: {
[ChartTypeVariant.threshold]: {
component: ChartThreshold,
animate: {
duration: 100,
Expand Down
22 changes: 20 additions & 2 deletions src/components/chart/chartHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@ import React from 'react';
import _cloneDeep from 'lodash/cloneDeep';
import { helpers } from '../../common';

/**
* Available chart types
*
* @type {{area: string, line: string, threshold: string}}
*/
const ChartTypeVariant = {
area: 'area',
line: 'line',
threshold: 'threshold'
};

/**
* Generate max X and Y values from datasets.
*
Expand Down Expand Up @@ -92,9 +103,14 @@ const generateDomains = ({ maxY, padding = {} } = {}) => {
* @param {number} params.maxY
* @param {Function} params.xValueFormat
* @param {Function} params.yValueFormat
* @param {object} options
* @param {object} options.chartTypeVariant
* @returns {{elementsById: object, stackedElements: Array, stackedElementsById: object, elements: Array}}
*/
const generateElementsProps = ({ dataSets = [], maxX, maxY, xValueFormat, yValueFormat }) => {
const generateElementsProps = (
{ dataSets = [], maxX, maxY, xValueFormat, yValueFormat } = {},
{ chartTypeVariant = ChartTypeVariant } = {}
) => {
const elements = [];
const stackedElements = [];
const elementsById = {};
Expand All @@ -109,7 +125,7 @@ const generateElementsProps = ({ dataSets = [], maxX, maxY, xValueFormat, yValue
data: {}
};

if (fill && chartType !== 'line' && chartType !== 'threshold') {
if (fill && chartType !== chartTypeVariant.line && chartType !== chartTypeVariant.threshold) {
dataColorStroke.data.fill = fill;
}

Expand Down Expand Up @@ -422,6 +438,7 @@ const generateAxisProps = ({
};

const chartHelpers = {
ChartTypeVariant,
generateAxisProps,
generateDomains,
generateElementsProps,
Expand All @@ -434,6 +451,7 @@ const chartHelpers = {
export {
chartHelpers as default,
chartHelpers,
ChartTypeVariant,
generateAxisProps,
generateDomains,
generateElementsProps,
Expand Down
19 changes: 10 additions & 9 deletions src/components/graphCard/__tests__/graphCardChartTooltip.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from 'react';
import { shallow } from 'enzyme';
import { GraphCardChartTooltip } from '../graphCardChartTooltip';
import { ChartTypeVariant } from '../../chart/chart';
import {
RHSM_API_QUERY_GRANULARITY_TYPES as GRANULARITY_TYPES,
RHSM_API_QUERY_SET_TYPES
Expand Down Expand Up @@ -134,23 +135,23 @@ describe('GraphCardChartTooltip Component', () => {

// threshold value > 0
itemsByKey.threshold = {
chartType: 'threshold',
chartType: ChartTypeVariant.threshold,
data: { x: 0, y: 100, date: '2019-06-01T00:00:00Z', hasInfinite: undefined }
};
expect({ daily: daily(), weekly: weekly(), monthly: monthly(), quarterly: quarterly() }).toMatchSnapshot(
'threshold display for > 0 and UNDEFINED hasInfinite on threshold'
);

itemsByKey.threshold = {
chartType: 'threshold',
chartType: ChartTypeVariant.threshold,
data: { x: 0, y: 100, date: '2019-06-01T00:00:00Z', hasInfinite: false }
};
expect({ daily: daily(), weekly: weekly(), monthly: monthly(), quarterly: quarterly() }).toMatchSnapshot(
'threshold display for > 0 and NOT hasInfinite on threshold'
);

itemsByKey.threshold = {
chartType: 'threshold',
chartType: ChartTypeVariant.threshold,
data: { x: 0, y: 100, date: '2019-06-01T00:00:00Z', hasInfinite: true }
};
expect({ daily: daily(), weekly: weekly(), monthly: monthly(), quarterly: quarterly() }).toMatchSnapshot(
Expand All @@ -159,23 +160,23 @@ describe('GraphCardChartTooltip Component', () => {

// threshold value = 0
itemsByKey.threshold = {
chartType: 'threshold',
chartType: ChartTypeVariant.threshold,
data: { x: 0, y: 0, date: '2019-06-01T00:00:00Z', hasInfinite: undefined }
};
expect({ daily: daily(), weekly: weekly(), monthly: monthly(), quarterly: quarterly() }).toMatchSnapshot(
'threshold display for zero and UNDEFINED hasInfinite on threshold'
);

itemsByKey.threshold = {
chartType: 'threshold',
chartType: ChartTypeVariant.threshold,
data: { x: 0, y: 0, date: '2019-06-01T00:00:00Z', hasInfinite: false }
};
expect({ daily: daily(), weekly: weekly(), monthly: monthly(), quarterly: quarterly() }).toMatchSnapshot(
'threshold display for zero and NOT hasInfinite on threshold'
);

itemsByKey.threshold = {
chartType: 'threshold',
chartType: ChartTypeVariant.threshold,
data: { x: 0, y: 0, date: '2019-06-01T00:00:00Z', hasInfinite: true }
};
expect({ daily: daily(), weekly: weekly(), monthly: monthly(), quarterly: quarterly() }).toMatchSnapshot(
Expand All @@ -184,23 +185,23 @@ describe('GraphCardChartTooltip Component', () => {

// threshold value = null
itemsByKey.threshold = {
chartType: 'threshold',
chartType: ChartTypeVariant.threshold,
data: { x: 0, y: null, date: '2019-06-01T00:00:00Z', hasInfinite: undefined }
};
expect({ daily: daily(), weekly: weekly(), monthly: monthly(), quarterly: quarterly() }).toMatchSnapshot(
'threshold display for null and UNDEFINED hasInfinite on threshold'
);

itemsByKey.threshold = {
chartType: 'threshold',
chartType: ChartTypeVariant.threshold,
data: { x: 0, y: null, date: '2019-06-01T00:00:00Z', hasInfinite: false }
};
expect({ daily: daily(), weekly: weekly(), monthly: monthly(), quarterly: quarterly() }).toMatchSnapshot(
'threshold display for null and NOT hasInfinite on threshold'
);

itemsByKey.threshold = {
chartType: 'threshold',
chartType: ChartTypeVariant.threshold,
data: { x: 0, y: null, date: '2019-06-01T00:00:00Z', hasInfinite: true }
};
expect({ daily: daily(), weekly: weekly(), monthly: monthly(), quarterly: quarterly() }).toMatchSnapshot(
Expand Down
7 changes: 4 additions & 3 deletions src/components/graphCard/graphCardChartTooltip.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import PropTypes from 'prop-types';
import { useProduct, useProductGraphTallyQuery } from '../productView/productViewContext';
import { getTooltipDate } from './graphCardHelpers';
import { translate } from '../i18n/i18n';
import { ChartTypeVariant } from '../chart/chart';
import { ChartIcon } from '../chart/chartIcon';
import { RHSM_API_QUERY_SET_TYPES } from '../../services/rhsm/rhsmConstants';
import { helpers } from '../../common';
Expand Down Expand Up @@ -43,7 +44,7 @@ const GraphCardChartTooltip = ({
color: itemsByKey[key]?.color
};

if (itemsByKey[key]?.chartType === 'threshold') {
if (itemsByKey[key]?.chartType === ChartTypeVariant.threshold) {
let thresholdStringValue = itemsByKey[key]?.data.y ?? t('curiosity-graph.label_noData');

if (itemsByKey[key]?.data.hasInfinite) {
Expand Down Expand Up @@ -102,10 +103,10 @@ const GraphCardChartTooltip = ({
return (
<tr key={`tooltip-${dataFacet.label}`}>
<th>
{dataFacet.chartType === 'threshold' && (
{dataFacet.chartType === ChartTypeVariant.threshold && (
<ChartIcon size="sm" symbol="dash" fill={dataFacet.color || 'transparent'} />
)}
{dataFacet.chartType !== 'threshold' && (
{dataFacet.chartType !== ChartTypeVariant.threshold && (
<ChartIcon size="sm" fill={dataFacet.color || 'transparent'} />
)}{' '}
{dataFacet.label}
Expand Down
3 changes: 2 additions & 1 deletion src/components/graphCard/graphCardHelpers.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import moment from 'moment';
import { chart_color_green_300 as chartColorGreenDark } from '@patternfly/react-tokens';
import { ChartTypeVariant } from '../chart/chart';
import { RHSM_API_QUERY_GRANULARITY_TYPES as GRANULARITY_TYPES } from '../../types/rhsmApiTypes';
import { dateHelpers, helpers } from '../../common';

Expand All @@ -19,7 +20,7 @@ const generateChartSettings = (filters = [], graphCardSettings = {}) => {
return;
}

const isThreshold = filterSettings?.chartType === 'threshold';
const isThreshold = filterSettings?.chartType === ChartTypeVariant.threshold;
const baseFilterSettings = {
id,
isStacked: !isThreshold,
Expand Down
5 changes: 3 additions & 2 deletions src/config/product.openshiftContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
} from '../services/rhsm/rhsmConstants';
import { dateHelpers, helpers } from '../common';
import { Tooltip } from '../components/tooltip/tooltip';
import { ChartTypeVariant } from '../components/chart/chart';
import { ChartIcon } from '../components/chart/chartIcon';
import { translate } from '../components/i18n/i18n';

Expand Down Expand Up @@ -75,8 +76,8 @@ const config = {
stroke: chartColorBlueDark.value,
color: chartColorBlueDark.value
},
{ id: 'thresholdSockets', chartType: 'threshold', isOptional: true },
{ id: 'thresholdCores', chartType: 'threshold', isOptional: true }
{ id: 'thresholdSockets', chartType: ChartTypeVariant.threshold, isOptional: true },
{ id: 'thresholdCores', chartType: ChartTypeVariant.threshold, isOptional: true }
],
initialGraphSettings: {},
initialGuestsFilters: [
Expand Down
5 changes: 3 additions & 2 deletions src/config/product.openshiftDedicated.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
RHSM_API_PATH_ID_TYPES
} from '../types/rhsmApiTypes';
import { RHSM_INTERNAL_PRODUCT_DISPLAY_TYPES as DISPLAY_TYPES } from '../services/rhsm/rhsmConstants';
import { ChartTypeVariant } from '../components/chart/chart';
import { dateHelpers, helpers } from '../common';
import { translate } from '../components/i18n/i18n';

Expand Down Expand Up @@ -49,7 +50,7 @@ const config = {
fill: chartColorBlueLight.value,
stroke: chartColorBlueDark.value,
color: chartColorBlueDark.value,
chartType: 'line',
chartType: ChartTypeVariant.line,
isStacked: false,
yAxisUseDataSet: true
},
Expand All @@ -58,7 +59,7 @@ const config = {
fill: chartColorCyanLight.value,
stroke: chartColorCyanDark.value,
color: chartColorCyanDark.value,
chartType: 'line',
chartType: ChartTypeVariant.line,
isStacked: false,
yAxisUseDataSet: true
}
Expand Down
3 changes: 2 additions & 1 deletion src/config/product.rhacs.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
RHSM_API_PATH_METRIC_TYPES,
RHSM_INTERNAL_PRODUCT_DISPLAY_TYPES as DISPLAY_TYPES
} from '../services/rhsm/rhsmConstants';
import { ChartTypeVariant } from '../components/chart/chart';
import { dateHelpers, helpers } from '../common';
import { translate, EMPTY_CONTEXT } from '../components/i18n/i18n';

Expand Down Expand Up @@ -64,7 +65,7 @@ const config = {
fill: chartColorBlueLight.value,
stroke: chartColorBlueDark.value,
color: chartColorBlueDark.value,
chartType: 'line',
chartType: ChartTypeVariant.line,
isStacked: false,
isStandalone: true,
yAxisChartLabel: ({ id }) => translate('curiosity-graph.label_axisY', { context: id })
Expand Down
3 changes: 2 additions & 1 deletion src/config/product.rhel.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
import { dateHelpers, helpers } from '../common';
import { Tooltip } from '../components/tooltip/tooltip';
import { ChartIcon } from '../components/chart/chartIcon';
import { ChartTypeVariant } from '../components/chart/chart';
import { translate } from '../components/i18n/i18n';

/**
Expand Down Expand Up @@ -84,7 +85,7 @@ const config = {
stroke: chartColorPurpleDark.value,
color: chartColorPurpleDark.value
},
{ id: 'thresholdSockets', chartType: 'threshold' }
{ id: 'thresholdSockets', chartType: ChartTypeVariant.threshold }
],
initialGuestsFilters: [
{
Expand Down
3 changes: 2 additions & 1 deletion src/config/product.rhods.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
RHSM_API_PATH_METRIC_TYPES,
RHSM_INTERNAL_PRODUCT_DISPLAY_TYPES as DISPLAY_TYPES
} from '../services/rhsm/rhsmConstants';
import { ChartTypeVariant } from '../components/chart/chart';
import { dateHelpers, helpers } from '../common';
import { translate, EMPTY_CONTEXT } from '../components/i18n/i18n';

Expand Down Expand Up @@ -59,7 +60,7 @@ const config = {
fill: chartColorBlueLight.value,
stroke: chartColorBlueDark.value,
color: chartColorBlueDark.value,
chartType: 'line',
chartType: ChartTypeVariant.line,
isStacked: false,
isStandalone: true,
yAxisChartLabel: ({ id }) => translate('curiosity-graph.label_axisY', { context: id })
Expand Down
Loading

0 comments on commit 2609829

Please sign in to comment.