|
1 | 1 | import * as React from "react"
|
2 |
| -import { range, round } from "../../math" |
| 2 | +import { pickClosestToValue, range, round, roundToNearestPowerOf10 } from "../../math" |
3 | 3 | import { usePaneContext } from "../../context/PaneContext"
|
4 | 4 | import { useTransformContext } from "../../context/TransformContext"
|
5 | 5 | import { vec } from "../../vec"
|
6 | 6 | import { XLabels, YLabels, AxisOptions, defaultAxisOptions } from "./Axes"
|
| 7 | +import { useCoordinateContext } from "../../context/CoordinateContext" |
| 8 | +import { useSpanContext } from "../../context/SpanContext" |
7 | 9 |
|
8 | 10 | // This is sort of a hack—every SVG pattern on a page needs a unique ID, otherwise they conflict.
|
9 | 11 | let incrementer = 0
|
10 | 12 |
|
11 | 13 | type GridAxisOptions = Partial<AxisOptions & { subdivisions: number | false }>
|
12 | 14 |
|
13 | 15 | export interface CartesianCoordinatesProps {
|
14 |
| - xAxis?: GridAxisOptions | false |
15 |
| - yAxis?: GridAxisOptions | false |
| 16 | + xAxis?: GridAxisOptions | false | "auto" |
| 17 | + yAxis?: GridAxisOptions | false | "auto" |
16 | 18 | subdivisions?: number | false
|
17 | 19 | }
|
18 | 20 |
|
19 | 21 | export function Cartesian({
|
20 |
| - xAxis: xAxisOverrides, |
21 |
| - yAxis: yAxisOverrides, |
| 22 | + xAxis: xAxisProp, |
| 23 | + yAxis: yAxisProp, |
22 | 24 | subdivisions = false,
|
23 | 25 | }: CartesianCoordinatesProps) {
|
24 |
| - const xAxisEnabled = xAxisOverrides !== false |
25 |
| - const yAxisEnabled = yAxisOverrides !== false |
| 26 | + const xAxisEnabled = xAxisProp !== false |
| 27 | + const yAxisEnabled = yAxisProp !== false |
| 28 | + |
| 29 | + // Better name? Also `3.5` is a magic number that makes it feel right to me, |
| 30 | + // it could have any other value (and perhaps it should be computed). |
| 31 | + // It *kind of* represents how many major grid lines can be on screen at once, |
| 32 | + // e.g. if it's 4 then there can be at most 4 major gridlines on the canvas. |
| 33 | + // |
| 34 | + // it seems like its behavior isn't quite that simple but it roughly explains why we divide at all here |
| 35 | + |
| 36 | + const mathWidth = useSpanContext().xSpan / 3.5 |
| 37 | + |
| 38 | + const nearestPowerOf10 = roundToNearestPowerOf10(mathWidth) |
| 39 | + |
| 40 | + // This should be a prop or something later, just a constant right now |
| 41 | + const multiples = [ |
| 42 | + { value: 1, subdivisions: 5 }, |
| 43 | + { value: 2, subdivisions: 4 }, |
| 44 | + { value: 5, subdivisions: 5 }, |
| 45 | + ] |
| 46 | + |
| 47 | + const autoClosest = pickClosestToValue( |
| 48 | + mathWidth, |
| 49 | + multiples.map((multiple) => nearestPowerOf10 * multiple.value), |
| 50 | + ) |
26 | 51 |
|
27 |
| - const xAxis = { subdivisions, ...defaultAxisOptions, ...xAxisOverrides } as GridAxisOptions |
28 |
| - const yAxis = { subdivisions, ...defaultAxisOptions, ...yAxisOverrides } as GridAxisOptions |
| 52 | + const autoLines = autoClosest[0]; |
| 53 | + const autoSubdivisions = multiples[autoClosest[1]].subdivisions |
| 54 | + |
| 55 | + const xAxisOverrides = |
| 56 | + xAxisProp === "auto" ? { lines: autoLines, subdivisions: autoSubdivisions } : xAxisProp !== false ? xAxisProp : false |
| 57 | + const yAxisOverrides = |
| 58 | + yAxisProp === "auto" ? { lines: autoLines, subdivisions: autoSubdivisions } : yAxisProp !== false ? yAxisProp : false |
| 59 | + |
| 60 | + const xAxis = { |
| 61 | + subdivisions, |
| 62 | + ...defaultAxisOptions, |
| 63 | + ...xAxisOverrides, |
| 64 | + } as GridAxisOptions |
| 65 | + const yAxis = { |
| 66 | + subdivisions, |
| 67 | + ...defaultAxisOptions, |
| 68 | + ...yAxisOverrides, |
| 69 | + } as GridAxisOptions |
29 | 70 |
|
30 | 71 | const id = React.useMemo(() => `cartesian-${incrementer++}`, [])
|
31 | 72 |
|
|
0 commit comments