Skip to content

Commit

Permalink
fix: allow empty tick values (#2671)
Browse files Browse the repository at this point in the history
  • Loading branch information
msabree authored Nov 15, 2023
1 parent 0a26400 commit 6dc9f10
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 2 deletions.
8 changes: 8 additions & 0 deletions demo/ts/components/victory-axis-demo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,14 @@ export default class VictoryAxisDemo extends React.Component<
"Mariners\nSEA",
]}
/>

<VictoryAxis
label="Empty Values"
padding={{ top: 40, bottom: 40, left: 40, right: 90 }}
orientation="right"
style={styleOverrides}
tickValues={[]}
/>
</div>
</div>
);
Expand Down
2 changes: 2 additions & 0 deletions packages/victory-axis/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# victory-axis

## 36.6.13

## 36.6.12

## 36.6.11
Expand Down
3 changes: 2 additions & 1 deletion packages/victory-axis/src/helper-methods.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,8 @@ export const getBaseProps = (props, fallbackProps) => {
? { [otherAxis]: props.scale[otherAxis] }
: undefined,
};
return ticks.reduce((childProps, tickValue, index) => {
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
return (ticks as number[]).reduce((childProps, tickValue, index) => {
const tick = stringTicks ? stringTicks[index] : tickValue;
const text = tickFormat(tickValue, index, ticks);
const styles = getEvaluatedStyles(
Expand Down
15 changes: 14 additions & 1 deletion packages/victory-core/src/victory-util/axis.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,11 @@ function getStringTicks(props) {

function getTickArray(props) {
const { tickValues, tickFormat } = props;

if (tickValues?.length === 0) {
return [];
}

const axis = getAxis(props);
const stringMap = props.stringMap && props.stringMap[axis];
const getTicksFromFormat = () => {
Expand Down Expand Up @@ -276,6 +281,11 @@ function downsampleTicks(ticks: number[], tickCount: number) {
export function getTicks(props, scale: D3Scale, filterZero = false) {
const { tickCount } = props;
const tickArray = getTickArray(props);

if (tickArray?.length === 0) {
return [""];
}

const tickValues = tickArray ? tickArray.map((v) => v.value) : undefined;
if (tickValues) {
return downsampleTicks(tickValues, tickCount);
Expand Down Expand Up @@ -307,7 +317,10 @@ export function getTicks(props, scale: D3Scale, filterZero = false) {
function getDomainFromData(props, axis) {
const { polar, startAngle = 0, endAngle = 360 } = props;
const tickArray = getTickArray(props);
const tickValues = tickArray ? tickArray.map((v) => v.value) : undefined;
const tickValues =
tickArray && tickArray?.length !== 0
? tickArray.map((v) => v.value)
: undefined;
if (!Array.isArray(tickValues)) {
return undefined;
}
Expand Down
7 changes: 7 additions & 0 deletions stories/victory-axis.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,13 @@ export const TickValues = () => {
<VictoryChart {...defaultChartProps} scale={{ x: "log" }}>
<VictoryAxis tickValues={[1, 3, 5, 7, 10, 50, 100, 500, 1000]} />
</VictoryChart>
<VictoryChart {...defaultChartProps}>
<VictoryAxis
label={"Empty Tick Values"}
tickValues={[]}
orientation="right"
/>
</VictoryChart>
</div>
);
};
Expand Down

1 comment on commit 6dc9f10

@vercel
Copy link

@vercel vercel bot commented on 6dc9f10 Nov 15, 2023

Choose a reason for hiding this comment

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

Please sign in to comment.