Skip to content

Commit

Permalink
Fix: allow for tickValues to be honored in multiple Y Axes (#438)
Browse files Browse the repository at this point in the history
  • Loading branch information
masiddee authored Dec 6, 2024
1 parent 4aa3a56 commit 04be366
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/hungry-adults-cover.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"victory-native": patch
---

allow tickValues for multiple Y axes
3 changes: 2 additions & 1 deletion example/app/multiple-y-axes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,8 @@ export default function MultipleYAxesPage() {
return value.toFixed(0);
},
axisSide: "right",
lineColor: "green",
lineWidth: 0,
tickValues: [10000, 10030],
},
]}
data={data}
Expand Down
16 changes: 11 additions & 5 deletions lib/src/cartesian/CartesianChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import type {
} from "./hooks/useChartPressState";
import { useFunctionRef } from "../hooks/useFunctionRef";
import { CartesianChartProvider } from "./contexts/CartesianChartContext";
import { normalizeYAxisTicks } from "../utils/normalizeYAxisTicks";
import { XAxis } from "./components/XAxis";
import { YAxis } from "./components/YAxis";
import { Frame } from "./components/Frame";
Expand All @@ -50,6 +49,7 @@ import {
} from "./contexts/CartesianTransformContext";
import { downsampleTicks } from "../utils/tickHelpers";
import { GestureHandler } from "../shared/GestureHandler";
import { normalizeYAxisTicks } from "../utils/normalizeYAxisTicks";

export type CartesianActionsHandle<T = undefined> =
T extends ChartPressState<infer S>
Expand Down Expand Up @@ -527,13 +527,19 @@ function CartesianChartContent<
hasMeasuredLayoutSize && (axisOptions || yAxes)
? normalizedAxisProps.yAxes?.map((axis, index) => {
const yAxis = yAxes[index];

if (!yAxis) return null;

const primaryAxisProps = normalizedAxisProps.yAxes[0]!;
const primaryRescaled = zoom.rescaleY(primaryYScale);

const rescaled = zoom.rescaleY(yAxis.yScale);

const rescaledTicks = axis.tickValues
? downsampleTicks(axis.tickValues, axis.tickCount)
: axis.enableRescaling
? rescaled.ticks(axis.tickCount)
: yAxis.yScale.ticks(axis.tickCount);

const primaryTicksRescaled = primaryAxisProps.tickValues
? downsampleTicks(
primaryAxisProps.tickValues,
Expand All @@ -542,21 +548,21 @@ function CartesianChartContent<
: primaryAxisProps.enableRescaling
? primaryRescaled.ticks(primaryAxisProps.tickCount)
: primaryYScale.ticks(primaryAxisProps.tickCount);

return (
<YAxis
key={index}
{...axis}
xScale={zoom.rescaleX(xScale)}
yScale={rescaled}
yTicksNormalized={
// Since we treat the first yAxis as the primary yAxis, we normalize the other Y ticks against it so the ticks line up nicely
index > 0
index > 0 && !axis.tickValues
? normalizeYAxisTicks(
primaryTicksRescaled,
primaryRescaled,
rescaled,
)
: primaryTicksRescaled
: rescaledTicks
}
chartBounds={clipRect}
/>
Expand Down

0 comments on commit 04be366

Please sign in to comment.