Skip to content

Commit

Permalink
fix(scales): handle empty series with timescale (#1683)
Browse files Browse the repository at this point in the history
  • Loading branch information
wyze authored Jul 14, 2021
1 parent 3e0f074 commit 81880cc
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 5 deletions.
6 changes: 5 additions & 1 deletion packages/scales/src/timeHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@ export const createDateNormalizer = ({
}) => {
const precisionFn = createPrecisionMethod(precision)

return (value: Date | string) => {
return (value: Date | string | undefined) => {
if (value === undefined) {
return value
}

if (format === 'native' || value instanceof Date) {
return precisionFn(value as Date)
}
Expand Down
8 changes: 5 additions & 3 deletions packages/scales/src/timeScale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const createTimeScale = <Input extends Date | NumberValue>(
) => {
const normalize = createDateNormalizer({ format, precision, useUTC })

let minValue: Date
let minValue: Date | undefined
if (min === 'auto') {
minValue = normalize(data.min)
} else if (format !== 'native') {
Expand All @@ -25,7 +25,7 @@ export const createTimeScale = <Input extends Date | NumberValue>(
minValue = min as Date
}

let maxValue: Date
let maxValue: Date | undefined
if (max === 'auto') {
maxValue = normalize(data.max)
} else if (format !== 'native') {
Expand All @@ -36,7 +36,9 @@ export const createTimeScale = <Input extends Date | NumberValue>(

const scale = useUTC ? scaleUtc() : scaleTime()

scale.domain([minValue, maxValue]).range([0, size])
scale.range([0, size])

if (minValue && maxValue) scale.domain([minValue, maxValue])

if (nice === true) scale.nice()
else if (typeof nice === 'object' || typeof nice === 'number') scale.nice(nice)
Expand Down
45 changes: 44 additions & 1 deletion packages/scales/tests/compute.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { generateSeriesAxis, stackAxis, computeAxisSlices } from '../src/compute'
import {
generateSeriesAxis,
stackAxis,
computeAxisSlices,
computeXYScalesForSeries,
} from '../src/compute'

const axes = ['x', 'y']

Expand Down Expand Up @@ -597,6 +602,44 @@ describe('stackAxis', () => {
})
})

describe('computeXYScalesForSeries', () => {
it('works with timescale an and empty series', () => {
const series = computeXYScalesForSeries(
[],
{
type: 'time',
format: '%Y-%m-%d',
useUTC: false,
precision: 'day',
},
{
type: 'linear',
stacked: false,
},
100,
100
)

expect(series).toMatchInlineSnapshot(`
Object {
"series": Array [],
"x": Object {
"all": Array [],
"max": undefined,
"min": undefined,
},
"xScale": [Function],
"y": Object {
"all": Array [],
"max": -Infinity,
"min": Infinity,
},
"yScale": [Function],
}
`)
})
})

// describe('computeAxisSlices', () => {
// axes.forEach(axis => {
// const otherAxis = axis === 'x' ? 'y' : 'x'
Expand Down

0 comments on commit 81880cc

Please sign in to comment.