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: add a categories formatter #878

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions src/components/chart-elements/AreaChart/AreaChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
BaseColors,
colorPalette,
defaultValueFormatter,
defaultCategoryFormatter,
getColorClassNames,
themeColorRange,
tremorTwMerge,
Expand All @@ -50,6 +51,7 @@ const AreaChart = React.forwardRef<HTMLDivElement, AreaChartProps>((props, ref)
const {
data = [],
categories = [],
categoryFormatter = defaultCategoryFormatter,
index,
stack = false,
colors = themeColorRange,
Expand Down Expand Up @@ -84,7 +86,8 @@ const AreaChart = React.forwardRef<HTMLDivElement, AreaChartProps>((props, ref)
const [legendHeight, setLegendHeight] = useState(60);
const [activeDot, setActiveDot] = useState<ActiveDot | undefined>(undefined);
const [activeLegend, setActiveLegend] = useState<string | undefined>(undefined);
const categoryColors = constructCategoryColors(categories, colors);
const formattedCategories = categories.map((category: string) => categoryFormatter(category));
const categoryColors = constructCategoryColors(formattedCategories, colors);

const yAxisDomain = getYAxisDomain(autoMinValue, minValue, maxValue);
const hasOnValueChange = !!onValueChange;
Expand Down Expand Up @@ -257,7 +260,7 @@ const AreaChart = React.forwardRef<HTMLDivElement, AreaChartProps>((props, ref)
}
/>
) : null}
{categories.map((category) => {
{formattedCategories.map((category) => {
return (
<defs key={category}>
{showGradient ? (
Expand Down Expand Up @@ -308,7 +311,7 @@ const AreaChart = React.forwardRef<HTMLDivElement, AreaChartProps>((props, ref)
</defs>
);
})}
{categories.map((category) => (
{formattedCategories.map((category) => (
<Area
className={
getColorClassNames(
Expand Down Expand Up @@ -399,7 +402,7 @@ const AreaChart = React.forwardRef<HTMLDivElement, AreaChartProps>((props, ref)
/>
))}
{onValueChange
? categories.map((category) => (
? formattedCategories.map((category) => (
<Line
className={tremorTwMerge("cursor-pointer")}
strokeOpacity={0}
Expand Down
1 change: 1 addition & 0 deletions src/components/chart-elements/common/BaseChartProps.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export type EventProps = BaseEventProps | null | undefined;
interface BaseChartProps extends BaseAnimationTimingProps, React.HTMLAttributes<HTMLDivElement> {
data: any[];
categories: string[];
categoryFormatter?: ValueFormatter<string, string>;
index: string;
colors?: (Color | string)[];
valueFormatter?: ValueFormatter;
Expand Down
4 changes: 2 additions & 2 deletions src/lib/inputTypes.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export type ValueFormatter = {
(value: number): string;
export type ValueFormatter<T = number, E = string> = {
(value: T): E;
};

export type CurveType = "linear" | "natural" | "monotone" | "step";
Expand Down
2 changes: 2 additions & 0 deletions src/lib/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ export const mapInputsToDeltaType = (deltaType: string, isIncreasePositive: bool

export const defaultValueFormatter: ValueFormatter = (value: number) => value.toString();

export const defaultCategoryFormatter: ValueFormatter<string, string> = (value: string) => value;

export const currencyValueFormatter: ValueFormatter = (e: number) =>
`$ ${Intl.NumberFormat("en-US").format(e)}`;

Expand Down
6 changes: 5 additions & 1 deletion src/stories/chart-elements/AreaChart.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
longIndexBaseChartData,
simpleBaseChartWithNegativeValues,
} from "./helpers/testData";
import { valueFormatter } from "./helpers/utils";
import { valueFormatter, categoryFormatter } from "./helpers/utils";

const meta: Meta<typeof AreaChart> = {
title: "Visualizations/Chart/AreaChart",
Expand Down Expand Up @@ -48,6 +48,10 @@ export const ValueFormatter: Story = {
args: { valueFormatter: valueFormatter, yAxisWidth: 60 },
};

export const CategoryFormatter: Story = {
args: { categoryFormatter: categoryFormatter },
};

export const AutoMinValue: Story = {
args: { autoMinValue: true },
};
Expand Down
4 changes: 4 additions & 0 deletions src/stories/chart-elements/helpers/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
export const valueFormatter = (number: number) => {
return Intl.NumberFormat("us").format(number).toString() + " $";
};

export const categoryFormatter = (value: string) => {
return value.toUpperCase();
};
Loading