diff --git a/packages/axes/src/canvas.ts b/packages/axes/src/canvas.ts index cf7435da7a..2ff2ec033c 100644 --- a/packages/axes/src/canvas.ts +++ b/packages/axes/src/canvas.ts @@ -1,8 +1,15 @@ import { degreesToRadians, CompleteTheme } from '@nivo/core' import { computeCartesianTicks, getFormatter, computeGridLines } from './compute' -import { TicksSpec, AnyScale, AxisLegendPosition, CanvasAxisProp, ValueFormatter } from './types' - -export const renderAxisToCanvas = ( +import { + AxisValue, + TicksSpec, + AnyScale, + AxisLegendPosition, + CanvasAxisProp, + ValueFormatter, +} from './types' + +export const renderAxisToCanvas = ( ctx: CanvasRenderingContext2D, { axis, @@ -153,10 +160,7 @@ export const renderAxisToCanvas = ( const positions = ['top', 'right', 'bottom', 'left'] as const -export const renderAxesToCanvas = < - X extends string | number | Date, - Y extends string | number | Date ->( +export const renderAxesToCanvas = ( ctx: CanvasRenderingContext2D, { xScale, @@ -210,7 +214,7 @@ export const renderAxesToCanvas = < }) } -export const renderGridLinesToCanvas = ( +export const renderGridLinesToCanvas = ( ctx: CanvasRenderingContext2D, { width, diff --git a/packages/axes/src/components/Axes.tsx b/packages/axes/src/components/Axes.tsx index d97a6bd05f..4d2e287b36 100644 --- a/packages/axes/src/components/Axes.tsx +++ b/packages/axes/src/components/Axes.tsx @@ -1,10 +1,10 @@ import React from 'react' import { Axis } from './Axis' -import { AnyScale, AxisProp } from '../types' +import { AnyScale, AxisProp, AxisValue } from '../types' const positions = ['top', 'right', 'bottom', 'left'] as const -export const Axes = ({ +export const Axes = ({ xScale, yScale, width, diff --git a/packages/axes/src/components/Axis.tsx b/packages/axes/src/components/Axis.tsx index ec490ad6e1..bb27cca9e1 100644 --- a/packages/axes/src/components/Axis.tsx +++ b/packages/axes/src/components/Axis.tsx @@ -3,9 +3,9 @@ import { useSpring, useTransition, animated } from 'react-spring' import { useTheme, useMotionConfig } from '@nivo/core' import { computeCartesianTicks, getFormatter } from '../compute' import { AxisTick } from './AxisTick' -import { AxisProps } from '../types' +import { AxisProps, AxisValue } from '../types' -export const Axis = ({ +export const Axis = ({ axis, scale, x = 0, diff --git a/packages/axes/src/components/AxisTick.tsx b/packages/axes/src/components/AxisTick.tsx index adcc35bbff..c7b038b32a 100644 --- a/packages/axes/src/components/AxisTick.tsx +++ b/packages/axes/src/components/AxisTick.tsx @@ -1,9 +1,9 @@ import React, { useMemo } from 'react' import { animated } from 'react-spring' import { useTheme } from '@nivo/core' -import { AxisTickProps } from '../types' +import { AxisTickProps, AxisValue } from '../types' -export const AxisTick = ({ +export const AxisTick = ({ value: _value, format, lineX, diff --git a/packages/axes/src/components/Grid.tsx b/packages/axes/src/components/Grid.tsx index f29129527c..c7d0aa1950 100644 --- a/packages/axes/src/components/Grid.tsx +++ b/packages/axes/src/components/Grid.tsx @@ -1,9 +1,9 @@ import React, { useMemo } from 'react' import { GridLines } from './GridLines' import { computeGridLines } from '../compute' -import { AnyScale } from '../types' +import { AnyScale, AxisValue } from '../types' -export const Grid = ({ +export const Grid = ({ width, height, xScale, diff --git a/packages/axes/src/compute.ts b/packages/axes/src/compute.ts index 149c5f4c5e..5d834934e0 100644 --- a/packages/axes/src/compute.ts +++ b/packages/axes/src/compute.ts @@ -35,7 +35,15 @@ import { timeFormat } from 'd3-time-format' import { format as d3Format } from 'd3-format' // @ts-ignore import { textPropsByEngine } from '@nivo/core' -import { Point, TicksSpec, AnyScale, ScaleWithBandwidth, ValueFormatter, Line } from './types' +import { + AxisValue, + Point, + TicksSpec, + AnyScale, + ScaleWithBandwidth, + ValueFormatter, + Line, +} from './types' export const centerScale = (scale: ScaleWithBandwidth) => { const bandwidth = scale.bandwidth() @@ -76,7 +84,7 @@ const isInteger = (value: unknown): value is number => const isArray = (value: unknown): value is T[] => Array.isArray(value) -export const getScaleTicks = ( +export const getScaleTicks = ( scale: AnyScale, spec?: TicksSpec ) => { @@ -125,7 +133,7 @@ export const getScaleTicks = ( return scale.domain() } -export const computeCartesianTicks = ({ +export const computeCartesianTicks = ({ axis, scale, ticksPosition, @@ -212,7 +220,7 @@ export const computeCartesianTicks = ({ } } -export const getFormatter = ( +export const getFormatter = ( format: string | ValueFormatter | undefined, scale: AnyScale ): ValueFormatter | undefined => { @@ -227,7 +235,7 @@ export const getFormatter = ( return (d3Format(format) as unknown) as ValueFormatter } -export const computeGridLines = ({ +export const computeGridLines = ({ width, height, scale, diff --git a/packages/axes/src/types.ts b/packages/axes/src/types.ts index 007ca3666e..2da83cd33e 100644 --- a/packages/axes/src/types.ts +++ b/packages/axes/src/types.ts @@ -10,6 +10,8 @@ import { } from 'd3-scale' import React from 'react' +export type AxisValue = string | number | Date + export type GridValuesBuilder = T extends number ? number[] : T extends string @@ -18,7 +20,7 @@ export type GridValuesBuilder = T extends number ? Date[] : never -export type GridValues = number | GridValuesBuilder +export type GridValues = number | GridValuesBuilder export type Point = { x: number @@ -37,7 +39,7 @@ export type AnyScale = | (ScaleLogarithmic & { type: 'log' }) | ScaleWithBandwidth -export type TicksSpec = +export type TicksSpec = // exact number of ticks, please note that // depending on the current range of values, // you might not get this exact count @@ -51,9 +53,9 @@ export type TicksSpec = export type AxisLegendPosition = 'start' | 'middle' | 'end' -export type ValueFormatter = (value: Value) => Value +export type ValueFormatter = (value: Value) => Value -export interface AxisProp { +export interface AxisProp { ticksPosition?: 'before' | 'after' tickValues?: TicksSpec tickSize?: number @@ -71,7 +73,7 @@ export interface CanvasAxisProp legend?: string } -export interface AxisProps { +export interface AxisProps { axis: 'x' | 'y' scale: AnyScale x?: number @@ -91,7 +93,7 @@ export interface AxisProps { ariaHidden?: boolean } -export interface AxisTickProps { +export interface AxisTickProps { tickIndex: number value: Value format?: string | ValueFormatter