diff --git a/packages/scales/legacy/index.d.ts b/packages/scales/legacy/index.d.ts deleted file mode 100644 index 2e466e3d3..000000000 --- a/packages/scales/legacy/index.d.ts +++ /dev/null @@ -1,75 +0,0 @@ -/* - * This file is part of the nivo project. - * - * Copyright 2016-present, Raphaƫl Benitte. - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -import { CountableTimeInterval } from 'd3-time' - -declare module '@nivo/scales' { - export interface LinearScale { - type: 'linear' - min?: 'auto' | number - max?: 'auto' | number - stacked?: boolean - reverse?: boolean - clamp?: boolean - nice?: boolean | number - } - - export interface PointScale { - type: 'point' - } - - export interface TimeScaleFormatted { - type: 'time' - format: string - precision?: 'millisecond' | 'second' | 'minute' | 'hour' | 'month' | 'year' | 'day' - useUTC?: boolean - min?: 'auto' | string - max?: 'auto' | string - } - - export interface TimeScale { - type: 'time' - precision?: 'millisecond' | 'second' | 'minute' | 'hour' | 'month' | 'year' | 'day' - useUTC?: boolean - min?: 'auto' | Date - max?: 'auto' | Date - nice?: boolean | number | CountableTimeInterval - } - - export interface LogScale { - type: 'log' - base?: number - min?: 'auto' | number - max?: 'auto' | number - } - - export interface SymlogScale { - type: 'symlog' - constant?: number - min?: 'auto' | number - max?: 'auto' | number - reverse?: boolean - } - - export interface BandScale { - type: 'band' - round?: boolean - } - - export type Scale = - | LinearScale - | PointScale - | TimeScale - | TimeScaleFormatted - | LogScale - | SymlogScale - | BandScale - - export type ScaleFunc = (value: string | number | Date) => number -} diff --git a/packages/scales/src/bandScale.ts b/packages/scales/src/bandScale.ts index 84fd35406..f62fd998a 100644 --- a/packages/scales/src/bandScale.ts +++ b/packages/scales/src/bandScale.ts @@ -1,12 +1,16 @@ import { scaleBand } from 'd3-scale' -import { ComputedSerieAxis, ScaleBand, ScaleBandSpec, StringValue } from './types' +import { ComputedSerieAxis, ScaleBand, ScaleBandSpec, StringValue, ScaleAxis } from './types' export const createBandScale = ( - _spec: ScaleBandSpec, + { round = true }: ScaleBandSpec, data: ComputedSerieAxis, - size: number + size: number, + axis: ScaleAxis ) => { - const scale = scaleBand().range([0, size]).domain(data.all) + const scale = scaleBand() + .range(axis === 'x' ? [0, size] : [size, 0]) + .domain(data.all) + .round(round) const typedScale = scale as ScaleBand typedScale.type = 'band' diff --git a/packages/scales/src/compute.ts b/packages/scales/src/compute.ts index b5c995554..0f0164765 100644 --- a/packages/scales/src/compute.ts +++ b/packages/scales/src/compute.ts @@ -4,8 +4,13 @@ import sortBy from 'lodash/sortBy' import last from 'lodash/last' import isDate from 'lodash/isDate' import { createDateNormalizer } from './timeHelpers' -import { ScaleAxis, ScaleSpec, Series, ScaleValue, SerieAxis } from './types' -import { computeScale } from './computeScale' +import { ScaleAxis, ScaleSpec, Series, ScaleValue, SerieAxis, ComputedSerieAxis } from './types' +import { createLinearScale } from './linearScale' +import { createPointScale } from './pointScale' +import { createBandScale } from './bandScale' +import { createTimeScale } from './timeScale' +import { createLogScale } from './logScale' +import { createSymLogScale } from './symLogScale' type XY = ReturnType @@ -49,6 +54,30 @@ export const getOtherAxis = (axis: ScaleAxis): ScaleAxis => (axis === 'x' ? 'y' export const compareValues = (a: string | number, b: string | number) => a === b export const compareDateValues = (a: Date, b: Date) => a.getTime() === b.getTime() +export function computeScale( + spec: ScaleSpec, + data: ComputedSerieAxis, + size: number, + axis: ScaleAxis +) { + switch (spec.type) { + case 'linear': + return createLinearScale(spec, data, size, axis) + case 'point': + return createPointScale(spec, data, size) + case 'band': + return createBandScale(spec, data, size, axis) + case 'time': + return createTimeScale(spec, data, size) + case 'log': + return createLogScale(spec, data, size, axis) + case 'symlog': + return createSymLogScale(spec, data, size, axis) + default: + throw new Error('invalid scale spec') + } +} + export const computeXYScalesForSeries = ( _series: XYSeries[], xScaleSpec: ScaleSpec, diff --git a/packages/scales/src/computeScale.ts b/packages/scales/src/computeScale.ts deleted file mode 100644 index be57af625..000000000 --- a/packages/scales/src/computeScale.ts +++ /dev/null @@ -1,31 +0,0 @@ -import { ScaleAxis, ScaleSpec, ComputedSerieAxis, ScaleValue } from './types' -import { createLinearScale } from './linearScale' -import { createPointScale } from './pointScale' -import { createBandScale } from './bandScale' -import { createTimeScale } from './timeScale' -import { createLogScale } from './logScale' -import { createSymLogScale } from './symLogScale' - -export function computeScale( - spec: ScaleSpec, - data: ComputedSerieAxis, - size: number, - axis: ScaleAxis -) { - switch (spec.type) { - case 'linear': - return createLinearScale(spec, data, size, axis) - case 'point': - return createPointScale(spec, data, size) - case 'band': - return createBandScale(spec, data, size) - case 'time': - return createTimeScale(spec, data, size) - case 'log': - return createLogScale(spec, data, size, axis) - case 'symlog': - return createSymLogScale(spec, data, size, axis) - default: - throw new Error('invalid scale spec') - } -} diff --git a/packages/scales/src/types.ts b/packages/scales/src/types.ts index 76dd09be1..b5cb6941d 100644 --- a/packages/scales/src/types.ts +++ b/packages/scales/src/types.ts @@ -5,7 +5,6 @@ import { ScaleLogarithmic as D3ScaleLogarithmic, ScaleSymLog as D3ScaleSymLog, ScaleTime as D3ScaleTime, - NumberValue, } from 'd3-scale' import { TIME_PRECISION } from './timeHelpers' @@ -34,8 +33,14 @@ export interface ScaleTypeToScale { symlog: ScaleSymLog point: ScalePoint band: ScaleBand + time: ScaleTime } +export type Scale = ScaleTypeToScale[keyof ScaleTypeToScale< + Input, + Output +>] + export type ScaleLinearSpec = { type: 'linear' // default to `auto` @@ -95,6 +100,7 @@ export const isScalePointSpec = (spec: ScaleSpec): spec is ScalePointSpec => spe export type ScaleBandSpec = { type: 'band' + round?: boolean } export interface ScaleBand extends D3ScaleBand { type: 'band' @@ -117,7 +123,7 @@ export type ScaleTimeSpec = { nice?: boolean } -export interface ScaleTime extends D3ScaleTime { +export interface ScaleTime extends D3ScaleTime { type: 'time' useUTC: boolean }