Skip to content

Commit bc4cd6e

Browse files
authored
feat: Support strokeDasharray and fillOpacity on AreaChart (#350)
* feat: Support strokeDasharray and fillOpacity on AreaChart Signed-off-by: lannyfu <[email protected]> * add changeset Signed-off-by: lannyfu <[email protected]> --------- Signed-off-by: lannyfu <[email protected]>
1 parent 2bf46b3 commit bc4cd6e

File tree

3 files changed

+38
-11
lines changed

3 files changed

+38
-11
lines changed

.changeset/olive-oranges-report.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@kubed/charts': patch
3+
---
4+
5+
feat: Support strokeDasharray and fillOpacity on AreaChart

packages/charts/src/charts/AreaChart/AreaChart.tsx

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,21 @@ import { AxisDomain } from 'recharts/types/util/types';
1515
import { remove } from 'lodash';
1616

1717
import { ChartBaseProps } from '../../types';
18-
import { defaultValueFormatter, getYAxisDomain, getCategoryColors } from '../../utils';
18+
import {
19+
defaultValueFormatter,
20+
getYAxisDomain,
21+
getCategoryColors,
22+
getCategoryMap,
23+
} from '../../utils';
1924
import { Legend as LegendContent } from '../../components/Legend';
2025
import { Tooltip as TooltipContent } from '../../components/Tooltip';
2126

2227
export interface AreaChartProps extends ChartBaseProps {
2328
stack?: boolean;
2429
xAxisProps?: XAxisProps;
2530
yAxisProps?: YAxisProps;
31+
strokeDasharrays?: any[];
32+
fillOpacitys?: any[];
2633
}
2734

2835
export const AreaChart = ({
@@ -53,12 +60,16 @@ export const AreaChart = ({
5360
maxActiveCategories = 50,
5461
xAxisProps = {},
5562
yAxisProps = {},
63+
strokeDasharrays,
64+
fillOpacitys,
5665
...rest
5766
}: AreaChartProps) => {
5867
const initialActiveCats = categories.slice(0, maxActiveCategories);
5968
const [activeCategories, setActiveCategories] = useState(initialActiveCats);
6069
const yAxisDomain = getYAxisDomain(autoMinValue, minValue, maxValue);
6170
const categoryColors = getCategoryColors(categories, colors);
71+
const categoryStrokeDasharrays = getCategoryMap(categories, strokeDasharrays);
72+
const categoryFillOpacitys = getCategoryMap(categories, fillOpacitys);
6273

6374
const handleLegendClick = (category) => {
6475
const newActiveCategories = [...activeCategories];
@@ -131,16 +142,17 @@ export const AreaChart = ({
131142
{showTooltip ? (
132143
<Tooltip
133144
wrapperStyle={{ outline: 'none' }}
134-
content={({ active, payload, label }) => (
135-
customTooltip && customTooltip(payload)) || (
136-
<TooltipContent
137-
active={active}
138-
payload={payload}
139-
label={label}
140-
valueFormatter={valueFormatter}
141-
legendFormatter={legendFormatter}
142-
/>
143-
)}
145+
content={({ active, payload, label }) =>
146+
(customTooltip && customTooltip(payload)) || (
147+
<TooltipContent
148+
active={active}
149+
payload={payload}
150+
label={label}
151+
valueFormatter={valueFormatter}
152+
legendFormatter={legendFormatter}
153+
/>
154+
)
155+
}
144156
/>
145157
) : null}
146158
{categories.map((category) => (
@@ -165,6 +177,8 @@ export const AreaChart = ({
165177
dataKey={category}
166178
stroke={categoryColors.get(category)}
167179
fill={`url(#${categoryColors.get(category)})`}
180+
strokeDasharray={categoryStrokeDasharrays.get(category)}
181+
fillOpacity={categoryFillOpacitys.get(category)}
168182
strokeWidth={1}
169183
dot={false}
170184
isAnimationActive={showAnimation}

packages/charts/src/utils.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,11 @@ export const removeValueFromArray = (value: any, array: any[]): any[] => {
3737
}
3838
return array;
3939
};
40+
41+
export const getCategoryMap = (categories: string[], array: string[] = []): Map<string, string> => {
42+
const categoryMap = new Map<string, string>();
43+
categories.forEach((category, idx) => {
44+
categoryMap.set(category, array[idx]);
45+
});
46+
return categoryMap;
47+
};

0 commit comments

Comments
 (0)