Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use as many time ticks as d3 can fit on the x-axis #146

Merged
merged 3 commits into from
Jan 7, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions giraffe/src/utils/PlotEnv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ export class PlotEnv {
this.xDomain,
this.config.width,
this.charMetrics.width,
this.config.tickFont,
this.xTickFormatter
)
}
Expand All @@ -113,6 +114,7 @@ export class PlotEnv {
this.yDomain,
this.config.height,
this.charMetrics.height,
this.config.tickFont,
this.yTickFormatter
)
}
Expand Down
4 changes: 4 additions & 0 deletions giraffe/src/utils/__mocks__/getTextMetrics.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const getTextMetrics = (...args) => ({
width: Math.floor(args[1].length * 1.2),
height: 12,
})
103 changes: 103 additions & 0 deletions giraffe/src/utils/getTicks.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import {FormatterType} from '../types'
import {getTicks} from './getTicks'

jest.mock('./getTextMetrics')

describe('getTicks', () => {
const charHeight = 12
const font = '10px sans-serif'

describe('vertical axis', () => {
const laptopScreenHeight = 788
const formatter = (num: number): string => `${num} foo`
const domain = [0, 100]
const result = [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100]

it('should give the correct number of ticks', () => {
expect(
getTicks(domain, laptopScreenHeight, charHeight, font, formatter)
).toEqual(result)
})
it('should handle extreme or unusual inputs', () => {
expect(getTicks(domain, 0, charHeight, font, formatter)).toEqual([])
expect(getTicks(domain, laptopScreenHeight, 0, font, formatter)).toEqual(
[]
)
expect(
getTicks([0, 0], laptopScreenHeight, charHeight, font, formatter)
).toEqual([0])
expect(
getTicks(domain, laptopScreenHeight, charHeight, font, formatter)
).toEqual(result)
})
})

describe('horizontal axis', () => {
const axisLength = 1380
const formatter = (num: number): string => new Date(num).toDateString()
formatter._GIRAFFE_FORMATTER_TYPE = FormatterType.Time
const domain = [1578355945276, 1578357085276]
const result = [
1578355950000,
1578355980000,
1578356010000,
1578356040000,
1578356070000,
1578356100000,
1578356130000,
1578356160000,
1578356190000,
1578356220000,
1578356250000,
1578356280000,
1578356310000,
1578356340000,
1578356370000,
1578356400000,
1578356430000,
1578356460000,
1578356490000,
1578356520000,
1578356550000,
1578356580000,
1578356610000,
1578356640000,
1578356670000,
1578356700000,
1578356730000,
1578356760000,
1578356790000,
1578356820000,
1578356850000,
1578356880000,
1578356910000,
1578356940000,
1578356970000,
1578357000000,
1578357030000,
1578357060000,
]
it('should give the correct number of ticks', () => {
expect(getTicks(domain, axisLength, charHeight, font, formatter)).toEqual(
result
)
})
it('should handle extreme or unusual inputs', () => {
expect(getTicks(domain, 0, charHeight, font, formatter)).toEqual([])
expect(getTicks([0, 0], axisLength, charHeight, font, formatter)).toEqual(
[0]
)
expect(getTicks(domain, axisLength, 0, font, formatter)).toEqual(result)
expect(
getTicks(domain, axisLength, 100000000000000000, font, formatter)
).toEqual(result)
expect(getTicks(domain, axisLength, charHeight, font, formatter)).toEqual(
result
)
expect(
getTicks(domain, axisLength / 2, charHeight, font, formatter).length
).toBeLessThan(result.length)
expect(getTicks(domain, 0, charHeight, font, formatter).length).toEqual(0)
})
})
})
81 changes: 51 additions & 30 deletions giraffe/src/utils/getTicks.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,13 @@
// Libraries
import {scaleUtc} from 'd3-scale'
import {ticks} from 'd3-array'
import memoizeOne from 'memoize-one'

// Types
import {Formatter, FormatterType} from '../types'

export const getTicks = (
domain: number[],
rangeLength: number,
charLength: number,
formatter?: Formatter
): number[] => {
const sampleTick = formatter(domain[1])
const numTicks = getNumTicks(sampleTick, rangeLength, charLength)
switch (formatter._GIRAFFE_FORMATTER_TYPE) {
case FormatterType.Time:
return getTimeTicks(domain, rangeLength, numTicks)

default:
return ticks(domain[0], domain[1], numTicks)
}
}
// Utils
import {getTextMetrics} from './getTextMetrics'

const getNumTicks = (
sampleTick: string,
Expand All @@ -29,21 +18,53 @@ const getNumTicks = (
return sampleTickWidth === 0 ? 0 : Math.round(length / sampleTickWidth)
}

const getTimeTicks = (
const getOptimalTimeTicks = (
[d0, d1]: number[],
length: number,
numTicks: number
): number[] => {
const results = scaleUtc()
rangeLength: number,
timeTickLength: number
): Date[] => {
const maxUseableLength = rangeLength - timeTickLength
const scaledTime = scaleUtc()
.domain([d0, d1])
.range([0, length])
.ticks(numTicks)
.map(d => d.getTime())
// added this to force D3 to use the numTicks since D3
// treats the tick params as suggestions:
// https://observablehq.com/@d3/scale-ticks
if (results.length > numTicks) {
return results.slice(0, numTicks)
.range([0, rangeLength])

const maxNumTicks = Math.floor(rangeLength / timeTickLength)

let optimalTicks = scaledTime.ticks(maxNumTicks)
let counter = 1

while (
counter < maxNumTicks &&
optimalTicks.length * timeTickLength > maxUseableLength
) {
optimalTicks = scaledTime.ticks(maxNumTicks - counter)
counter += 1
}
return optimalTicks
}

const getTimeTicksMemoized = memoizeOne(
([d0, d1]: number[], length: number, timeTickLength: number): number[] => {
const timeTicks = getOptimalTimeTicks([d0, d1], length, timeTickLength)
return timeTicks.map(d => d.getTime())
}
)

export const getTicks = (
domain: number[],
rangeLength: number,
charLength: number,
tickFont: string,
formatter: Formatter
): number[] => {
const sampleTick = formatter(domain[1])
const tickTextMetrics = getTextMetrics(tickFont, sampleTick)
const numTicks = getNumTicks(sampleTick, rangeLength, charLength)
switch (formatter._GIRAFFE_FORMATTER_TYPE) {
case FormatterType.Time:
return getTimeTicksMemoized(domain, rangeLength, tickTextMetrics.width)

default:
return ticks(domain[0], domain[1], numTicks)
}
return results
}
6 changes: 4 additions & 2 deletions stories/src/index.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ storiesOf('XY Plot', module)
'DD/MM/YYYY HH:mm:ss.sss': 'DD/MM/YYYY HH:mm:ss.sss',
'MM/DD/YYYY HH:mm:ss.sss': 'MM/DD/YYYY HH:mm:ss.sss',
'YYYY/MM/DD HH:mm:ss': 'YYYY/MM/DD HH:mm:ss',
'YYYY-MM-DD HH:mm:ss ZZ': 'YYYY-MM-DD HH:mm:ss ZZ',
'hh:mm a': 'hh:mm a',
'HH:mm': 'HH:mm',
'HH:mm:ss': 'HH:mm:ss',
Expand All @@ -49,7 +50,7 @@ storiesOf('XY Plot', module)
'MMMM D, YYYY HH:mm:ss': 'MMMM D, YYYY HH:mm:ss',
'dddd, MMMM D, YYYY HH:mm:ss': 'dddd, MMMM D, YYYY HH:mm:ss',
},
'hh:mm a'
'YYYY-MM-DD HH:mm:ss ZZ'
)
const fill = fillKnob(table, 'cpu')
const position = select(
Expand Down Expand Up @@ -118,6 +119,7 @@ storiesOf('XY Plot', module)
'DD/MM/YYYY HH:mm:ss.sss': 'DD/MM/YYYY HH:mm:ss.sss',
'MM/DD/YYYY HH:mm:ss.sss': 'MM/DD/YYYY HH:mm:ss.sss',
'YYYY/MM/DD HH:mm:ss': 'YYYY/MM/DD HH:mm:ss',
'YYYY-MM-DD HH:mm:ss ZZ': 'YYYY-MM-DD HH:mm:ss ZZ',
'hh:mm a': 'hh:mm a',
'HH:mm': 'HH:mm',
'HH:mm:ss': 'HH:mm:ss',
Expand All @@ -126,7 +128,7 @@ storiesOf('XY Plot', module)
'MMMM D, YYYY HH:mm:ss': 'MMMM D, YYYY HH:mm:ss',
'dddd, MMMM D, YYYY HH:mm:ss': 'dddd, MMMM D, YYYY HH:mm:ss',
},
'YYYY/MM/DD HH:mm:ss'
'YYYY-MM-DD HH:mm:ss ZZ'
)
const fill = fillKnob(table, 'cpu')
const interpolation = interpolationKnob()
Expand Down