Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Custom Chart Color #845

Merged
merged 4 commits into from Dec 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 4 additions & 28 deletions src/components/chart-elements/AreaChart/AreaChart.tsx
Expand Up @@ -20,7 +20,6 @@ import ChartTooltip from "../common/ChartTooltip";
import NoData from "../common/NoData";
import {
constructCategoryColors,
constructCustomCategoryColors,
getYAxisDomain,
hasOnlyOneValueForThisKey,
} from "../common/utils";
Expand Down Expand Up @@ -54,7 +53,6 @@ const AreaChart = React.forwardRef<HTMLDivElement, AreaChartProps>((props, ref)
index,
stack = false,
colors = themeColorRange,
customChartColors = [],
valueFormatter = defaultValueFormatter,
startEndOnly = false,
showXAxis = true,
Expand Down Expand Up @@ -87,7 +85,6 @@ const AreaChart = React.forwardRef<HTMLDivElement, AreaChartProps>((props, ref)
const [activeDot, setActiveDot] = useState<ActiveDot | undefined>(undefined);
const [activeLegend, setActiveLegend] = useState<string | undefined>(undefined);
const categoryColors = constructCategoryColors(categories, colors);
const customCategoryColors = constructCustomCategoryColors(categories, customChartColors);

const yAxisDomain = getYAxisDomain(autoMinValue, minValue, maxValue);
const hasOnValueChange = !!onValueChange;
Expand Down Expand Up @@ -222,9 +219,7 @@ const AreaChart = React.forwardRef<HTMLDivElement, AreaChartProps>((props, ref)
<CustomTooltip
payload={payload?.map((payloadItem: any) => ({
...payloadItem,
color: customCategoryColors
? customCategoryColors.get(payloadItem.dataKey) ?? BaseColors.Gray
: categoryColors.get(payloadItem.dataKey) ?? BaseColors.Gray,
color: categoryColors.get(payloadItem.dataKey) ?? BaseColors.Gray,
}))}
active={active}
label={label}
Expand All @@ -236,7 +231,6 @@ const AreaChart = React.forwardRef<HTMLDivElement, AreaChartProps>((props, ref)
label={label}
valueFormatter={valueFormatter}
categoryColors={categoryColors}
customCategoryColors={customCategoryColors}
/>
)
) : (
Expand All @@ -259,7 +253,6 @@ const AreaChart = React.forwardRef<HTMLDivElement, AreaChartProps>((props, ref)
? (clickedLegendItem: string) => onCategoryClick(clickedLegendItem)
: undefined,
enableLegendSlider,
customCategoryColors,
)
}
/>
Expand All @@ -273,14 +266,9 @@ const AreaChart = React.forwardRef<HTMLDivElement, AreaChartProps>((props, ref)
getColorClassNames(
categoryColors.get(category) ?? BaseColors.Gray,
colorPalette.text,
!customCategoryColors ? undefined : customCategoryColors.get(category),
).textColor
}
id={
!customCategoryColors
? categoryColors.get(category)
: customCategoryColors.get(category)
}
id={categoryColors.get(category)}
x1="0"
y1="0"
x2="0"
Expand All @@ -301,14 +289,9 @@ const AreaChart = React.forwardRef<HTMLDivElement, AreaChartProps>((props, ref)
getColorClassNames(
categoryColors.get(category) ?? BaseColors.Gray,
colorPalette.text,
!customCategoryColors ? undefined : customCategoryColors.get(category),
).textColor
}
id={
!customCategoryColors
? categoryColors.get(category)
: customCategoryColors.get(category)
}
id={categoryColors.get(category)}
x1="0"
y1="0"
x2="0"
Expand All @@ -331,7 +314,6 @@ const AreaChart = React.forwardRef<HTMLDivElement, AreaChartProps>((props, ref)
getColorClassNames(
categoryColors.get(category) ?? BaseColors.Gray,
colorPalette.text,
!customCategoryColors ? undefined : customCategoryColors.get(category),
).strokeColor
}
strokeOpacity={activeDot || (activeLegend && activeLegend !== category) ? 0.3 : 1}
Expand All @@ -346,7 +328,6 @@ const AreaChart = React.forwardRef<HTMLDivElement, AreaChartProps>((props, ref)
getColorClassNames(
categoryColors.get(dataKey) ?? BaseColors.Gray,
colorPalette.text,
!customCategoryColors ? undefined : customCategoryColors.get(dataKey),
).fillColor,
)}
cx={cx}
Expand Down Expand Up @@ -395,7 +376,6 @@ const AreaChart = React.forwardRef<HTMLDivElement, AreaChartProps>((props, ref)
getColorClassNames(
categoryColors.get(dataKey) ?? BaseColors.Gray,
colorPalette.text,
!customCategoryColors ? undefined : customCategoryColors.get(dataKey),
).fillColor,
)}
/>
Expand All @@ -408,11 +388,7 @@ const AreaChart = React.forwardRef<HTMLDivElement, AreaChartProps>((props, ref)
type={curveType}
dataKey={category}
stroke=""
fill={`url(#${
!customCategoryColors
? categoryColors.get(category)
: customCategoryColors.get(category)
})`}
fill={`url(#${categoryColors.get(category)})`}
strokeWidth={2}
strokeLinejoin="round"
strokeLinecap="round"
Expand Down
16 changes: 2 additions & 14 deletions src/components/chart-elements/BarChart/BarChart.tsx
Expand Up @@ -17,12 +17,7 @@ import BaseChartProps from "../common/BaseChartProps";
import ChartLegend from "../common/ChartLegend";
import ChartTooltip from "../common/ChartTooltip";
import NoData from "../common/NoData";
import {
constructCategoryColors,
constructCustomCategoryColors,
deepEqual,
getYAxisDomain,
} from "../common/utils";
import { constructCategoryColors, deepEqual, getYAxisDomain } from "../common/utils";

import { BaseColors, defaultValueFormatter, themeColorRange } from "lib";
import { AxisDomain } from "recharts/types/util/types";
Expand Down Expand Up @@ -73,7 +68,6 @@ const BarChart = React.forwardRef<HTMLDivElement, BarChartProps>((props, ref) =>
categories = [],
index,
colors = themeColorRange,
customChartColors = [],
valueFormatter = defaultValueFormatter,
layout = "horizontal",
stack = false,
Expand Down Expand Up @@ -104,7 +98,6 @@ const BarChart = React.forwardRef<HTMLDivElement, BarChartProps>((props, ref) =>
const paddingValue = !showXAxis && !showYAxis ? 0 : 20;
const [legendHeight, setLegendHeight] = useState(60);
const categoryColors = constructCategoryColors(categories, colors);
const customCategoryColors = constructCustomCategoryColors(categories, customChartColors);
const [activeBar, setActiveBar] = React.useState<any | undefined>(undefined);
const [activeLegend, setActiveLegend] = useState<string | undefined>(undefined);
const hasOnValueChange = !!onValueChange;
Expand Down Expand Up @@ -287,9 +280,7 @@ const BarChart = React.forwardRef<HTMLDivElement, BarChartProps>((props, ref) =>
<CustomTooltip
payload={payload?.map((payloadItem: any) => ({
...payloadItem,
color: customCategoryColors
? customCategoryColors.get(payloadItem.dataKey) ?? BaseColors.Gray
: categoryColors.get(payloadItem.dataKey) ?? BaseColors.Gray,
color: categoryColors.get(payloadItem.dataKey) ?? BaseColors.Gray,
}))}
active={active}
label={label}
Expand All @@ -301,7 +292,6 @@ const BarChart = React.forwardRef<HTMLDivElement, BarChartProps>((props, ref) =>
label={label}
valueFormatter={valueFormatter}
categoryColors={categoryColors}
customCategoryColors={customCategoryColors}
/>
)
) : (
Expand All @@ -324,7 +314,6 @@ const BarChart = React.forwardRef<HTMLDivElement, BarChartProps>((props, ref) =>
? (clickedLegendItem: string) => onCategoryClick(clickedLegendItem)
: undefined,
enableLegendSlider,
customCategoryColors,
)
}
/>
Expand All @@ -335,7 +324,6 @@ const BarChart = React.forwardRef<HTMLDivElement, BarChartProps>((props, ref) =>
getColorClassNames(
categoryColors.get(category) ?? BaseColors.Gray,
colorPalette.background,
!customCategoryColors ? undefined : customCategoryColors.get(category),
).fillColor,
onValueChange ? "cursor-pointer" : "",
)}
Expand Down
6 changes: 2 additions & 4 deletions src/components/chart-elements/DonutChart/DonutChart.tsx
Expand Up @@ -25,8 +25,7 @@ export interface DonutChartProps extends BaseAnimationTimingProps {
data: any[];
category?: string;
index?: string;
colors?: Color[];
customChartColors?: string[];
colors?: (Color | string)[];
variant?: DonutChartVariant;
valueFormatter?: ValueFormatter;
label?: string;
Expand Down Expand Up @@ -80,7 +79,6 @@ const DonutChart = React.forwardRef<HTMLDivElement, DonutChartProps>((props, ref
category = "value",
index = "name",
colors = themeColorRange,
customChartColors = [],
variant = "donut",
valueFormatter = defaultValueFormatter,
label,
Expand Down Expand Up @@ -162,7 +160,7 @@ const DonutChart = React.forwardRef<HTMLDivElement, DonutChartProps>((props, ref
"stroke-tremor-background dark:stroke-dark-tremor-background",
onValueChange ? "cursor-pointer" : "cursor-default",
)}
data={parseData(data, colors, customChartColors)}
data={parseData(data, colors)}
cx="50%"
cy="50%"
startAngle={90}
Expand Down
Expand Up @@ -21,7 +21,6 @@ export const DonutChartTooltip = ({ active, payload, valueFormatter }: DonutChar
value={valueFormatter(payloadRow.value)}
name={payloadRow.name}
color={payloadRow.payload.color}
customColor={payloadRow.payload.customColor}
/>
</div>
</ChartTooltipFrame>
Expand Down
12 changes: 3 additions & 9 deletions src/components/chart-elements/DonutChart/inputParser.ts
@@ -1,21 +1,15 @@
import { BaseColors, colorPalette, getColorClassNames, sumNumericArray } from "lib";
import { Color, ValueFormatter } from "../../../lib/inputTypes";

export const parseData = (data: any[], colors: Color[], customColors?: string[]) =>
export const parseData = (data: any[], colors: (Color | string)[]) =>
data.map((dataPoint: any, idx: number) => {
const baseColor = idx < colors.length ? colors[idx] : BaseColors.Gray;
const baseColorCustom =
customColors && idx < customColors.length ? customColors[idx] : BaseColors.Gray;
return {
...dataPoint,
// explicitly adding color key if not present for tooltip coloring
color: baseColor,
customColor: baseColorCustom,
className: getColorClassNames(
baseColor ?? BaseColors.Gray,
colorPalette.background,
!customColors ? undefined : customColors[idx],
).fillColor,
className: getColorClassNames(baseColor ?? BaseColors.Gray, colorPalette.background)
.fillColor,
fill: "",
};
});
Expand Down
12 changes: 1 addition & 11 deletions src/components/chart-elements/LineChart/LineChart.tsx
Expand Up @@ -19,7 +19,6 @@ import ChartTooltip from "../common/ChartTooltip";
import NoData from "../common/NoData";
import {
constructCategoryColors,
constructCustomCategoryColors,
getYAxisDomain,
hasOnlyOneValueForThisKey,
} from "../common/utils";
Expand Down Expand Up @@ -50,7 +49,6 @@ const LineChart = React.forwardRef<HTMLDivElement, LineChartProps>((props, ref)
categories = [],
index,
colors = themeColorRange,
customChartColors = [],
valueFormatter = defaultValueFormatter,
startEndOnly = false,
showXAxis = true,
Expand Down Expand Up @@ -82,7 +80,6 @@ const LineChart = React.forwardRef<HTMLDivElement, LineChartProps>((props, ref)
const [activeDot, setActiveDot] = useState<ActiveDot | undefined>(undefined);
const [activeLegend, setActiveLegend] = useState<string | undefined>(undefined);
const categoryColors = constructCategoryColors(categories, colors);
const customCategoryColors = constructCustomCategoryColors(categories, customChartColors);

const yAxisDomain = getYAxisDomain(autoMinValue, minValue, maxValue);
const hasOnValueChange = !!onValueChange;
Expand Down Expand Up @@ -218,9 +215,7 @@ const LineChart = React.forwardRef<HTMLDivElement, LineChartProps>((props, ref)
<CustomTooltip
payload={payload?.map((payloadItem: any) => ({
...payloadItem,
color: customCategoryColors
? customCategoryColors.get(payloadItem.dataKey) ?? BaseColors.Gray
: categoryColors.get(payloadItem.dataKey) ?? BaseColors.Gray,
color: categoryColors.get(payloadItem.dataKey) ?? BaseColors.Gray,
}))}
active={active}
label={label}
Expand All @@ -232,7 +227,6 @@ const LineChart = React.forwardRef<HTMLDivElement, LineChartProps>((props, ref)
label={label}
valueFormatter={valueFormatter}
categoryColors={categoryColors}
customCategoryColors={customCategoryColors}
/>
)
) : (
Expand All @@ -256,7 +250,6 @@ const LineChart = React.forwardRef<HTMLDivElement, LineChartProps>((props, ref)
? (clickedLegendItem: string) => onCategoryClick(clickedLegendItem)
: undefined,
enableLegendSlider,
customCategoryColors,
)
}
/>
Expand All @@ -267,7 +260,6 @@ const LineChart = React.forwardRef<HTMLDivElement, LineChartProps>((props, ref)
getColorClassNames(
categoryColors.get(category) ?? BaseColors.Gray,
colorPalette.text,
!customCategoryColors ? undefined : customCategoryColors.get(category),
).strokeColor,
)}
strokeOpacity={activeDot || (activeLegend && activeLegend !== category) ? 0.3 : 1}
Expand All @@ -282,7 +274,6 @@ const LineChart = React.forwardRef<HTMLDivElement, LineChartProps>((props, ref)
getColorClassNames(
categoryColors.get(dataKey) ?? BaseColors.Gray,
colorPalette.text,
!customCategoryColors ? undefined : customCategoryColors.get(category),
).fillColor,
)}
cx={cx}
Expand Down Expand Up @@ -331,7 +322,6 @@ const LineChart = React.forwardRef<HTMLDivElement, LineChartProps>((props, ref)
getColorClassNames(
categoryColors.get(dataKey) ?? BaseColors.Gray,
colorPalette.text,
!customCategoryColors ? undefined : customCategoryColors.get(dataKey),
).fillColor,
)}
/>
Expand Down