From 4831e18e3dffc6f0a64a40df7d8263aa823ba2bc Mon Sep 17 00:00:00 2001 From: Puneet Date: Wed, 8 Jun 2022 11:11:46 +0530 Subject: [PATCH] [Feature] Add: option for normalized shade coloring for cells --- docs/src/App.js | 1 + package.json | 2 +- src/DataStore.js | 49 ++++++++++++++++++++++++++++++++--------- src/ReactCohortGraph.js | 8 ++++--- src/constants.js | 6 ++++- src/index.d.ts | 3 ++- 6 files changed, 53 insertions(+), 16 deletions(-) diff --git a/docs/src/App.js b/docs/src/App.js index 79b993a..789e2bf 100755 --- a/docs/src/App.js +++ b/docs/src/App.js @@ -106,6 +106,7 @@ class App extends Component { scrollableTableContentStyles: PropTypes.object, headerValueStyles: PropTypes.object, headerLabelStyles: PropTypes.object, + isNormalizedShadeColor: PropTypes.bool, }`} diff --git a/package.json b/package.json index 87847e5..ed94b44 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "react-cohort-graph", - "version": "2.0.0-beta.3", + "version": "2.1.0-beta.0", "description": "Cohort Analysis Graph with ReactJS", "main": "lib/index.js", "types": "lib/index.d.ts", diff --git a/src/DataStore.js b/src/DataStore.js index 3e59cf3..dcda3b4 100644 --- a/src/DataStore.js +++ b/src/DataStore.js @@ -80,16 +80,30 @@ export default class DataStore { cellData, ...data[key][anotherKey].map((value, index) => { const percent = this._getPercentage(cellData.total, value); return { - isHeader: false, - index: index, - type: key, - [VALUE]: value, - valueFor: anotherKey, - total: cellData.total, - isTotal: index === 0, - isCell: index > 0, - [PERCENT]: percent, - color: index === 0 ? this.options.bodyCellColor : this._shadeCellWithColor(percent, this.options.shadeColor) + isHeader: false, + index: index, + type: key, + [VALUE]: value, + valueFor: anotherKey, + total: cellData.total, + isTotal: index === 0, + isCell: index > 0, + [PERCENT]: percent, + color: + index === 0 + ? this.options.bodyCellColor + : this.options.isNormalizedShadeColor + ? this._shadeCellWithColor( + this._normalizedValue( + data[key], + value + ), + this.options.shadeColor + ) + : this._shadeCellWithColor( + percent, + this.options.shadeColor + ), }; }) ]); @@ -337,5 +351,20 @@ export default class DataStore { return text.toLowerCase().replace(/\b\w/g, replaced => replaced.toUpperCase()); } } + + /** + * Normalized value of cell for color shade + * @param array + * @param cellValue - cell value + * @returns {number} - normalized cell value + */ + _normalizedValue = (array, value) => { + let joinedArray = []; + Object.entries(array).map((el) => { + joinedArray = [...joinedArray, ...el[1].slice(1)]; + }); + const nv = ((value - Math.min(...joinedArray)) / (Math.max(...joinedArray) - Math.min(...joinedArray))) * 100; + return Math.round(nv); + }; } diff --git a/src/ReactCohortGraph.js b/src/ReactCohortGraph.js index 4ce6864..059a9c4 100644 --- a/src/ReactCohortGraph.js +++ b/src/ReactCohortGraph.js @@ -10,7 +10,7 @@ import { } from './styles'; import DataStore from './DataStore'; import { HeaderCell, BodyCell, ScrollableContent } from './components'; -import { VALUE_KEYS } from './constants'; +import { DEFAULT_VALUES, VALUE_KEYS } from './constants'; import { DEFAULT_SHADE_COLOR, DEFAULT_BODY_CELL_COLOR, DEFAULT_HEADER_CELL_COLOR, DEFAULT_KEY_CELL_COLOR @@ -35,9 +35,10 @@ class ReactCohortGraph extends React.Component { _getStore = (props) => { const { data = {}, shadeColor = DEFAULT_SHADE_COLOR, headerCellColor = DEFAULT_HEADER_CELL_COLOR, - bodyCellColor = DEFAULT_BODY_CELL_COLOR, keyCellColor = DEFAULT_KEY_CELL_COLOR + bodyCellColor = DEFAULT_BODY_CELL_COLOR, keyCellColor = DEFAULT_KEY_CELL_COLOR, + isNormalizedShadeColor = DEFAULT_VALUES.IS_NORMALIZED_SHADE_COLOR, } = props; - return new DataStore(data, {shadeColor, headerCellColor, bodyCellColor, keyCellColor}); + return new DataStore(data, {shadeColor, headerCellColor, bodyCellColor, keyCellColor, isNormalizedShadeColor}); }; componentWillMount(){ @@ -267,6 +268,7 @@ ReactCohortGraph.propTypes = { scrollableTableContentStyles: PropTypes.object, headerValueStyles: PropTypes.object, headerLabelStyles: PropTypes.object, + isNormalizedShadeColor: PropTypes.bool, }; export default ReactCohortGraph; \ No newline at end of file diff --git a/src/constants.js b/src/constants.js index 1f474e4..ddf44f3 100644 --- a/src/constants.js +++ b/src/constants.js @@ -5,4 +5,8 @@ export const VALUE_KEYS = { VALUE: "value", PERCENT: "percent" -}; \ No newline at end of file +}; + +export const DEFAULT_VALUES = { + IS_NORMALIZED_SHADE_COLOR: false, + }; \ No newline at end of file diff --git a/src/index.d.ts b/src/index.d.ts index 7117da4..cddc2ff 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -37,7 +37,8 @@ export interface Props { scrollableTablePartStyles?: object; scrollableTableContentStyles?: object; headerValueStyles?: object; - headerLabelStyles?: object + headerLabelStyles?: object; + isNormalizedShadeColor?: boolean; } declare const ReactCohortGraph: React.ComponentType;