Skip to content

Commit

Permalink
feat: fill line plot data with undefined
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel J. Lauk committed Dec 3, 2021
1 parent 1404ecb commit 5399d8f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 10 deletions.
22 changes: 17 additions & 5 deletions src/transforms.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,20 @@ import type { Datapoint } from './transforms'

describe('module transforms', () => {
describe('transformToLinePlotData', () => {
it('returns empty datapoints on empty series', () => {
it('returns y undefined on empty series', () => {
const input: Datapoint<number>[] = []
const output = transformToLinePlotData(input, {
startAt: 100,
endAt: 130,
stepSize: 10,
})
expect(output).toHaveLength(0)
const expectedDatapoints = [
{ x: 100, y: undefined },
{ x: 110, y: undefined },
{ x: 120, y: undefined },
{ x: 130, y: undefined },
]
expect(output).toEqual(expectedDatapoints)
})

it('returns empty datapoints if startAt > endAt', () => {
Expand All @@ -26,7 +32,7 @@ describe('module transforms', () => {
expect(output).toHaveLength(0)
})

it('returns empty datapoints on input after range', () => {
it('returns y undefined on input after range', () => {
const input: Datapoint<number>[] = [
{ x: 200, y: 1 },
{ x: 300, y: 2 },
Expand All @@ -36,7 +42,13 @@ describe('module transforms', () => {
endAt: 130,
stepSize: 10,
})
expect(output).toHaveLength(0)
const expectedDatapoints = [
{ x: 100, y: undefined },
{ x: 110, y: undefined },
{ x: 120, y: undefined },
{ x: 130, y: undefined },
]
expect(output).toEqual(expectedDatapoints)
})

it('repeats last datapoint on input before range', () => {
Expand Down Expand Up @@ -93,7 +105,7 @@ describe('module transforms', () => {
stepSize: 10,
})
const expectedDatapoints = [
// no data point for x = 100
{ x: 100, y: undefined },
{ x: 110, y: 1 },
{ x: 120, y: 4 },
{ x: 130, y: 4 },
Expand Down
24 changes: 19 additions & 5 deletions src/transforms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,40 @@ export type Datapoint<Y> = { x: number; y: Y }
* and then creating a new data series by tracing the line and noting
* an (x/y) pair at fixed intervals (steps of x).
*
* Please note:
* If there is no data (at the beginning), data points will be created
* for it with `y = undefined`.
* As a consequence, the size of the resulting array depends only on
* the options, not on the input data.
*
* @param datapoints The data points to be transformed
* @param options Controls the transformation
* @returns The transformed data points
*/
export function transformToLinePlotData<Y>(
datapoints: Datapoint<Y>[],
options: { stepSize: number; startAt: number; endAt: number }
): Datapoint<Y>[] {
const result: Datapoint<Y>[] = []
if (datapoints.length === 0) return result

): Datapoint<Y | undefined>[] {
const result: Datapoint<Y | undefined>[] = []
const { startAt, endAt, stepSize } = options

if (datapoints.length === 0) {
for (let x = startAt; x <= endAt; x += stepSize) {
result.push({ x, y: undefined })
}
return result
}

// indexes into series.datapoints
let lastIdx = 0
let nextIdx
let y = datapoints[lastIdx].y
let endReached = false
for (let x = startAt; x <= endAt; x += stepSize) {
if (datapoints[lastIdx].x > x) continue
if (datapoints[lastIdx].x > x) {
result.push({ x, y: undefined })
continue
}
if (!endReached) {
// move forward, until we cross x or we reach the end
nextIdx = lastIdx + 1
Expand Down

0 comments on commit 5399d8f

Please sign in to comment.