Skip to content

Commit

Permalink
refactor(scales): move things around and work with other packages
Browse files Browse the repository at this point in the history
  • Loading branch information
wyze committed Jun 22, 2021
1 parent 7393acf commit cdd88ef
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 114 deletions.
75 changes: 0 additions & 75 deletions packages/scales/legacy/index.d.ts

This file was deleted.

12 changes: 8 additions & 4 deletions packages/scales/src/bandScale.ts
Original file line number Diff line number Diff line change
@@ -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 = <Input extends StringValue>(
_spec: ScaleBandSpec,
{ round = true }: ScaleBandSpec,
data: ComputedSerieAxis<Input>,
size: number
size: number,
axis: ScaleAxis
) => {
const scale = scaleBand<Input>().range([0, size]).domain(data.all)
const scale = scaleBand<Input>()
.range(axis === 'x' ? [0, size] : [size, 0])
.domain(data.all)
.round(round)

const typedScale = scale as ScaleBand<Input>
typedScale.type = 'band'
Expand Down
33 changes: 31 additions & 2 deletions packages/scales/src/compute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof generateSeriesXY>

Expand Down Expand Up @@ -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<Input extends ScaleValue>(
spec: ScaleSpec,
data: ComputedSerieAxis<any>,
size: number,
axis: ScaleAxis
) {
switch (spec.type) {
case 'linear':
return createLinearScale(spec, data, size, axis)
case 'point':
return createPointScale<Input>(spec, data, size)
case 'band':
return createBandScale<Input>(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,
Expand Down
31 changes: 0 additions & 31 deletions packages/scales/src/computeScale.ts

This file was deleted.

10 changes: 8 additions & 2 deletions packages/scales/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
ScaleLogarithmic as D3ScaleLogarithmic,
ScaleSymLog as D3ScaleSymLog,
ScaleTime as D3ScaleTime,
NumberValue,
} from 'd3-scale'
import { TIME_PRECISION } from './timeHelpers'

Expand Down Expand Up @@ -34,8 +33,14 @@ export interface ScaleTypeToScale<Input, Output> {
symlog: ScaleSymLog
point: ScalePoint<Input>
band: ScaleBand<Input>
time: ScaleTime<Input>
}

export type Scale<Input, Output> = ScaleTypeToScale<Input, Output>[keyof ScaleTypeToScale<
Input,
Output
>]

export type ScaleLinearSpec = {
type: 'linear'
// default to `auto`
Expand Down Expand Up @@ -95,6 +100,7 @@ export const isScalePointSpec = (spec: ScaleSpec): spec is ScalePointSpec => spe

export type ScaleBandSpec = {
type: 'band'
round?: boolean
}
export interface ScaleBand<Input extends StringValue> extends D3ScaleBand<Input> {
type: 'band'
Expand All @@ -117,7 +123,7 @@ export type ScaleTimeSpec = {
nice?: boolean
}

export interface ScaleTime<Input extends Date | NumberValue> extends D3ScaleTime<Input, Date> {
export interface ScaleTime<Input> extends D3ScaleTime<Input, number> {
type: 'time'
useUTC: boolean
}
Expand Down

0 comments on commit cdd88ef

Please sign in to comment.