refactor area char to take an array of areas and data keys
This commit is contained in:
1 parent
f1451cca96
commit
70ff4c31fb
1 file changed
+71
-20
@@ -11,6 +11,7 @@ import { createGradient } from "../Utils/gradientUtils";
|
|||||||
import PropTypes from "prop-types";
|
import PropTypes from "prop-types";
|
||||||
import { useTheme } from "@mui/material";
|
import { useTheme } from "@mui/material";
|
||||||
import { useId } from "react";
|
import { useId } from "react";
|
||||||
|
import { Fragment } from "react";
|
||||||
/**
|
/**
|
||||||
* CustomAreaChart component for rendering an area chart with optional gradient and custom ticks.
|
* CustomAreaChart component for rendering an area chart with optional gradient and custom ticks.
|
||||||
*
|
*
|
||||||
@@ -92,7 +93,49 @@ const CustomAreaChart = ({
|
|||||||
}) => {
|
}) => {
|
||||||
const theme = useTheme();
|
const theme = useTheme();
|
||||||
const uniqueId = useId();
|
const uniqueId = useId();
|
||||||
const gradientId = `gradient-${uniqueId}`;
|
|
||||||
|
const AREA_COLORS = [
|
||||||
|
// Blues
|
||||||
|
"#3182bd", // Deep blue
|
||||||
|
"#6baed6", // Medium blue
|
||||||
|
"#9ecae1", // Light blue
|
||||||
|
|
||||||
|
// Greens
|
||||||
|
"#74c476", // Soft green
|
||||||
|
"#a1d99b", // Light green
|
||||||
|
"#c7e9c0", // Pale green
|
||||||
|
|
||||||
|
// Oranges
|
||||||
|
"#fdae6b", // Warm orange
|
||||||
|
"#fdd0a2", // Light orange
|
||||||
|
"#feedde", // Pale orange
|
||||||
|
|
||||||
|
// Purples
|
||||||
|
"#9467bd", // Lavender
|
||||||
|
"#a55194", // Deep magenta
|
||||||
|
"#c994c7", // Soft magenta
|
||||||
|
|
||||||
|
// Reds
|
||||||
|
"#ff9896", // Soft red
|
||||||
|
"#de2d26", // Deep red
|
||||||
|
"#fc9272", // Medium red
|
||||||
|
|
||||||
|
// Cyans/Teals
|
||||||
|
"#17becf", // Cyan
|
||||||
|
"#7fcdbb", // Teal
|
||||||
|
"#a1dab4", // Light teal
|
||||||
|
|
||||||
|
// Yellows
|
||||||
|
"#fec44f", // Mustard
|
||||||
|
"#fee391", // Light yellow
|
||||||
|
"#ffffd4", // Pale yellow
|
||||||
|
|
||||||
|
// Additional colors
|
||||||
|
"#e377c2", // Soft pink
|
||||||
|
"#bcbd22", // Olive
|
||||||
|
"#2ca02c", // Vibrant green
|
||||||
|
];
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<ResponsiveContainer
|
<ResponsiveContainer
|
||||||
width="100%"
|
width="100%"
|
||||||
@@ -106,17 +149,11 @@ const CustomAreaChart = ({
|
|||||||
{...(xTick && { tick: xTick })}
|
{...(xTick && { tick: xTick })}
|
||||||
/>
|
/>
|
||||||
<YAxis
|
<YAxis
|
||||||
{...(yDomain && { domain: yDomain })}
|
|
||||||
dataKey={yKey}
|
dataKey={yKey}
|
||||||
|
{...(yDomain && { domain: yDomain })}
|
||||||
{...(yTick && { tick: yTick })}
|
{...(yTick && { tick: yTick })}
|
||||||
/>
|
/>
|
||||||
{gradient === true &&
|
|
||||||
createGradient({
|
|
||||||
id: gradientId,
|
|
||||||
startColor: gradientStartColor,
|
|
||||||
endColor: gradientEndColor,
|
|
||||||
direction: gradientDirection,
|
|
||||||
})}
|
|
||||||
<CartesianGrid
|
<CartesianGrid
|
||||||
stroke={theme.palette.border.light}
|
stroke={theme.palette.border.light}
|
||||||
strokeWidth={1}
|
strokeWidth={1}
|
||||||
@@ -124,15 +161,29 @@ const CustomAreaChart = ({
|
|||||||
fill="transparent"
|
fill="transparent"
|
||||||
vertical={false}
|
vertical={false}
|
||||||
/>
|
/>
|
||||||
{dataKeys.map((dataKey) => (
|
{dataKeys.map((dataKey, index) => {
|
||||||
<Area
|
const gradientId = `gradient-${uniqueId}-${index}`;
|
||||||
key={dataKey}
|
|
||||||
type="monotone"
|
return (
|
||||||
dataKey={dataKey}
|
<Fragment key={dataKey}>
|
||||||
stroke={strokeColor}
|
{gradient === true &&
|
||||||
fill={gradient === true ? `url(#${gradientId})` : fillColor}
|
createGradient({
|
||||||
/>
|
id: gradientId,
|
||||||
))}
|
startColor: gradientStartColor || AREA_COLORS[index],
|
||||||
|
endColor: gradientEndColor,
|
||||||
|
direction: gradientDirection,
|
||||||
|
})}
|
||||||
|
<Area
|
||||||
|
yKey={dataKey}
|
||||||
|
key={dataKey}
|
||||||
|
type="monotone"
|
||||||
|
dataKey={dataKey}
|
||||||
|
stroke={strokeColor || AREA_COLORS[index]}
|
||||||
|
fill={gradient === true ? `url(#${gradientId})` : fillColor}
|
||||||
|
/>
|
||||||
|
</Fragment>
|
||||||
|
);
|
||||||
|
})}
|
||||||
{customTooltip ? (
|
{customTooltip ? (
|
||||||
<Tooltip
|
<Tooltip
|
||||||
cursor={{ stroke: theme.palette.border.light }}
|
cursor={{ stroke: theme.palette.border.light }}
|
||||||
@@ -154,7 +205,7 @@ CustomAreaChart.propTypes = {
|
|||||||
yTick: PropTypes.object, // Recharts takes an instance of component, so we can't pass the component itself
|
yTick: PropTypes.object, // Recharts takes an instance of component, so we can't pass the component itself
|
||||||
xKey: PropTypes.string.isRequired,
|
xKey: PropTypes.string.isRequired,
|
||||||
xDomain: PropTypes.array,
|
xDomain: PropTypes.array,
|
||||||
yKey: PropTypes.string.isRequired,
|
yKey: PropTypes.string,
|
||||||
yDomain: PropTypes.array,
|
yDomain: PropTypes.array,
|
||||||
fillColor: PropTypes.string,
|
fillColor: PropTypes.string,
|
||||||
strokeColor: PropTypes.string,
|
strokeColor: PropTypes.string,
|
||||||
@@ -162,7 +213,7 @@ CustomAreaChart.propTypes = {
|
|||||||
gradientDirection: PropTypes.string,
|
gradientDirection: PropTypes.string,
|
||||||
gradientStartColor: PropTypes.string,
|
gradientStartColor: PropTypes.string,
|
||||||
gradientEndColor: PropTypes.string,
|
gradientEndColor: PropTypes.string,
|
||||||
customTooltip: PropTypes.func,
|
customTooltip: PropTypes.object,
|
||||||
height: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
|
height: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user