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

fix: allow empty tick values #2671

Merged
merged 5 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
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={[]}
/>
scottrippey marked this conversation as resolved.
Show resolved Hide resolved
</div>
</div>
);
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) => {
scottrippey marked this conversation as resolved.
Show resolved Hide resolved
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
scottrippey marked this conversation as resolved.
Show resolved Hide resolved
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