-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(generators): add a halper to generate x/y series
- Loading branch information
Showing
2 changed files
with
59 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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), | ||
} | ||
}), | ||
} | ||
}) | ||
} |