Skip to content

Commit

Permalink
fix(axes): respect useUTC option on x/y scale property (#574)
Browse files Browse the repository at this point in the history
  • Loading branch information
wyze authored and plouc committed Jun 27, 2019
1 parent cca8a9e commit b4ca5ec
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/axes/src/compute.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export const getScaleTicks = (scale, spec) => {
// UTC is used as it's more predictible
// however local time could be used too
// let's see how it fits users' requirements
const timeType = timeByType[matches[2]][1]
const timeType = timeByType[matches[2]][scale.useUTC ? 1 : 0]
if (matches[1] === undefined) {
return scale.ticks(timeType)
}
Expand Down
27 changes: 26 additions & 1 deletion packages/axes/tests/compute.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
import { scaleLinear, scaleOrdinal, scalePoint, scaleBand, scaleUtc } from 'd3-scale'
import { scaleLinear, scaleOrdinal, scalePoint, scaleBand, scaleTime, scaleUtc } from 'd3-scale'
import { getScaleTicks, computeCartesianTicks } from '../src/compute'

describe('getTicks', () => {
Expand Down Expand Up @@ -72,6 +72,29 @@ describe('getTicks', () => {
])
})

it('should support non-UTC dates', () => {
const noUtcTimeScale = scaleTime().domain([
new Date(2000, 0, 1, 0, 0, 0, 0),
new Date(2001, 0, 1, 0, 0, 0, 0),
])

expect(getScaleTicks(noUtcTimeScale)).toEqual([
new Date(2000, 0, 1, 0, 0, 0, 0),
new Date(2000, 1, 1, 0, 0, 0, 0),
new Date(2000, 2, 1, 0, 0, 0, 0),
new Date(2000, 3, 1, 0, 0, 0, 0),
new Date(2000, 4, 1, 0, 0, 0, 0),
new Date(2000, 5, 1, 0, 0, 0, 0),
new Date(2000, 6, 1, 0, 0, 0, 0),
new Date(2000, 7, 1, 0, 0, 0, 0),
new Date(2000, 8, 1, 0, 0, 0, 0),
new Date(2000, 9, 1, 0, 0, 0, 0),
new Date(2000, 10, 1, 0, 0, 0, 0),
new Date(2000, 11, 1, 0, 0, 0, 0),
new Date(2001, 0, 1, 0, 0, 0, 0),
])
})

const intervals = [
{
interval: '5 years',
Expand Down Expand Up @@ -160,6 +183,8 @@ describe('getTicks', () => {
it(`should support ${interval.interval} interval`, () => {
const intervalTimeScale = scaleUtc().domain(interval.domain)

intervalTimeScale.useUTC = true

expect(getScaleTicks(intervalTimeScale, `every ${interval.interval}`)).toEqual(
interval.expect
)
Expand Down
1 change: 1 addition & 0 deletions packages/scales/src/timeScale.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export const timeScale = (
scale.domain([minValue, maxValue]).range([0, size])

scale.type = 'time'
scale.useUTC = useUTC

return scale
}
Expand Down

0 comments on commit b4ca5ec

Please sign in to comment.