From 7782d46a6e8cc4cd74feac9c0c87bdcbe9348866 Mon Sep 17 00:00:00 2001 From: Connor Howington Date: Fri, 4 Nov 2022 17:27:31 -0400 Subject: [PATCH 1/5] Refactor legend to incorporate gradient legend --- .../plotControls/PlotGradientLegend.tsx | 37 +-- src/components/plotControls/PlotLegend.tsx | 270 ++++-------------- .../plotControls/PlotListLegend.tsx | 229 +++++++++++++++ .../plotControls/PlotLegend.stories.tsx | 3 +- 4 files changed, 295 insertions(+), 244 deletions(-) create mode 100644 src/components/plotControls/PlotListLegend.tsx diff --git a/src/components/plotControls/PlotGradientLegend.tsx b/src/components/plotControls/PlotGradientLegend.tsx index 43f10edd..eb3cd032 100755 --- a/src/components/plotControls/PlotGradientLegend.tsx +++ b/src/components/plotControls/PlotGradientLegend.tsx @@ -10,51 +10,32 @@ import { export interface PlotLegendGradientProps { legendMax: number; legendMin: number; - gradientColorscaleType?: string; + gradientColorscaleType?: 'sequential' | 'divergent'; nTicks?: number; // MUST be odd! - legendTitle?: string; showMissingness?: boolean; } export default function PlotGradientLegend({ legendMax, legendMin, gradientColorscaleType, - legendTitle, nTicks, showMissingness, }: PlotLegendGradientProps) { // Declare constants - const legendTextSize = '1.0em'; + // const legendTextSize = '1.0em'; /** Most of below identical to the current custom legend. Hoping that eventually the GradientColorscaleLegend can work within * the nice custom legend that DK created. */ return ( <> { -
-
- {legendTitle != null - ? legendEllipsis(legendTitle, 23) - : legendTitle} -
- - -
+ } ); diff --git a/src/components/plotControls/PlotLegend.tsx b/src/components/plotControls/PlotLegend.tsx index bf0c626d..74c77748 100755 --- a/src/components/plotControls/PlotLegend.tsx +++ b/src/components/plotControls/PlotLegend.tsx @@ -2,61 +2,69 @@ import React from 'react'; import { Checkbox } from '@material-ui/core'; import * as ColorMath from 'color-math'; import { ContainerStylesAddon } from '../../types/plots'; +import PlotListLegend, { PlotListLegendProps } from './PlotListLegend'; +import PlotGradientLegend, { + PlotLegendGradientProps, +} from './PlotGradientLegend'; // define legendItems props -export interface LegendItemsProps { - label: string; - marker: string; - markerColor?: string; - hasData: boolean; - group?: number; - rank?: number; -} +// export interface LegendItemsProps { +// label: string; +// marker: string; +// markerColor?: string; +// hasData: boolean; +// group?: number; +// rank?: number; +// } -// set props for custom legend function -interface PlotLegendProps extends ContainerStylesAddon { - legendItems: LegendItemsProps[]; - checkedLegendItems: string[] | undefined; +interface PlotLegendBaseProps extends ContainerStylesAddon { legendTitle?: string; - onCheckedLegendItemsChange?: (checkedLegendItems: string[]) => void; - // add a condition to show legend for single overlay data - showOverlayLegend?: boolean; } + +// interface PlotLegendGradientProps extends PlotLegendBaseProps { +// legendType: 'colorscale'; +// legendMax: number; +// legendMin: number; +// gradientColorscaleType?: string; +// nTicks?: number; // MUST be odd! +// showMissingness?: boolean; +// } + +// interface PlotLegendListProps extends PlotLegendBaseProps { +// legendType: 'list'; +// legendItems: LegendItemsProps[]; +// checkedLegendItems: string[] | undefined; +// onCheckedLegendItemsChange?: (checkedLegendItems: string[]) => void; +// // add a condition to show legend for single overlay data +// showOverlayLegend?: boolean; +// } + +type PlotLegendProps = PlotLegendBaseProps & + ( + | ({ type: 'list' } & PlotListLegendProps) + | ({ type: 'colorscale' } & PlotLegendGradientProps) + ); + +// set props for custom legend function +// interface PlotLegendProps extends ContainerStylesAddon, PlotLegendPropsUnion { +// legendTitle?: string; +// } + export default function PlotLegend({ - legendItems, - checkedLegendItems, + type, legendTitle, - onCheckedLegendItemsChange, - showOverlayLegend = false, containerStyles, + ...otherProps }: PlotLegendProps) { - // change checkbox state by click - const handleLegendCheckboxClick = (checked: boolean, id: string) => { - if (checkedLegendItems != null) { - if (checked) { - // for vizconfig.checkedLegendItems - if (onCheckedLegendItemsChange != null) - onCheckedLegendItemsChange([...checkedLegendItems, id]); - } else { - // for vizconfig.checkedLegendItems - if (onCheckedLegendItemsChange != null) - onCheckedLegendItemsChange( - checkedLegendItems.filter((el: string) => el !== id) - ); - } - } - }; - - // set some default sizes - const defaultMarkerSize = '0.8em'; const legendTextSize = '1.0em'; - const circleMarkerSize = '0.7em'; - const scatterMarkerSpace = '2em'; return ( <> {/* add a condition to show legend for single overlay data */} - {(legendItems.length > 1 || showOverlayLegend) && ( + {((type === 'list' && + ((otherProps as PlotListLegendProps).legendItems.length > 1 || + (otherProps as PlotListLegendProps).showOverlayLegend)) || + type === 'colorscale') && (
{legendTitle}
-
- {legendItems.map((item: LegendItemsProps, index: number) => ( -
- {/* wrap checkbox with label so that label text is clickable */} -