Skip to content

Commit

Permalink
feat(generators): add a helper to generate x/y series
Browse files Browse the repository at this point in the history
  • Loading branch information
plouc committed Jan 12, 2022
1 parent 262c34a commit 15efb6e
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/generators/src/index.ts
Expand Up @@ -283,3 +283,4 @@ export * from './network'
export * from './parallelCoordinates'
export * from './sankey'
export * from './swarmplot'
export * from './xySeries'
58 changes: 58 additions & 0 deletions packages/generators/src/xySeries.ts
@@ -0,0 +1,58 @@
interface XYRangeStaticValues {
values: string[] | number[]
}

interface XYRandomNumericValues {
length: number
min: number
max: number
round?: boolean
}

type XYRangeValues = XYRangeStaticValues | XYRandomNumericValues

const getValueGenerator = (config: XYRangeValues) => {
let generator: (index: number) => string | number

if ('values' in config) {
generator = (index: number) => config.values[index]
} else {
generator = () => {
let value = config.min + Math.random() * (config.max - config.min)
if (config.round) {
value = Math.round(value)
}

return value
}
}

return generator
}

export const generateXYSeries = ({
serieIds,
x,
y,
}: {
serieIds: string[]
x: XYRangeValues
y: XYRangeValues
}) => {
const xLength = 'length' in x ? x.length : x.values.length

const getX = getValueGenerator(x)
const getY = getValueGenerator(y)

return serieIds.map(serieId => {
return {
id: serieId,
data: Array.from({ length: xLength }).map((_, index) => {
return {
x: getX(index),
y: getY(index),
}
}),
}
})
}

0 comments on commit 15efb6e

Please sign in to comment.