From 1a2fa2f4c7d16ee087922096fe3dda44d2240c11 Mon Sep 17 00:00:00 2001 From: Aramis Sennyey Date: Fri, 24 Feb 2023 12:56:30 -0500 Subject: [PATCH] Support new option getRingColor to determine text and legend color. Signed-off-by: Aramis Sennyey --- .../src/components/Radar/Radar.test.tsx | 5 ++-- .../tech-radar/src/components/Radar/Radar.tsx | 16 ++++------- .../tech-radar/src/components/Radar/types.ts | 27 +++++++++++++++++++ .../src/components/RadarComponent.tsx | 7 ++++- .../src/components/RadarGrid/RadarGrid.tsx | 8 ++++-- .../components/RadarLegend/RadarLegend.tsx | 2 ++ .../RadarLegend/RadarLegendQuadrant.tsx | 3 +++ .../RadarLegend/RadarLegendRing.tsx | 7 ++++- .../src/components/RadarLegend/types.ts | 3 +++ .../src/components/RadarPlot/RadarPlot.tsx | 7 ++++- 10 files changed, 67 insertions(+), 18 deletions(-) create mode 100644 plugins/tech-radar/src/components/Radar/types.ts diff --git a/plugins/tech-radar/src/components/Radar/Radar.test.tsx b/plugins/tech-radar/src/components/Radar/Radar.test.tsx index b76eb20a16..5260c298fa 100644 --- a/plugins/tech-radar/src/components/Radar/Radar.test.tsx +++ b/plugins/tech-radar/src/components/Radar/Radar.test.tsx @@ -18,9 +18,10 @@ import React from 'react'; import { renderInTestApp } from '@backstage/test-utils'; import GetBBoxPolyfill from '../../utils/polyfills/getBBox'; -import Radar, { Props } from './Radar'; +import Radar from './Radar'; +import type { RadarProps } from './types'; -const minProps: Props = { +const minProps: RadarProps = { width: 500, height: 200, quadrants: [{ id: 'languages', name: 'Languages' }], diff --git a/plugins/tech-radar/src/components/Radar/Radar.tsx b/plugins/tech-radar/src/components/Radar/Radar.tsx index 8ba5af9811..a6dc045325 100644 --- a/plugins/tech-radar/src/components/Radar/Radar.tsx +++ b/plugins/tech-radar/src/components/Radar/Radar.tsx @@ -15,27 +15,20 @@ */ import React, { useMemo, useRef, useState } from 'react'; -import type { Entry, Quadrant, Ring } from '../../utils/types'; +import type { Entry } from '../../utils/types'; import RadarPlot from '../RadarPlot'; +import { RadarProps } from './types'; import { adjustEntries, adjustQuadrants, adjustRings } from './utils'; -export type Props = { - width: number; - height: number; - quadrants: Quadrant[]; - rings: Ring[]; - entries: Entry[]; - svgProps?: object; -}; - const Radar = ({ width, height, quadrants, rings, entries, + getRingColor, ...props -}: Props): JSX.Element => { +}: RadarProps): JSX.Element => { const radius = Math.min(width, height) / 2; // State @@ -66,6 +59,7 @@ const Radar = ({ return ( Color; +}; diff --git a/plugins/tech-radar/src/components/RadarComponent.tsx b/plugins/tech-radar/src/components/RadarComponent.tsx index 1f14e9d258..bf8fdd85da 100644 --- a/plugins/tech-radar/src/components/RadarComponent.tsx +++ b/plugins/tech-radar/src/components/RadarComponent.tsx @@ -20,7 +20,8 @@ import React, { useEffect } from 'react'; import useAsync from 'react-use/lib/useAsync'; import { RadarEntry, techRadarApiRef, TechRadarLoaderResponse } from '../api'; import Radar from '../components/Radar'; -import { Entry } from '../utils/types'; +import { Entry, Ring } from '../utils/types'; +import { RadarProps } from './Radar/types'; const useTechRadarLoader = (id: string | undefined) => { const errorApi = useApi(errorApiRef); @@ -89,6 +90,8 @@ export interface TechRadarComponentProps { * Text to filter {@link RadarEntry} inside Tech Radar */ searchText?: string; + + getRingColor?: RadarProps['getRingColor']; } /** @@ -102,6 +105,7 @@ export interface TechRadarComponentProps { */ export function RadarComponent(props: TechRadarComponentProps) { const { loading, error, value: data } = useTechRadarLoader(props.id); + const { getRingColor = (ring: Ring) => ring.color } = props; const mapToEntries = ( loaderResponse: TechRadarLoaderResponse, @@ -136,6 +140,7 @@ export function RadarComponent(props: TechRadarComponentProps) { {!loading && !error && data && ( (theme => ({ @@ -47,7 +49,7 @@ const useStyles = makeStyles(theme => ({ // A component for the background grid of the radar, with axes, rings etc. It will render around the origin, i.e. // assume that (0, 0) is in the middle of the drawing. const RadarGrid = (props: Props) => { - const { radius, rings } = props; + const { radius, rings, getRingColor } = props; const classes = useStyles(props); const makeRingNode = (ringIndex: number, ringRadius?: number) => [ @@ -63,7 +65,9 @@ const RadarGrid = (props: Props) => { y={ringRadius !== undefined ? -ringRadius + 42 : undefined} textAnchor="middle" className={classes.text} - style={{ fill: rings[ringIndex].color }} + style={{ + fill: getRingColor ? getRingColor(rings[ringIndex]) : undefined, + }} > {rings[ringIndex].name} , diff --git a/plugins/tech-radar/src/components/RadarLegend/RadarLegend.tsx b/plugins/tech-radar/src/components/RadarLegend/RadarLegend.tsx index 38d3757ff8..1db6c52517 100644 --- a/plugins/tech-radar/src/components/RadarLegend/RadarLegend.tsx +++ b/plugins/tech-radar/src/components/RadarLegend/RadarLegend.tsx @@ -88,6 +88,7 @@ const RadarLegend = ({ entries, onEntryMouseEnter, onEntryMouseLeave, + getRingColor, ...props }: RadarLegendProps): JSX.Element => { const classes = useStyles(props); @@ -96,6 +97,7 @@ const RadarLegend = ({ {quadrants.map(quadrant => ( ; onEntryMouseEnter: RadarLegendProps['onEntryMouseEnter']; onEntryMouseLeave: RadarLegendProps['onEntryMouseLeave']; + getRingColor: RadarLegendProps['getRingColor']; }; export const RadarLegendQuadrant = ({ @@ -37,6 +38,7 @@ export const RadarLegendQuadrant = ({ classes, onEntryMouseEnter, onEntryMouseLeave, + getRingColor, }: RadarLegendQuadrantProps) => { return ( ; onEntryMouseEnter?: RadarLegendProps['onEntryMouseEnter']; onEntryMouseLeave?: RadarLegendProps['onEntryMouseEnter']; + getRingColor?: RadarLegendProps['getRingColor']; }; export const RadarLegendRing = ({ @@ -34,10 +35,14 @@ export const RadarLegendRing = ({ classes, onEntryMouseEnter, onEntryMouseLeave, + getRingColor, }: RadarLegendRingProps) => { return (
-

+

{ring.name}

{entries.length === 0 ? ( diff --git a/plugins/tech-radar/src/components/RadarLegend/types.ts b/plugins/tech-radar/src/components/RadarLegend/types.ts index cdac1bfe9d..9974a6592b 100644 --- a/plugins/tech-radar/src/components/RadarLegend/types.ts +++ b/plugins/tech-radar/src/components/RadarLegend/types.ts @@ -15,6 +15,7 @@ */ import { Entry, Quadrant, Ring } from '../../utils/types'; +import { RadarProps } from '../Radar/types'; export type Segments = { [k: number]: { [k: number]: Entry[] }; @@ -26,4 +27,6 @@ export type RadarLegendProps = { entries: Entry[]; onEntryMouseEnter?: (entry: Entry) => void; onEntryMouseLeave?: (entry: Entry) => void; + + getRingColor?: RadarProps['getRingColor']; }; diff --git a/plugins/tech-radar/src/components/RadarPlot/RadarPlot.tsx b/plugins/tech-radar/src/components/RadarPlot/RadarPlot.tsx index d9788be887..a51d1dacd9 100644 --- a/plugins/tech-radar/src/components/RadarPlot/RadarPlot.tsx +++ b/plugins/tech-radar/src/components/RadarPlot/RadarPlot.tsx @@ -22,6 +22,7 @@ import RadarEntry from '../RadarEntry'; import RadarBubble from '../RadarBubble'; import RadarFooter from '../RadarFooter'; import RadarLegend from '../RadarLegend'; +import { RadarProps } from '../Radar/types'; export type Props = { width: number; @@ -33,6 +34,8 @@ export type Props = { activeEntry?: Entry; onEntryMouseEnter?: (entry: Entry) => void; onEntryMouseLeave?: (entry: Entry) => void; + + getRingColor?: RadarProps['getRingColor']; }; // A component that draws the radar circle. @@ -47,6 +50,7 @@ const RadarPlot = (props: Props): JSX.Element => { activeEntry, onEntryMouseEnter, onEntryMouseLeave, + getRingColor, } = props; return ( @@ -61,9 +65,10 @@ const RadarPlot = (props: Props): JSX.Element => { onEntryMouseLeave={ onEntryMouseLeave && (entry => onEntryMouseLeave(entry)) } + getRingColor={getRingColor} /> - + {entries.map(entry => (