From 708bcda76a0c0b7f6da3d21d80e2ed592b3d828d Mon Sep 17 00:00:00 2001 From: Domas Date: Fri, 10 May 2024 21:27:09 +0300 Subject: [PATCH] VizLegend: Represent line style in series legend and tooltip (#87558) * wip * aand finished * remove console log * use css for styling * user boerder for solid as well * misc fixes * attempt css based approach --------- Co-authored-by: Leon Sorokin --- .../src/components/VizLegend/SeriesIcon.tsx | 50 +++++++++++++------ .../VizLegend/VizLegendListItem.tsx | 1 + .../VizLegend/VizLegendSeriesIcon.tsx | 8 ++- .../VizLegend/VizLegendTableItem.tsx | 7 ++- .../src/components/VizLegend/types.ts | 3 +- .../VizTooltip/VizTooltipColorIndicator.tsx | 20 +++++--- .../VizTooltip/VizTooltipContent.tsx | 3 +- .../components/VizTooltip/VizTooltipRow.tsx | 5 +- .../src/components/VizTooltip/types.ts | 3 ++ .../src/components/VizTooltip/utils.ts | 3 +- .../src/components/uPlot/PlotLegend.tsx | 1 + 11 files changed, 76 insertions(+), 28 deletions(-) diff --git a/packages/grafana-ui/src/components/VizLegend/SeriesIcon.tsx b/packages/grafana-ui/src/components/VizLegend/SeriesIcon.tsx index c658fe0f072..981bad51eb6 100644 --- a/packages/grafana-ui/src/components/VizLegend/SeriesIcon.tsx +++ b/packages/grafana-ui/src/components/VizLegend/SeriesIcon.tsx @@ -1,19 +1,21 @@ import { css, cx } from '@emotion/css'; import React, { CSSProperties } from 'react'; -import { fieldColorModeRegistry } from '@grafana/data'; +import { GrafanaTheme2, fieldColorModeRegistry } from '@grafana/data'; +import { LineStyle } from '@grafana/schema'; import { useTheme2, useStyles2 } from '../../themes'; export interface Props extends React.HTMLAttributes { color?: string; gradient?: string; + lineStyle?: LineStyle; } export const SeriesIcon = React.memo( - React.forwardRef(({ color, className, gradient, ...restProps }, ref) => { + React.forwardRef(({ color, className, gradient, lineStyle, ...restProps }, ref) => { const theme = useTheme2(); - const styles2 = useStyles2(getStyles); + const styles = useStyles2(getStyles); let cssColor: string; @@ -29,28 +31,48 @@ export const SeriesIcon = React.memo( cssColor = color!; } - const styles: CSSProperties = { - background: cssColor, - width: '14px', - height: '4px', - borderRadius: theme.shape.radius.pill, - display: 'inline-block', - marginRight: '8px', - }; + let customStyle: CSSProperties; + + if (lineStyle?.fill === 'dot' && !gradient) { + // make a circle bg image and repeat it + customStyle = { + backgroundImage: `radial-gradient(circle at 2px 2px, ${color} 2px, transparent 0)`, + backgroundSize: '4px 4px', + backgroundRepeat: 'space', + }; + } else if (lineStyle?.fill === 'dash' && !gradient) { + // make a rectangle bg image and repeat it + customStyle = { + backgroundImage: `linear-gradient(to right, ${color} 100%, transparent 0%)`, + backgroundSize: '6px 4px', + backgroundRepeat: 'space', + }; + } else { + customStyle = { + background: cssColor, + borderRadius: theme.shape.radius.pill, + }; + } return (
); }) ); -const getStyles = () => ({ +const getStyles = (theme: GrafanaTheme2) => ({ + container: css({ + marginRight: '8px', + display: 'inline-block', + width: '14px', + height: '4px', + }), forcedColors: css({ '@media (forced-colors: active)': { forcedColorAdjust: 'none', diff --git a/packages/grafana-ui/src/components/VizLegend/VizLegendListItem.tsx b/packages/grafana-ui/src/components/VizLegend/VizLegendListItem.tsx index 5a43727e728..f19b2d61d8f 100644 --- a/packages/grafana-ui/src/components/VizLegend/VizLegendListItem.tsx +++ b/packages/grafana-ui/src/components/VizLegend/VizLegendListItem.tsx @@ -75,6 +75,7 @@ export const VizLegendListItem = ({ color={item.color} gradient={item.gradient} readonly={readonly} + lineStyle={item.lineStyle} />