Skip to content

Commit bb1fbff

Browse files
committed
Add axisScales "log" example
1 parent 93da949 commit bb1fbff

File tree

7 files changed

+35
-15
lines changed

7 files changed

+35
-15
lines changed

example/app/line-chart.tsx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
} from "victory-native";
1313
import type { AxisLabelPosition } from "lib/src/types";
1414
import { useDarkMode } from "react-native-dark";
15+
import type { AxisScaletype } from "lib/src/cartesian/utils/makeScale";
1516
import { InputSlider } from "example/components/InputSlider";
1617
import { InputSegment } from "example/components/InputSegment";
1718
import {
@@ -44,6 +45,7 @@ export default function LineChartPage(props: { segment: string }) {
4445
strokeWidth,
4546
xAxisSide,
4647
yAxisSide,
48+
axisScales,
4749
xLabelOffset,
4850
yLabelOffset,
4951
xTickCount,
@@ -84,6 +86,7 @@ export default function LineChartPage(props: { segment: string }) {
8486
padding={chartPadding}
8587
yKeys={["sales"]}
8688
axisOptions={{
89+
axisScales,
8790
font,
8891
lineWidth: { grid: { x: 0, y: 2 }, frame: 0 },
8992
lineColor: {
@@ -287,7 +290,6 @@ export default function LineChartPage(props: { segment: string }) {
287290
value={xAxisSide}
288291
values={["top", "bottom"]}
289292
/>
290-
291293
<InputSegment<AxisLabelPosition>
292294
label="X Axis Label position"
293295
onChange={(val) =>
@@ -339,6 +341,14 @@ export default function LineChartPage(props: { segment: string }) {
339341
value={yAxisSide}
340342
values={["left", "right"]}
341343
/>
344+
<InputSegment<AxisScaletype>
345+
label="Y Axis scales"
346+
onChange={(val) =>
347+
dispatch({ type: "SET_AXIS_SCALE", payload: { yAxisScale: val } })
348+
}
349+
value={axisScales?.yAxisScale || "linear"}
350+
values={["linear", "log"]}
351+
/>
342352
<InputColor
343353
label="Y-axis Label Color"
344354
color={colors.yLabel}

example/hooks/useOptionsReducer.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { AxisLabelPosition } from "lib/src/types";
1+
import type { AxisLabelPosition, AxisScales } from "lib/src/types";
22
import type { CurveType, XAxisSide, YAxisSide } from "victory-native";
33

44
type State = {
@@ -11,6 +11,7 @@ type State = {
1111
xTickCount: number;
1212
xAxisSide: XAxisSide;
1313
yAxisSide: YAxisSide;
14+
axisScales: AxisScales;
1415
scatterRadius: number;
1516
xAxisLabelPosition: AxisLabelPosition;
1617
yAxisLabelPosition: AxisLabelPosition;
@@ -33,6 +34,7 @@ type Action =
3334
| { type: "SET_X_TICK_COUNT"; payload: number }
3435
| { type: "SET_X_AXIS_SIDE"; payload: XAxisSide }
3536
| { type: "SET_Y_AXIS_SIDE"; payload: YAxisSide }
37+
| { type: "SET_AXIS_SCALE"; payload: AxisScales }
3638
| { type: "SET_SCATTER_RADIUS"; payload: number }
3739
| { type: "SET_X_AXIS_LABEL_POSITION"; payload: AxisLabelPosition }
3840
| { type: "SET_Y_AXIS_LABEL_POSITION"; payload: AxisLabelPosition }
@@ -64,6 +66,8 @@ export const optionsReducer = (state: State, action: Action): State => {
6466
return { ...state, xAxisSide: action.payload };
6567
case "SET_Y_AXIS_SIDE":
6668
return { ...state, yAxisSide: action.payload };
69+
case "SET_AXIS_SCALE":
70+
return { ...state, axisScales: action.payload };
6771
case "SET_SCATTER_RADIUS":
6872
return { ...state, scatterRadius: action.payload };
6973
case "SET_X_AXIS_LABEL_POSITION":
@@ -101,6 +105,9 @@ export const optionsInitialState: State = {
101105
scatterRadius: 7,
102106
xAxisSide: "bottom",
103107
yAxisSide: "left",
108+
axisScales: {
109+
yAxisScale: "linear",
110+
},
104111
xAxisLabelPosition: "outset",
105112
yAxisLabelPosition: "outset",
106113
colors: {},

lib/src/cartesian/CartesianChart.tsx

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,7 @@ import type {
2727
Viewport,
2828
GestureHandlerConfig,
2929
} from "../types";
30-
import {
31-
transformInputData,
32-
type AxisScaleParam,
33-
} from "./utils/transformInputData";
30+
import { transformInputData } from "./utils/transformInputData";
3431
import { findClosestPoint } from "../utils/findClosestPoint";
3532
import { valueFromSidedNumber } from "../utils/valueFromSidedNumber";
3633
import { asNumber } from "../utils/asNumber";
@@ -94,7 +91,6 @@ type CartesianChartProps<
9491
args: CartesianChartRenderArg<RawData, YK>,
9592
) => React.ReactNode;
9693
axisOptions?: Partial<Omit<AxisProps<RawData, XK, YK>, "xScale" | "yScale">>;
97-
axisScales?: AxisScaleParam;
9894
onChartBoundsChange?: (bounds: ChartBounds) => void;
9995
onScaleChange?: (
10096
xScale: ScaleLinear<number, number>,
@@ -164,7 +160,6 @@ function CartesianChartContent<
164160
customGestures,
165161
actionsRef,
166162
viewport,
167-
axisScales,
168163
}: CartesianChartProps<RawData, XK, YK>) {
169164
const [size, setSize] = React.useState({ width: 0, height: 0 });
170165
const chartBoundsRef = React.useRef<ChartBounds | undefined>(undefined);
@@ -244,7 +239,7 @@ function CartesianChartContent<
244239
yAxes: normalizedAxisProps.yAxes,
245240
viewport,
246241
labelRotate: normalizedAxisProps.xAxis.labelRotate,
247-
axisScales,
242+
axisScales: axisOptions?.axisScales,
248243
});
249244

250245
const primaryYAxis = yAxes[0];

lib/src/cartesian/components/CartesianAxis.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ export const CartesianAxisDefaultProps = {
251251
tickCount: 5,
252252
labelOffset: { x: 2, y: 4 },
253253
axisSide: { x: "bottom", y: "left" },
254+
axisScales: { xAxisScale: "linear", yAxisScale: "linear" },
254255
labelPosition: "outset",
255256
formatXLabel: (label: ValueOf<InputDatum>) => String(label),
256257
formatYLabel: (label: ValueOf<InputDatum>) => String(label),

lib/src/cartesian/utils/makeScale.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const scaleFunctions = {
1010
log: scaleLog,
1111
};
1212

13-
export type AxisScale = keyof typeof scaleFunctions;
13+
export type AxisScaletype = keyof typeof scaleFunctions;
1414

1515
export const makeScale = ({
1616
inputBounds,

lib/src/cartesian/utils/transformInputData.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ import type {
1414
XAxisPropsWithDefaults,
1515
} from "../../types";
1616
import { asNumber } from "../../utils/asNumber";
17-
import { makeScale, type AxisScale } from "./makeScale";
17+
import { makeScale, type AxisScaletype } from "./makeScale";
1818

19-
export type AxisScaleParam = {
20-
xAxisScale?: AxisScale;
21-
yAxisScale?: AxisScale;
19+
export type AxisScales = {
20+
xAxisScale?: AxisScaletype;
21+
yAxisScale?: AxisScaletype;
2222
};
2323

2424
/**
@@ -68,7 +68,7 @@ export const transformInputData = <
6868
y?: [number, number];
6969
};
7070
labelRotate?: number;
71-
axisScales?: AxisScaleParam;
71+
axisScales?: AxisScales;
7272
}): TransformedData<RawData, XK, YK> & {
7373
xScale: ScaleLinear<number, number>;
7474
isNumericalData: boolean;

lib/src/types.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import type {
1212
UserSelect,
1313
TouchAction,
1414
} from "react-native-gesture-handler/lib/typescript/handlers/gestureHandlerCommon";
15+
import type { AxisScaletype } from "./cartesian/utils/makeScale";
1516

1617
export type PrimitiveViewWindow = {
1718
xMin: number;
@@ -35,6 +36,11 @@ export type XAxisSide = "top" | "bottom";
3536
export type YAxisSide = "left" | "right";
3637
export type AxisLabelPosition = "inset" | "outset";
3738

39+
export type AxisScales = {
40+
xAxisScale?: AxisScaletype;
41+
yAxisScale?: AxisScaletype;
42+
};
43+
3844
export type ScatterOptions = {
3945
radius: number;
4046
};
@@ -125,6 +131,7 @@ export type AxisProps<
125131
yTicksNormalized: number[];
126132
xScale: ScaleLinear<number, number, never>;
127133
yScale: ScaleLinear<number, number, never>;
134+
axisScales?: AxisScales;
128135
font?: SkFont | null;
129136
lineColor?: Color | { grid: Color | { x: Color; y: Color }; frame: Color };
130137
lineWidth?:

0 commit comments

Comments
 (0)