Skip to content

Commit 6b45b0b

Browse files
authored
Merge pull request #25 from kubit-ui/feat/bar-axis-value-formatter-and-halfchart-path-fix
feat: add bar axis valueFormatter and fix halfChart path
2 parents a6eacf6 + 66b5e6e commit 6b45b0b

File tree

18 files changed

+304
-31
lines changed

18 files changed

+304
-31
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@
172172
"@storybook/addon-docs": "10.2.10",
173173
"@storybook/addon-links": "10.2.10",
174174
"@storybook/addon-themes": "10.2.10",
175+
"@storybook/react": "10.2.10",
175176
"@storybook/react-vite": "10.2.10",
176177
"@storybook/test-runner": "0.24.2",
177178
"@storybook/testing-library": "0.2.2",

pnpm-lock.yaml

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/__mocks__/styleMock.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
module.exports = {};
1+
export default {};

src/charts/barChart/barChart.type.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import type { TickData } from '@/components/tick/tick.types';
88
import type { CanvasConfig } from '@/types/canvas.type';
99
import type { ChartError, ChartErrorCollection, ErrorType } from '@/types/errors.type';
1010
import type { Positions } from '@/types/position.enum';
11+
import type { ValueFormatter } from '@/types/valueFormatter.type';
1112

1213
export type BarChartChildrenType = ReactNode | ReactElement<PathProps | XAxisProps | YAxisProps>;
1314

@@ -121,13 +122,15 @@ export interface BarChartXAxisProps extends Omit<XAxisProps, OmitProps>, React.A
121122
*/
122123
ariaLabel?: string;
123124
tickValues?: BarChartTickValuesAxisProps;
125+
valueFormatter?: ValueFormatter<string>;
124126
}
125127
export interface BarChartYAxisProps extends Omit<YAxisProps, OmitProps>, React.AriaAttributes {
126128
/**
127129
* @deprecated Use aria-label instead for better accessibility standards
128130
*/
129131
ariaLabel?: string;
130132
tickValues?: BarChartTickValuesAxisProps;
133+
valueFormatter?: ValueFormatter<string>;
131134
}
132135
export interface BarChartSeparatorProps {
133136
topSeparator?: StyleProps;

src/charts/barChart/fragments/__tests__/barChartXAxis.test.tsx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,20 @@ describe('LineChartXAxis', () => {
1515
const xAxis = getByTestId('testxAxis');
1616
expect(xAxis).toBeInTheDocument();
1717
});
18+
19+
it('renders formatted tick labels', () => {
20+
const { getByText } = render(
21+
<BarChartContext.Provider value={CONTEXT}>
22+
<BarChart.XAxis
23+
tickLine={{}}
24+
tickText={{ fontSize: 12 }}
25+
valueFormatter={value => `Label ${value}`}
26+
/>
27+
</BarChartContext.Provider>
28+
);
29+
30+
expect(getByText('Label 1')).toBeInTheDocument();
31+
expect(getByText('Label 2')).toBeInTheDocument();
32+
expect(getByText('Label 3')).toBeInTheDocument();
33+
});
1834
});

src/charts/barChart/fragments/__tests__/barChartYAxis.test.tsx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,20 @@ describe('LineChartYAxis', () => {
1515
const yAxis = getByTestId('testyAxis');
1616
expect(yAxis).toBeInTheDocument();
1717
});
18+
19+
it('renders formatted tick labels', () => {
20+
const { getByText } = render(
21+
<BarChartContext.Provider value={CONTEXT}>
22+
<BarChart.YAxis
23+
tickLine={{}}
24+
tickText={{ fontSize: 12 }}
25+
valueFormatter={value => `${value}%`}
26+
/>
27+
</BarChartContext.Provider>
28+
);
29+
30+
expect(getByText('10%')).toBeInTheDocument();
31+
expect(getByText('20%')).toBeInTheDocument();
32+
expect(getByText('30%')).toBeInTheDocument();
33+
});
1834
});

src/charts/barChart/fragments/barChartXAxis.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { type FC, type ReactElement, useContext } from 'react';
22

33
import { XAxis } from '@/components/axisChart/xAxis/xAxis';
4+
import { TickDataUtils } from '@/components/tick/tick.types';
45
import { Positions } from '@/types/position.enum';
56
import { getTickTextYCoordinate } from '@/utils/getTickTextCoordinate/getTickTextCoordinates';
67

@@ -12,6 +13,7 @@ export const BarChartXAxis: FC<BarChartXAxisProps> = ({
1213
position = Positions.BOTTOM,
1314
tickLine,
1415
tickText,
16+
valueFormatter = (value: string) => value,
1517
...props
1618
}): ReactElement => {
1719
const {
@@ -29,6 +31,9 @@ export const BarChartXAxis: FC<BarChartXAxisProps> = ({
2931

3032
const y1 = context.extraSpaceTopY;
3133
const y2 = Number(context.canvasHeight) - context.extraSpaceBottomY;
34+
const formattedTickValues = tickText
35+
? TickDataUtils.formatTicksValues(tickValues, valueFormatter)
36+
: undefined;
3237

3338
return (
3439
<XAxis
@@ -51,7 +56,7 @@ export const BarChartXAxis: FC<BarChartXAxisProps> = ({
5156
...tickText,
5257
y: tickTextY,
5358
}}
54-
tickValues={tickText ? tickValues : undefined}
59+
tickValues={formattedTickValues}
5560
/>
5661
);
5762
};

src/charts/barChart/fragments/barChartYAxis.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { type FC, type ReactElement, useContext } from 'react';
22

33
import { YAxis } from '@/components/axisChart/yAxis/yAxis';
4+
import { TickDataUtils } from '@/components/tick/tick.types';
45
import { Positions } from '@/types/position.enum';
56
import { ajustedTextSpace } from '@/utils/ajustedTextSpace/ajustedTextSpace';
67
import { getTickTextXCoordinate } from '@/utils/getTickTextCoordinate/getTickTextCoordinates';
@@ -13,6 +14,7 @@ export const BarChartYAxis: FC<BarChartYAxisProps> = ({
1314
position = Positions.LEFT,
1415
tickLine,
1516
tickText,
17+
valueFormatter = (value: string) => value,
1618
...props
1719
}): ReactElement => {
1820
const {
@@ -30,6 +32,9 @@ export const BarChartYAxis: FC<BarChartYAxisProps> = ({
3032
coordinates.x1,
3133
ajustedText
3234
);
35+
const formattedTickValues = tickText
36+
? TickDataUtils.formatTicksValues(tickValues, valueFormatter)
37+
: undefined;
3338

3439
return (
3540
<YAxis
@@ -44,7 +49,7 @@ export const BarChartYAxis: FC<BarChartYAxisProps> = ({
4449
x2: Number(context.canvasWidth) - context.extraSpaceRightX,
4550
}}
4651
tickText={{ ...tickText, x: xTickText }}
47-
tickValues={tickText ? tickValues : undefined}
52+
tickValues={formattedTickValues}
4853
/>
4954
);
5055
};

src/charts/barChart/stories/barChart.stories.tsx

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,7 @@ import { AXIS_WITH_HORIZONTAL_BARS } from './templates/axisWithHorizontalBars';
1212
import { AXIS_WITH_VERTICAL_BARS } from './templates/axisWithVericalBars';
1313
import { COMPARATIVE_DATA, MIXED_DATA } from './templates/data';
1414
import { BarChartWithErrorHandlingWithHooks } from './templates/withErrorHandling';
15-
import {
16-
BarChartWithTooltip,
17-
BarChartHorizontalWithTooltip,
18-
} from './templates/withTooltip';
15+
import { BarChartHorizontalWithTooltip, BarChartWithTooltip } from './templates/withTooltip';
1916

2017
const meta = {
2118
argTypes: argtypes(),
@@ -272,8 +269,8 @@ export const WithTooltipHorizontal: Story = {
272269
text={[
273270
<div key="tooltip-horizontal-explanation">
274271
<p>
275-
This example shows a <strong>horizontal bar chart</strong> with a fixed info
276-
panel that displays details about the hovered bar.
272+
This example shows a <strong>horizontal bar chart</strong> with a fixed info panel
273+
that displays details about the hovered bar.
277274
</p>
278275
<p>
279276
Instead of a floating tooltip, this pattern uses a persistent panel that updates

src/charts/barChart/stories/children/XAxis/xAxis.argtypes.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,26 @@ export const xAxisArgTypes = (): ArgTypes<BarChartXAxisProps> => {
181181
},
182182
},
183183

184+
valueFormatter: {
185+
control: {
186+
labels: {
187+
currency: 'Currency ($)',
188+
custom: 'Custom Format [val]',
189+
millions: 'Millions (M)',
190+
none: 'None (no formatting)',
191+
percentage: 'Percentage (%)',
192+
thousands: 'Thousands (K)',
193+
},
194+
type: 'select',
195+
},
196+
description: 'Select a formatting style for tick labels in this story.',
197+
options: ['none', 'currency', 'percentage', 'thousands', 'millions', 'custom'],
198+
table: {
199+
category: CATEGORY_CONTROL.DATA,
200+
type: { summary: 'ValueFormatter | undefined' },
201+
},
202+
},
203+
184204
transform: {
185205
control: { type: 'text' },
186206
description: 'SVG transform attribute for positioning and scaling.',

0 commit comments

Comments
 (0)