From c0ae43726e569b6a154d95335704ced1105ca0ba Mon Sep 17 00:00:00 2001 From: Usama Ansari Date: Mon, 20 Nov 2023 15:35:03 +0100 Subject: [PATCH] fix: restrict heatmaps to only 2 categorical columns --- src/vis/heatmap/HeatmapGrid.tsx | 78 +++++++++++-------- .../Vis/Heatmap/HeatmapRandom.stories.tsx | 77 +++++++++--------- 2 files changed, 87 insertions(+), 68 deletions(-) diff --git a/src/vis/heatmap/HeatmapGrid.tsx b/src/vis/heatmap/HeatmapGrid.tsx index d482f06df..80fc386b1 100644 --- a/src/vis/heatmap/HeatmapGrid.tsx +++ b/src/vis/heatmap/HeatmapGrid.tsx @@ -1,11 +1,11 @@ -import { Box, Loader, Stack } from '@mantine/core'; +import { /* Box, */ Loader, Stack } from '@mantine/core'; import React, { useMemo } from 'react'; import { useAsync } from '../../hooks/useAsync'; import { InvalidCols } from '../general/InvalidCols'; import { VisColumn } from '../interfaces'; import { Heatmap } from './Heatmap'; import { IHeatmapConfig } from './interfaces'; -import { getHeatmapData, setsOfTwo } from './utils'; +import { getHeatmapData /* , setsOfTwo */ } from './utils'; export function HeatmapGrid({ config, @@ -21,7 +21,10 @@ export function HeatmapGrid({ selected?: { [key: string]: boolean }; }) { const { value: allColumns, status } = useAsync(getHeatmapData, [columns, config.catColumnsSelected, config.aggregateColumn]); - const hasAtLeast2CatCols = useMemo(() => allColumns?.catColumn && allColumns?.catColumn?.length > 1, [allColumns?.catColumn]); + const hasTwoCatCols = useMemo(() => allColumns?.catColumn && allColumns?.catColumn?.length === 2, [allColumns?.catColumn]); + + // NOTE: @dv-usama-ansari: This flag is used when multiple heatmaps are rendered. + // const hasAtLeastTwoCatCols = useMemo(() => allColumns?.catColumn && allColumns?.catColumn?.length > 1, [allColumns?.catColumn]); const margin = useMemo(() => { return { @@ -32,40 +35,53 @@ export function HeatmapGrid({ }; }, []); - const heatmapMultiples = useMemo(() => { - return setsOfTwo(hasAtLeast2CatCols ? allColumns?.catColumn : []) as Awaited>['catColumn'][]; - }, [allColumns?.catColumn, hasAtLeast2CatCols]); + // NOTE: @dv-usama-ansari: This implementation for multiple heatmaps works, but it's not very performant. + // const heatmapMultiples = useMemo(() => { + // return setsOfTwo(hasTwoCatCols ? allColumns?.catColumn : []) as Awaited>['catColumn'][]; + // }, [allColumns?.catColumn, hasTwoCatCols]); return ( {status === 'pending' ? ( - ) : !hasAtLeast2CatCols ? ( - + ) : !hasTwoCatCols ? ( + ) : ( - - {heatmapMultiples.map(([column1, column2]) => ( - - ))} - + + + // NOTE: @dv-usama-ansari: This implementation for multiple heatmaps works, but it's not very performant. + // + // {heatmapMultiples.map(([column1, column2]) => ( + // + // ))} + // )} ); diff --git a/src/vis/stories/Vis/Heatmap/HeatmapRandom.stories.tsx b/src/vis/stories/Vis/Heatmap/HeatmapRandom.stories.tsx index 26d7dad43..d128d5b94 100644 --- a/src/vis/stories/Vis/Heatmap/HeatmapRandom.stories.tsx +++ b/src/vis/stories/Vis/Heatmap/HeatmapRandom.stories.tsx @@ -6,14 +6,16 @@ import { VisProvider } from '../../../Provider'; import { ESortTypes } from '../../../heatmap/interfaces'; import { BaseVisConfig, EAggregateTypes, EColumnTypes, ENumericalColorScaleType, ESupportedPlotlyVis, VisColumn } from '../../../interfaces'; -function RNG(seed) { - const m = 2 ** 35 - 31; - const a = 185852; - let s = seed % m; - return function () { - return (s = (s * a) % m) / m; - }; -} +// NOTE: @dv-usama-ansari: This function is not used anywhere, maybe it can be removed. +// function RNG(seed) { +// const m = 2 ** 35 - 31; +// const a = 185852; +// let s = seed % m; +// return function () { +// return (s = (s * a) % m) / m; +// }; +// } + function fetchData(numberOfPoints: number): VisColumn[] { const norm = d3.randomNormal.source(d3.randomLcg(0.5)); const rng = norm(0.5, 0.3); @@ -162,32 +164,33 @@ Basic.args = { } as BaseVisConfig, }; -export const Multiples: typeof Template = Template.bind({}) as typeof Template; -Multiples.args = { - externalConfig: { - type: ESupportedPlotlyVis.HEATMAP, - catColumnsSelected: [ - { - description: '', - id: 'category', - name: 'category', - }, - { - description: '', - id: 'category2', - name: 'category2', - }, - { - description: '', - id: 'category3', - name: 'category3', - }, - ], - xSortedBy: ESortTypes.CAT_ASC, - ySortedBy: ESortTypes.CAT_ASC, - color: null, - numColorScaleType: ENumericalColorScaleType.SEQUENTIAL, - aggregateColumn: null, - aggregateType: EAggregateTypes.COUNT, - } as BaseVisConfig, -}; +// NOTE: @dv-usama-ansari: This is the implementation for multiple heatmaps, but it's not very performant. +// export const Multiples: typeof Template = Template.bind({}) as typeof Template; +// Multiples.args = { +// externalConfig: { +// type: ESupportedPlotlyVis.HEATMAP, +// catColumnsSelected: [ +// { +// description: '', +// id: 'category', +// name: 'category', +// }, +// { +// description: '', +// id: 'category2', +// name: 'category2', +// }, +// { +// description: '', +// id: 'category3', +// name: 'category3', +// }, +// ], +// xSortedBy: ESortTypes.CAT_ASC, +// ySortedBy: ESortTypes.CAT_ASC, +// color: null, +// numColorScaleType: ENumericalColorScaleType.SEQUENTIAL, +// aggregateColumn: null, +// aggregateType: EAggregateTypes.COUNT, +// } as BaseVisConfig, +// };