Skip to content

Commit 5841519

Browse files
feat(charts): add support for children prop (enable linear gradients) (#1105)
* feat(charts): add support for children prop (enable linear gradients) * LineChart: add support for showDot flag * Update packages/charts/src/interfaces/IChartBaseProps.ts Co-authored-by: Lukas Harbarth <lukas742@gmx.net> Co-authored-by: Lukas Harbarth <lukas742@gmx.net>
1 parent e053710 commit 5841519

10 files changed

Lines changed: 54 additions & 3 deletions

File tree

packages/charts/src/components/BarChart/BarChart.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,7 @@ const BarChart: FC<BarChartProps> = forwardRef((props: BarChartProps, ref: Ref<H
280280
height={20}
281281
/>
282282
)}
283+
{props.children}
283284
</BarChartLib>
284285
</ChartContainer>
285286
);

packages/charts/src/components/ColumnChart/ColumnChart.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,7 @@ const ColumnChart: FC<ColumnChartProps> = forwardRef((props: ColumnChartProps, r
299299
height={20}
300300
/>
301301
)}
302+
{props.children}
302303
</ColumnChartLib>
303304
</ChartContainer>
304305
);

packages/charts/src/components/ComposedChart/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,7 @@ const ComposedChart: FC<ComposedChartProps> = forwardRef((props: ComposedChartPr
410410
height={20}
411411
/>
412412
)}
413+
{props.children}
413414
</ComposedChartLib>
414415
</ChartContainer>
415416
);

packages/charts/src/components/LineChart/LineChart.stories.tsx

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,3 +204,34 @@ export const withReferenceLineStory = (props) => {
204204
};
205205

206206
withReferenceLineStory.storyName = 'With reference line';
207+
208+
export const withLinearGradient = (props) => {
209+
return (
210+
<LineChart
211+
loading={props.loading}
212+
noLegend={props.noLegend}
213+
noAnimation={props.noAnimation}
214+
onDataPointClick={props.onDataPointClick}
215+
onLegendClick={props.onLegendClick}
216+
dataset={bigDataSet}
217+
dimensions={[{ accessor: 'name' }]}
218+
measures={[
219+
{
220+
accessor: 'users',
221+
width: 2,
222+
color: 'url(#colorUsers)'
223+
}
224+
]}
225+
style={{ width: '95%', height: '40vh' }}
226+
>
227+
<defs>
228+
<linearGradient id="colorUsers" x1="0%" y1="0%" x2="0%" y2="100%">
229+
<stop offset="0%" stopColor="red" />
230+
<stop offset="100%" stopColor="green" />
231+
</linearGradient>
232+
</defs>
233+
</LineChart>
234+
);
235+
};
236+
237+
withLinearGradient.storyName = 'With linear gradient';

packages/charts/src/components/LineChart/LineChart.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ interface MeasureConfig extends IChartMeasure {
4343
* @default 1
4444
*/
4545
opacity?: number;
46+
47+
/**
48+
* Flag whether the line dot should be displayed or not.
49+
*/
50+
showDot?: boolean;
4651
}
4752

4853
interface DimensionConfig extends IChartDimension {
@@ -67,6 +72,7 @@ export interface LineChartProps extends IChartBaseProps {
6772
* - `DataLabel`: a custom component to be used for the data label
6873
* - `width`: line width, defaults to `1`
6974
* - `opacity`: line opacity, defaults to `1`
75+
* - `showDot`: Flag whether the line dot should be displayed or not.
7076
*
7177
*/
7278
measures: MeasureConfig[];
@@ -227,7 +233,7 @@ const LineChart: FC<LineChartProps> = forwardRef((props: LineChartProps, ref: Re
227233
{measures.map((element, index) => {
228234
return (
229235
<Line
230-
dot={!isBigDataSet}
236+
dot={element.showDot ?? !isBigDataSet}
231237
yAxisId={chartConfig.secondYAxis?.dataKey === element.accessor ? 'right' : 'left'}
232238
key={element.accessor}
233239
name={element.label ?? element.accessor}
@@ -275,6 +281,7 @@ const LineChart: FC<LineChartProps> = forwardRef((props: LineChartProps, ref: Re
275281
height={20}
276282
/>
277283
)}
284+
{props.children}
278285
</LineChartLib>
279286
</ChartContainer>
280287
);

packages/charts/src/components/MicroBarChart/MicroBarChart.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ interface DimensionConfig {
4141
}
4242

4343
export interface MicroBarChartProps
44-
extends Omit<IChartBaseProps, 'noLegend' | 'onLegendClick' | 'noAnimation' | 'chartConfig'> {
44+
extends Omit<IChartBaseProps, 'noLegend' | 'onLegendClick' | 'noAnimation' | 'chartConfig' | 'children'> {
4545
dimension: DimensionConfig;
4646
/**
4747
* An array of config objects. Each object is defining one bar in the chart.

packages/charts/src/components/PieChart/PieChart.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,7 @@ const PieChart: FC<PieChartProps> = forwardRef((props: PieChartProps, ref: Ref<H
285285
wrapperStyle={legendWrapperStyle}
286286
/>
287287
)}
288+
{props.children}
288289
</PieChartLib>
289290
</ChartContainer>
290291
);

packages/charts/src/components/RadarChart/RadarChart.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ const RadarChart: FC<RadarChartProps> = forwardRef((props: RadarChartProps, ref:
8585
legendPosition: 'bottom',
8686
legendHorizontalAlign: 'center',
8787
dataLabel: true,
88-
polarGridType: 'circle' as 'circle',
88+
polarGridType: 'circle',
8989
resizeDebounce: 250,
9090
...props.chartConfig
9191
};
@@ -183,6 +183,7 @@ const RadarChart: FC<RadarChartProps> = forwardRef((props: RadarChartProps, ref:
183183
onClick={onItemLegendClick}
184184
/>
185185
)}
186+
{props.children}
186187
</RadarChartLib>
187188
</ChartContainer>
188189
);

packages/charts/src/components/ScatterChart/ScatterChart.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ const ScatterChart: FC<ScatterChartProps> = forwardRef((props: ScatterChartProps
270270
</ReferenceLine>
271271
)}
272272
<Tooltip cursor={tooltipFillOpacity} formatter={tooltipValueFormatter} contentStyle={tooltipContentStyle} />
273+
{props.children}
273274
</ScatterChartLib>
274275
</ChartContainer>
275276
);

packages/charts/src/interfaces/IChartBaseProps.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
import { CommonProps } from '@ui5/webcomponents-react/interfaces/CommonProps';
2+
import { ReactNode, ReactNodeArray } from 'react';
23
import { ICartesianChartConfig } from './ICartesianChartConfig';
34

45
export interface IChartBaseProps<T = ICartesianChartConfig> extends CommonProps {
56
loading?: boolean;
67
dataset?: Record<string, any>[];
78

9+
/**
10+
* With the help of the `children` prop you can add more svg elements to the chart, e.g. if you want to display
11+
* a linear gradient.
12+
*/
13+
children?: ReactNode | ReactNodeArray;
14+
815
noLegend?: boolean;
916
onDataPointClick?: (event: CustomEvent) => void;
1017
onLegendClick?: (event: CustomEvent) => void;

0 commit comments

Comments
 (0)