From 019fbeb8303860023a4e1e38f45f46d1666152fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Cort=C3=A9s?= Date: Mon, 26 Feb 2024 10:59:48 -0600 Subject: [PATCH] frontend: add and use charts colors (#2926) This PR adds the color palette to be used in charts and updates current usages with references to the theme. A new property was added to the theme object: `chartColors`, to hold an static array of colors. Added default colors to the `theme.colors` object for the `Pie` and `LinearTimeline` components, `TimeseriesChart` does not use default colors, but their stories now use colors from `theme`. Changed components: ### Linear Timeline before image after image Custom styled before image after image ### Pie Chart Chart now uses colors from theme Changed labels to get colors from palette before image after image ### TimeseriesChart before image after image before image after image ### Testing Performed manual, unit tests --- .../core/src/Charts/linearTimeline.tsx | 18 ++++- frontend/packages/core/src/Charts/pie.tsx | 45 +++++------ .../Charts/stories/linearTimeline.stories.tsx | 41 +++++++--- .../src/Charts/stories/timeseries.stories.tsx | 30 +++++--- .../core/src/Charts/tests/pie.test.tsx | 8 +- frontend/packages/core/src/Theme/colors.tsx | 74 +++++++++++++++++-- frontend/packages/core/src/Theme/types.tsx | 18 +++++ 7 files changed, 178 insertions(+), 56 deletions(-) diff --git a/frontend/packages/core/src/Charts/linearTimeline.tsx b/frontend/packages/core/src/Charts/linearTimeline.tsx index 0e6e1cd30a..43aee97ed8 100644 --- a/frontend/packages/core/src/Charts/linearTimeline.tsx +++ b/frontend/packages/core/src/Charts/linearTimeline.tsx @@ -10,6 +10,8 @@ import { YAxis, } from "recharts"; +import { useTheme } from "../AppProvider/themes"; + import { calculateDomainEdges, calculateTicks, localTimeFormatter } from "./helpers"; import type { CustomTooltipProps, LinearTimelineData, LinearTimelineStylingProps } from "./types"; @@ -46,6 +48,8 @@ const LinearTimeline = ({ tooltipFormatterFunc = null, stylingProps = {}, }: LinearTimelineProps) => { + const theme = useTheme(); + const combinedData = Object.keys(data).reduce((acc, lane) => { return [...acc, ...data[lane].points]; }, []); @@ -86,8 +90,11 @@ const LinearTimeline = ({ return (
{localTimeFormatter(payload[0].value)} @@ -102,8 +109,11 @@ const LinearTimeline = ({ ({ + fill: theme.colors.charts.pie.labelPrimary, +})); + +const ChartLabelSecondary = styled("text")(({ theme }: { theme: Theme }) => ({ + fill: theme.colors.charts.pie.labelSecondary, +})); const renderActiveShape = (props, options) => { const RADIAN = Math.PI / 180; @@ -161,12 +156,12 @@ const renderActiveShape = (props, options) => { /> - = 0 ? 1 : -1) * 12} y={ey} textAnchor={textAnchor} fill="#333"> + = 0 ? 1 : -1) * 12} y={ey} textAnchor={textAnchor}> {payload.name} - - = 0 ? 1 : -1) * 12} y={ey} dy={18} textAnchor={textAnchor} fill="#999"> + + = 0 ? 1 : -1) * 12} y={ey} dy={18} textAnchor={textAnchor}> {`${value} (${(percent * 100).toFixed(2)}%)`} - + ); }; @@ -219,6 +214,8 @@ class PieChart extends PureComponent { tooltip, } = this.props; + const { colors } = this.context; + const chartOptions = { activeTooltip: typeof activeTooltip === "boolean" ? activeTooltip : true, activeTooltipOptions: typeof activeTooltip !== "boolean" ? { ...activeTooltip } : {}, @@ -270,7 +267,7 @@ class PieChart extends PureComponent { > { ))} {centerLabel &&