Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/specBuilder/bar/barSpecBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export const addBar = produce<Spec, [BarProps & { colorScheme?: ColorScheme; ind
color = { value: 'categorical-100' },
colorScheme = DEFAULT_COLOR_SCHEME,
dimension = DEFAULT_CATEGORICAL_DIMENSION,
hasSquareCorners = false,
index = 0,
lineType = { value: 'solid' },
lineWidth = 0,
Expand All @@ -74,6 +75,7 @@ export const addBar = produce<Spec, [BarProps & { colorScheme?: ColorScheme; ind
color,
colorScheme,
dimension,
hasSquareCorners,
index,
lineType,
lineWidth,
Expand Down
1 change: 1 addition & 0 deletions src/specBuilder/bar/barTestUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export const defaultBarProps: BarSpecProps = {
color: DEFAULT_COLOR,
colorScheme: DEFAULT_COLOR_SCHEME,
dimension: DEFAULT_CATEGORICAL_DIMENSION,
hasSquareCorners: false,
index: 0,
lineType: { value: 'solid' },
lineWidth: 0,
Expand Down
43 changes: 43 additions & 0 deletions src/specBuilder/bar/barUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,27 @@ describe('barUtils', () => {
expect(horizontal?.cornerRadiusBottomRight).toEqual(vertical?.cornerRadiusTopRight);
expect(horizontal?.cornerRadiusBottomLeft).toEqual(vertical?.cornerRadiusBottomRight);
});
test('corner radius should be 0 when the hasSquareCorners prop is true', () => {
const squareRadius = getCornerRadiusEncodings({ ...defaultBarProps, hasSquareCorners: true });

// Square radius should have values of 0
expect(squareRadius).toEqual(
expect.objectContaining({
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't know about expect.objectContaining. Very cool. The more you know.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a good way to only check about the properties you care about, regardless of the rest of the object shape.

cornerRadiusTopLeft: expect.arrayContaining([expect.objectContaining({ value: 0 })]),
cornerRadiusTopRight: expect.arrayContaining([expect.objectContaining({ value: 0 })]),
})
);

const roundRadius = getCornerRadiusEncodings({ ...defaultBarProps });

// Round radius should have values of 6
expect(roundRadius).toEqual(
expect.objectContaining({
cornerRadiusTopLeft: expect.arrayContaining([expect.objectContaining({ value: CORNER_RADIUS })]),
cornerRadiusTopRight: expect.arrayContaining([expect.objectContaining({ value: CORNER_RADIUS })]),
})
);
});
});

describe('getStackedCorderRadiusEncodings()', () => {
Expand Down Expand Up @@ -261,6 +282,28 @@ describe('barUtils', () => {
{ value: 0 },
]);
});

test('corner radius should be 0 when the hasSquareCorners prop is true', () => {
const squareRadius = getStackedCornerRadiusEncodings({ ...defaultBarProps, hasSquareCorners: true });

// Square radius should have values of 0
expect(squareRadius).toEqual(
expect.objectContaining({
cornerRadiusTopLeft: expect.arrayContaining([expect.objectContaining({ value: 0 })]),
cornerRadiusTopRight: expect.arrayContaining([expect.objectContaining({ value: 0 })]),
})
);

const roundRadius = getStackedCornerRadiusEncodings({ ...defaultBarProps });

// Round radius should have values of 6
expect(roundRadius).toEqual(
expect.objectContaining({
cornerRadiusTopLeft: expect.arrayContaining([expect.objectContaining({ value: CORNER_RADIUS })]),
cornerRadiusTopRight: expect.arrayContaining([expect.objectContaining({ value: CORNER_RADIUS })]),
})
);
});
});

describe('getOrientationProperties()', () => {
Expand Down
13 changes: 9 additions & 4 deletions src/specBuilder/bar/barUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,8 @@ export const getStackedMetricEncodings = (props: BarSpecProps): RectEncodeEntry
};

export const getCornerRadiusEncodings = (props: BarSpecProps): RectEncodeEntry => {
const { type, lineWidth, metric } = props;
const value = Math.max(1, CORNER_RADIUS - getLineWidthPixelsFromLineWidth(lineWidth) / 2);
const { type, lineWidth, metric, hasSquareCorners } = props;
const value = hasSquareCorners ? 0 : Math.max(1, CORNER_RADIUS - getLineWidthPixelsFromLineWidth(lineWidth) / 2);
Comment on lines +183 to +184
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clean and simple


let rectEncodeEntry: RectEncodeEntry;

Expand All @@ -199,10 +199,15 @@ export const getCornerRadiusEncodings = (props: BarSpecProps): RectEncodeEntry =
return rotateRectClockwiseIfNeeded(rectEncodeEntry, props);
};

export const getStackedCornerRadiusEncodings = ({ name, metric, lineWidth }: BarSpecProps): RectEncodeEntry => {
export const getStackedCornerRadiusEncodings = ({
name,
metric,
lineWidth,
hasSquareCorners,
}: BarSpecProps): RectEncodeEntry => {
const topTestString = `datum.${metric}1 > 0 && data('${name}_stacks')[indexof(pluck(data('${name}_stacks'), '${STACK_ID}'), datum.${STACK_ID})].max_${metric}1 === datum.${metric}1`;
const bottomTestString = `datum.${metric}1 < 0 && data('${name}_stacks')[indexof(pluck(data('${name}_stacks'), '${STACK_ID}'), datum.${STACK_ID})].min_${metric}1 === datum.${metric}1`;
const value = Math.max(1, CORNER_RADIUS - getLineWidthPixelsFromLineWidth(lineWidth) / 2);
const value = hasSquareCorners ? 0 : Math.max(1, CORNER_RADIUS - getLineWidthPixelsFromLineWidth(lineWidth) / 2);

return {
cornerRadiusTopLeft: [{ test: topTestString, value }, { value: 0 }],
Expand Down
9 changes: 8 additions & 1 deletion src/stories/components/Bar/Bar.story.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,11 @@ WithAnnotation.args = {
metric: 'downloads',
};

export { Basic, Horizontal, LineType, Opacity, PaddingRatio, WithAnnotation };
const HasSquareCorners = bindWithProps(BarStory);
HasSquareCorners.args = {
dimension: 'browser',
metric: 'downloads',
hasSquareCorners: true,
};

export { Basic, Horizontal, LineType, Opacity, PaddingRatio, WithAnnotation, HasSquareCorners };
2 changes: 2 additions & 0 deletions src/types/Chart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,8 @@ export interface BarProps extends Omit<MarkProps, 'color'> {
dimension?: string;
/** Sets the inner padding between bars in a group */
groupedPadding?: number;
/** Should the top-left and top-right corners of the bars be square? Round by default */
hasSquareCorners?: boolean;
/** Line type or key in the data that is used as the line type facet */
lineType?: LineTypeFacet | DualFacet;
/** Border width of the bar */
Expand Down
1 change: 1 addition & 0 deletions src/types/specBuilderTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ export interface AxisAnnotationSpecProps
type BarPropsWithDefaults =
| 'color'
| 'dimension'
| 'hasSquareCorners'
| 'lineType'
| 'lineWidth'
| 'metric'
Expand Down